A Shell script

To write Python and PHP scripts, you usually need to master the functions of the language, but not Shell scripts. You only need to master Linux commands to write Shell scripts, because Shell scripts are composed of multiple Linux commands. By combining multiple Linux commands and saving them into a script file, Can be used directly by others.


Combined command

To access a directory and view the files in the directory, run the CD and ls commands respectively.

The format of the two commands is as follows:

[root@lincoding usr]# cd /usr/
[root@lincoding usr]#
[root@lincoding usr]# ls
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@lincoding usr]#
Copy the code

We can use a semicolon; , to combine the two commands and execute them in sequence, then execute them together in the following form:

[root@lincoding usr]# cd /usr/ ; ls
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@lincoding usr]#
Copy the code

The process of writing Shell scripts

So if these two commands are used frequently or provided to others, we can save these two commands as Shell script files.

01 Creating a Shell script file

Shells that use bash usually end with the.sh suffix

[root@lincoding home]# touch test.sh
Copy the code

02 Writing Shell Scripts

Run the vi command to compile the test.sh script. The content is as follows:

cd /usr/
ls
Copy the code

Note that Shell scripts do not use a semicolon after each statement; Each command takes the form of a line break, and is executed sequentially when the Shell script is executed.

03 Grant the execute permission to Shell scripts

When creating a file, the script does not have the execute permission by default. Therefore, the script can run only when the script has the execute permission

[root@lincoding home]# chmod u+x test.sh
Copy the code

Viewing Script Permissions

[root@lincoding home]# ls -l test.sh
-rwxr--r--. 1 root root 13 Sep 12 09:10 test.sh
Copy the code

04 Run the Shell script

Using bash to execute Shell scripts, the result is the same as if we had executed single-line combined commands outside

[root@lincoding home]# bash test.sh
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
Copy the code

Declare the Shell interpreter

If the default Shell is not bash, executing the Shell script may fail because some of bash’s Shell features may be in it.

We can declare which Shell is used on the first line of the Shell script file in the following format:

#! /bin/bashCopy the code

The advantage of this is that when a Shell script is executed, the system is automatically told to use the Shell of the Bash interpreter to execute the script.

We will modify the test.sh script as follows:

#! /bin/bash
cd /usr/
ls
Copy the code

By declaring which Shell interpreter to use, the way we execute the script becomes much simpler

[root@lincoding home]# ./test.sh
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
Copy the code

summary

When we write a Shell script, the first line begins with #! /bin/bash declares the Shell interpreter, gives the Shell permission to execute, and then executes.