Simple bird beginner Shell Programming (b) the preparation of Shell scripts

Shell Script

Written in Python, PHP scripts usually need to have a functional language, then you do not need Shell scripts, just need to know Linux commands can write Shell scripts, Shell scripts because Linux is composed of a plurality of commands, save by multiple Linux commands into a script file can be used directly to others.


A combination of command

Enter a directory, see directory of files, this process needs to execute two commands are, respectively cd, and ls.

Two separate forms of execution of the command 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]#

We can use the semicolons ;to command the two together in combination, the order of execution, the execution of the form with the following:

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

Shell script writing process

So if these two frequently used commands or provided to others to use, we can save these two commands together with Shell script file.

01 establish Shell script file

Use the bash Shell usually .shsuffix

[root@lincoding home]# touch test.sh

02 Shell script writing

By vicommand to write test.sha script, reads as follows:

cd /usr/
ls

Note that Shell script behind each statement without a semicolon ;, each command by way of wrap, Shell script execution time of the order will be executed.

Shell scripts execute permissions given 03

Because when building the file, the default is not authorized to perform, we need to give permission to execute the script, the script can run

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

View Script Permissions

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

04 Shell script execution

Bash Shell script execution with the results of the implementation and results of our execution on the outside is the same one-line command combination

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

Shell statement interpreter

So here also consider other issues, assuming that Shell put this script will be a problem when running under a different system, if the system is not the default Shell bash, Shell execute this script may fail because there may be Some of the bash Shell properties in it.

Then we can at Shell script file first line declares it used to be which Shell, written in the following format:

#!/bin/bash

The benefits of this writing, the Shell script execution time, the system will automatically tell by bashShell interpreter to execute the script.

We will just test.sh script is modified as follows:

#!/bin/bash
cd /usr/
ls

So after which Shell statement using an interpreter, the way we execute scripts can become very simple

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

summary

When we write Shell scripts, the first line to be #!/bin/bashdeclared Shell interpreter, after writing to give Shell permission to execute, then you can perform the operation.

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11601016.html