[Operation and maintenance engineer learning three] shell programming
-
- Shell program classification
- 1. The sh command in the system is a soft link of bash
- 2. File suffix in standard format of Shell script
- 3. The first line of the file content in the standard format of Shell script
- 4. How to run the Shell script
- 5. Common commands of find, grep, xargs, sort, uniq, tr, cut, paste, wc, sed, awk
-
- 1. `find`: Used to search for files or directories in the file system.
- 2. `grep`: Used to find a specific pattern in a file or input.
- 3. `xargs`: Used to read arguments from standard input and pass them to other commands.
- 4. `sort`: used to sort the text.
- 5. `uniq`: Used to filter out unique rows from the sorted input.
- 6. `tr`: Used for character replacement and deletion.
- 7. `cut`: Used to extract specified fields from text.
- 8. `paste`: Used to merge the contents of multiple files into one line.
- 9. `wc`: Used to calculate the number of lines, words and characters of a file or text.
- 10. `sed`: for streaming text editing.
- 11. `awk`: Used to process structured text data.
Shell program classification
There are many shell programs, such as, Korn shell(ksh)、Bourne Again shell(bash)、C shell(包括csh与tcsh)
etc.,
the default shells under each major operating system:
- Under AIX is
Korn Shell
- The Solaris default is
Bourne shell
- FreeBSD defaults to
C shell
- HP-UX defaults to
POSIX shell
- Linux default is
Bourne Again shell
1. The sh command in the system is a soft link of bash
sh=bash
file /usr/bin/sh
sh --version
/usr/bin/bash --version
whereis sh
But this kind of command in the command line outputs the result immediately and cannot be reused (repeated input can be reused, but it is extremely inconvenient if the command needs to be input in multiple lines). To facilitate the reuse of the same function, put Multi-line commands are placed in a standard format file, which is shell programming.
So the so-called shell programming is a collection of shell commands with one or more lines of different functions ! The file of this shell command collection is called shell script (shell script).
2. File suffix in standard format of Shell script
Shell script files are generally suffixed with .sh. The file fin shown in the figure below is the default shell script file on the system.
find /-name *.sh
3. The first line of the file content in the standard format of Shell script
The first line usually starts with #!/bin/sh
or #!/bin/bash
( #!/usr/bin/sh或#!/usr/bin/bash
or you may see other paths, or all of them). The meaning of this line is that it tells the system what interpreter (that is, what shell, this system is bash) is required for this script to execute.
Starting from the second line, any line starting with # means that the line will not be executed, and it is mostly used for comments and explanations.
Note: It is in units of behavior.
It is a valid command and code starting from the line that does not start with #.
Due to space reasons, this screenshot saves a lot of content later, please check the complete content of the file on the system by yourself.
4. How to run the Shell script
1. As an executable program
#!/bin/sh
ip addr | grep -v 127.0.0.1 | grep -v inet6 | grep inet | awk '{print $2}' | cut -d / -f 1
explain
The function of this shell script is to obtain the local IPv4 address and output it.
Let me explain the specific content of this script step by step:
-
#!/bin/sh
: This is a shebang (also known as a hashbang) that specifies which interpreter the script uses to execute. Here,#!/bin/sh
indicates that the script will be executed using the default shell interpreter, usually Bash or a similar shell. -
ip addr
: This command is used to get information about network interfaces, including IP addresses and other network configurations. -
grep -v 127.0.0.1
: This command is used to filter out127.0.0.1
the lines contained in the IP address, that is, to filter out the loopback address. -
grep -v inet6
: This command is used to filter outinet6
the lines contained in the IP address, that is, to filter out the IPv6 address. -
grep inet
: This command is used to filter out the lines containinginet
(IPv4 address). -
awk '{print $2}'
: This command uses awk to extract the second field (IPv4 address) of each line and print it out. -
cut -d / -f 1
: This command uses cut to/
separate according to , extracts the first field after separation (the host part of the IPv4 address), and prints it out.
Taken together, the function of this script is to obtain the local IPv4 address (except the local loopback address and IPv6 address) and output it.
Please note that this script is executed in a Linux environment, you may need to ensure that the required commands (such as ip, grep, awk, cut) are installed in your system.
Save the above content in the file /root
in the directory showip.sh
, and cd to the /root directory.
chmod +x ./showip.sh #使脚本具有执行权限
./showip.sh #执行脚本
2. As an interpreter (bash) parameter
The interpreter (shell) of this system is bash, and we know that sh is a soft link of bash (similar to a shortcut under windows), the most commonly used method is "sh script file name".
- The latter two are just intentional demonstrations. Please use the method above. The reason is: less input and thus higher efficiency.
- We know that sh is a command, and what follows the command is called the command parameter. So this method is called "running the script as an argument to the interpreter".
- However, this method runs the script regardless of the first line in the script (that is, it does not matter whether the first line is written or not). Why? ! Because the interpreter is called directly!
5. Common commands of find, grep, xargs, sort, uniq, tr, cut, paste, wc, sed, awk
When it comes to text processing and data manipulation, here are some common commands for , find
, grep
, xargs
, sort
, , , , , and :uniq
tr
cut
paste
wc
sed
awk
1. find
: Used to search for a file or directory in the file system.
find /path/to/directory -name "filename"
: Search for files in the specified directory according to the file name.find /path/to/directory -type f
: Find all files in the specified directory.find /path/to/directory -type d
: Find all directories under the specified directory.
2. grep
: Used to find a specific pattern in a file or input.
grep "pattern" file.txt
: Searches the file for lines containing the specified pattern.cat file.txt | grep "pattern"
: Searches the input stream for lines containing the specified pattern.
3. xargs
: Used to read arguments from standard input and pass them to other commands.
-
find /path/to/directory -name "*.txt" | xargs rm
: Find all.txt
files ending with and delete them. -
find /path/to/directory -name "*.txt" | xargs cat
: Find all.txt
files ending with and view them.
4. sort
: Used to sort the text.
-
sort file.txt
: Sorts the lines in the file alphabetically. -
sort -n file.txt
: Sort the lines in the file by numerical size.
5. uniq
: Used to filter out unique rows from the sorted input.
-
sort file.txt | uniq
: Sorts the lines in the file and eliminates duplicate lines. -
sort file.txt | uniq -c
: Counts the number of occurrences of each unique row.
6. tr
: Used for character replacement and deletion.
cat file.txt | tr 'a' 'b'
: Replace all letters 'a' in the input with 'b'.cat file.txt | tr -d 'a'
: Removes all letter 'a' in the input.
7. cut
: Used to extract the specified field from the text.
cut -d ',' -f 1,3 file.csv
: Use commas as separators to extract the first and third fields in the CSV file.
8. paste
: Used to combine the contents of multiple files into one line.
paste file1.txt file2.txt
: Merge the contents of two files side by side.
9. wc
: Used to count the number of lines, words and characters of a file or text.
wc -l file.txt
: Count the number of lines in the file.echo "Hello, World!" | wc -w
: Calculate the word count of the input content.
10. sed
: For streaming text editing.
sed 's/pattern/replacement/g' file.txt
pattern
: Replace all occurrences of in the file withreplacement
.sed '/pattern/d' file.txt
: Delete the line containing from the filepattern
.
11. awk
: Used to process structured text data.
awk '{print $1}' file.txt
: Print the first field of each line in the file.awk -F ',' '{print NF}' file.csv
: Counts the number of fields for each line in the file.
The above are just some common usage examples of these commands, they have more powerful and flexible functions. You can consult the documentation of the command or run it on the terminal man <command>
for more detailed usage and options.