Bash script programming learning note 05: user interaction and script debugging

Keywords: Linux

User interaction

In learning note 04, we mentioned the location parameter, which is a way to pass the parameter to the script. Another way is the read command.

[root@c7-server ~]# read name
alongdidi
[root@c7-server ~]# echo $name
alongdidi

The read command can read data from STDIN and store it in the variable specified by the user.

It can be assigned to multiple variables.

[root@c7-server ~]# read a b
tom jerry
[root@c7-server ~]# echo $a $b
tom jerry

When the number of variables is more than the number of values, the redundant variable value is empty.

[root@c7-server ~]# read a b c
tom jerry
[root@c7-server ~]# echo $a
tom
[root@c7-server ~]# echo $b
jerry
[root@c7-server ~]# echo $c

[root@c7-server ~]#

When the number of values is more than the number of variables, all the redundant values will be assigned to the last variable.

[root@c7-server ~]# read a b
tom jerry mike
[root@c7-server ~]# echo $a
tom
[root@c7-server ~]# echo $b
jerry mike

The reminder information can be realized through the - p option, which makes the whole user interaction more human-oriented.

[root@c7-server ~]# read -p "Please enter your name:" name
Please enter your name:alongdidi
[root@c7-server ~]# echo $name
alongdidi

If the user doesn't type, the read command stays there until the user types or breaks by typing Ctrl+c. You can use the - t option to specify a timeout duration (in seconds), which will make it more secure, similar to the timeout mechanism of website login credentials.

[root@c7-server ~]# read -p "Please enter your name:" name
Please enter your name:^C
[root@c7-server ~]# read -t 3 -p "Please enter your name:" name
Please enter your name:[root@c7-server ~]# 
[root@c7-server ~]#

 

Script debugging

We can use two options to debug the script.

# bash -n test.sh
# bash -x test.sh

In fact, these two options cannot be found in man bash, because they do not belong to bash's options, but to set. Some of set's single character options (short options) can be used by bash, - n and - x are two of them.

-n: Similar to syntax checking. If the script syntax is OK, there will be no errors. Exit status code is 0.

[root@c7-server ~]# cat test.sh 
#!/bin/bash
if id zwl &> /dev/null; then
    echo "User zwl exists."
else
    echo "User zwl doesn't exists."
fi
[root@c7-server ~]# bash -n test.sh
[root@c7-server ~]# echo $?
0

If we comment out fi, we break the structure of if statement. Error will be reported.

[root@c7-server ~]# cat test.sh 
#!/bin/bash
if id zwl &> /dev/null; then
    echo "User zwl exists."
else
    echo "User zwl doesn't exists."
#fi
[root@c7-server ~]# bash -n test.sh 
test.sh: line 7: syntax error: unexpected end of file
[root@c7-server ~]# echo $?
2

But the ability of syntax detection itself is very weak, for example, we will modify else. At this time, the syntax check does not report an error.

Because it understands the original two branch if structure as a single branch, and elsealongdi as a command, and does not query whether the command exists.

[root@c7-server ~]# cat test.sh 
#!/bin/bash
if id zwl &> /dev/null; then
    echo "User zwl exists."
elsealongdidi
    echo "User zwl doesn't exists."
fi
[root@c7-server ~]# bash -n test.sh 
[root@c7-server ~]# echo $?
0

So in this case, even if syntax detection is OK, the script will still report an error when executing.

[root@c7-server ~]# bash test.sh 
User zwl exists.
test.sh: line 4: elsealongdidi: command not found
User zwl doesn't exists.

Therefore, the syntax detection is only to ensure whether the script can be executed. As for any error in the execution process, it doesn't matter as long as it doesn't cause the script to continue executing.

-x: Track script execution. It can be used to understand which branch a script takes when it encounters a conditional structure. When encountering the cyclic structure, you can also see the cyclic structure several times.

[root@c7-server ~]# cat test.sh
#!/bin/bash
if id zwl &> /dev/null; then
    echo "User zwl exists."
else
    echo "User zwl doesn't exists."
fi
[root@c7-server ~]# bash -x test.sh
+ id zwl
+ echo 'User zwl exists.'
User zwl exists.

Posted by Mirge on Wed, 08 Jan 2020 01:22:23 -0800