Use of shell scripts

1. Write a simple script

Commands for shell scripts work in two ways: interactive and batch.

➢ Interactive: Every time the user enters a command, it will be executed immediately.
➢ Batch (Batch): A complete Shell script is written by the user in advance, and the Shell will execute
many commands in the script at one time.

First introduce a simple shell script example ( although it is very small, but all large programs are piled up by small modules, programmers must know how to write a script ): just use the vim editor to follow the linux command A function that is placed sequentially in a file is a simple script.

[root@linuxprobe ~]# vim example.sh
#!/bin/bash
#For Example BY linuxprobe.com
pwd

ls -al

The file name in linux can be arbitrary, but to avoid people seeing ordinary files, just add the .sh suffix,

Follow the steps above to see the results.

[root@linuxprobe ~]# bash example.sh
/root/Desktop
total 8
drwxr-xr-x. 2 root root 23 Jul 23 17:31 .
dr-xr-x---. 14 root root 4096 Jul 23 17:31 ..


2. Can accept user parameters

Scripts like the above that only implement simple command control are not enough to reflect the power of linux. Shell scripts have long been built in to accept user arguments and can use space between variables. For example, $0 corresponds to the name of the current Shell script, $# corresponds to a total of several parameters, $* corresponds to the parameter values ​​of all positions, $? corresponds to the return value of the last command execution, and $1 , $2, $3... correspond to the parameter values ​​of the Nth position respectively, as shown below:




See the real effect by referencing the above parameters:

[root@linuxprobe ~]# vim example.sh
#!/bin/bash
echo "The current script name is $0"
echo "There are $# parameters in total, which are $*."
echo "The first parameter is $1, and the first parameter is $1. 5 is $5."
[root@linuxprobe ~]# sh example.sh one two three four five six
The current script is named example.sh
and has a total of 6 parameters, one two three four five six.
The first parameter is one, and the fifth is five.


Third, determine the parameters of the user

After learning this, we should enter a new height. Here we talk about judging the user's parameters. For example, as we learned earlier, the mkdir command is to judge whether the folder name specified by the user exists. If it exists, an error will be reported. Otherwise, it is automatically created. The conditional test syntax in the shell script can determine whether it is true. If it is true, it returns the number 0, otherwise it returns other random numbers.

Execution format of the test syntax: (ps: There is a space on both sides of the conditional expression.) Test statement format: [conditional expression]

According to the test object, the conditional test statement can be divided into 4 types:
➢ File test statement;
➢ Logical test statement;
➢ Integer value comparison statement;
➢ String comparison statement.

The file test is an operator that uses the specified conditions to determine whether the file exists or whether the permissions are satisfied. The specific parameters are shown in Table 4-3:


The following uses the file test statement to determine whether /etc/fstab is a directory type file, and then displays the return value after the execution of the previous command through the built-in $? variable of the Shell interpreter. If the return value is 0, the directory exists; if the return value is non-zero, it means the directory does not exist:

[root@linuxprobe ~]# [ -d /etc/fstab ]
[root@linuxprobe ~]# echo $?
1

Then use the file test statement to determine whether /etc/fstab is a general file. If the return value is 0, it means that the file exists and is a general file:

[root@linuxprobe ~]# [ -f /etc/fstab ]
[root@linuxprobe ~]# echo $?

0

The logic statement is used to logically analyze the test results, and different effects can be achieved according to the test results. For example, the operator symbol of logical "and" in the Shell
terminal is &&, which means that the command after the current command will be executed after it is successfully executed,
so it can be used to judge whether the /dev/cdrom file exists, and if so, output Exist typeface


[root@linuxprobe ~]# [ -e /dev/cdrom ] && echo "Exist"
Exist

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324587824&siteId=291194637
Recommended