directory

  1. Basic commands
  2. variable
  3. Create and run the script
  4. conditions
  5. cycle
  6. function
  7. Others (read, mktemp, Trap)

Create and run the script

A script, “script,” is a text file that contains a series of commands. The Shell reads this file and executes all the commands in turn as if they were typed directly to the command line. All tasks that can be done from the command line can be done with scripts, which have the advantage of being reusable and can be specified for automatic invocation in specific situations, such as when the system is started or shut down.

Write a script

  1. Create a script file and grant execution permission
$chmod +rx script.sh # or $chmod 755 script.shCopy the code
  1. Open the script and specify the script interpreter in the first line of the script
#! /bin/bashCopy the code
  1. Add comments
Echo 'Hello World! 'Copy the code
  1. Script parameters

When executing a script, you can carry arguments. In the following example, script.sh is a script file, and word1, word2, and word3 are the three arguments.

$ ./script.sh word1 word2 word3
Copy the code

Inside the script file, you can reference these parameters using special variables.

  • $0: Indicates the name of the script filescript.sh.
  • The $1~$9: corresponds to the first to the ninth parameter of the script.
  • $#: Indicates the total number of parameters.
  • $@: All parameters are separated by Spaces.
  • $*: All parameters, using variables between parameters$IFSValue is delimited by the first character. Default is space, but you can customize it.

If the script has more than nine arguments, the 10th argument can be referenced as ${10}, and so on.

Note that if the command is command-o foo bar, then -o is $1, foo is $2, and bar is $3.

The user can enter any number of parameters, and with the for loop, each parameter can be read.

#! /bin/bash for i in "${@}"; do echo "${i}" doneCopy the code
  1. Exit script command

The exit command is used to terminate the execution of the current script and return an exit value to the Shell.

$ exit
Copy the code

The exit command can be followed by a parameter that indicates the exit status. 0 indicates normal, and other parameters indicate an error.

# exit value 0(success) $exit 0 # exit value 1(failure) $exit 1Copy the code
  1. Execute the script

There are two ways to execute a script, one is to open a subshell to execute the script, and the other is to execute the script in the current Shell.

  • The subshell execution script can use the following command directly
./script.sh
Copy the code
  • To execute scripts in the current ShellsourceThe command

The source command can be used to execute a script or load a configuration file.

$ source .bashrc
Copy the code

The main feature of the source command is that the script is executed in the current Shell. Therefore, the source command does not need the export variable to execute the script.

#! /bin/bash # test.sh echo $fooCopy the code

The script above prints the value of the $foo variable.

# create a new variable foo $foo=1 # print out 1 $source test.sh 1 # print out the empty string $./test.shCopy the code

In the example above, the current Shell variable foo is not exported, so the direct execution cannot read it, but the source execution can.

Another use of the source command is to load external libraries inside the script.

#! /bin/bash source ./lib.sh function_from_libCopy the code

The above script internally loads an external library using the source command, and then uses the functions defined by the external library inside the script.

Source has a short form that uses a dot (.) To represent.

$ . .bashrc
Copy the code