[Operation and maintenance engineer learning three] Shell scripting in Linux

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 isKorn Shell
  • The Solaris default isBourne shell
  • FreeBSD defaults toC shell
  • HP-UX defaults toPOSIX shell
  • Linux default isBourne 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

insert image description here

 whereis sh

insert image description here
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

insert image description here

3. The first line of the file content in the standard format of Shell script

The first line usually starts with #!/bin/shor #!/bin/bash( #!/usr/bin/sh或#!/usr/bin/bashor 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.
insert image description here
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:

  1. #!/bin/sh: This is a shebang (also known as a hashbang) that specifies which interpreter the script uses to execute. Here, #!/bin/shindicates that the script will be executed using the default shell interpreter, usually Bash or a similar shell.

  2. ip addr: This command is used to get information about network interfaces, including IP addresses and other network configurations.

  3. grep -v 127.0.0.1: This command is used to filter out 127.0.0.1the lines contained in the IP address, that is, to filter out the loopback address.

  4. grep -v inet6: This command is used to filter out inet6the lines contained in the IP address, that is, to filter out the IPv6 address.

  5. grep inet: This command is used to filter out the lines containing inet(IPv4 address).

  6. awk '{print $2}': This command uses awk to extract the second field (IPv4 address) of each line and print it out.

  7. 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 /rootin the directory showip.sh, and cd to the /root directory.

chmod +x ./showip.sh  	#使脚本具有执行权限
./showip.sh 		 #执行脚本

insert image description here
insert image description here

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".
insert image description here

  • 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 :uniqtrcutpastewcsedawk

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.

insert image description here

insert image description here
insert image description here

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.
    insert image description here
    insert image description here

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 .txtfiles ending with and delete them.

  • find /path/to/directory -name "*.txt" | xargs cat: Find all .txtfiles ending with and view them.
    insert image description here
    insert image description here

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.
    insert image description here
    insert image description here

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.
    insert image description here
    insert image description here

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.
    insert image description here
    insert image description here

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.
    insert image description here

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.
    insert image description here
    insert image description here
    insert image description here

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.
    insert image description here
    insert image description here

10. sed: For streaming text editing.

  • sed 's/pattern/replacement/g' file.txtpattern: Replace all occurrences of in the file with replacement.
  • sed '/pattern/d' file.txt: Delete the line containing from the file pattern.
    insert image description here
    insert image description here

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.
    insert image description here

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.

Guess you like

Origin blog.csdn.net/weixin_43576565/article/details/131471878