学习 Shell Scripts
1、第一支 script 的撰写与执行
现在我们假设你写的这个程序文件名是 /home/dmtsai/shell.sh,那么如何执行这个文件呢?
- 直接指令下达:shell.sh 文件必须要具备可读与可执行(rx)的权限,然后:
- 绝对路径:使用 /home/dmtsai/shell.sh 来下达指令;
- 相对路径:假设工作目录在 /home/dmtsai,则使用 ./shell.sh 来执行;
- 变量 “PATH” 的功能:将 shell.sh 放在 PATH 指定的目录内,例如:~/bin/
- 以 bash 程序来执行:通过 “bash shell.sh” 或 “sh shell.sh” 来执行
#!/bin/bash
#Program:
# This program shows "Hello World!" in your screen.
#History
#2021/09/02 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
echo -e "Hello World! \a \n"
exit 0
-
第一行 #!/bin/bash 在宣告这个 script 使用的 shell 名称:
因为我们使用的是 bash,所以,必须要以 “#!/bin/bash” 来宣告这个文件内的语法使用 bash 的语法。那么当这个程序被执行时,他就能加载 bash 的相关环境配置文件(一般来说就是 non-login shell 的 ~/.bashrc)并且执行 bash 来使我们底下的指令能够执行。
-
程序内容说明
建议添加说明:1、内容与功能;2、版本信息;3、作者与联系方式;4、文件日期;5、历史记录。
-
主要环境的宣告
PATH
-
主要程序部分
-
执行结果告知(定义回传值)
[root@MiWiFi-R4A-srv ~]# sh hello.sh
Hello World!
2、简单的 shell script 练习
2.1、简单范例
2.1.1、对谈式脚本:变量内容由用户决定
#!/bin/bash
#Program:
# User inputs his first name and last name. Program shows his full name.
#History
#2021/09/02 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
read -p "Please input your first name:" firstname
read -p "Please input your last name:" lastname
echo -e "\n Your full name is: ${firstname} ${lastname}"
2.1.2、随日期变化:利用 date 进行文件的建立
#!/bin/bash
#Program:
# Program creates three files, which named by user's input and date command.
#History
#2021/09/02 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1.让使用者输入文件名,并取得 fileuser 这个变量
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename:" fileuser
#2.为了避免使用者随意按 Enter,利用变量功能分析文件名是否有设定?
filename=${fileuser:-"filename"}
#3.开始利用 date 指令来取得所需要的文件名
date1=$(date --date='2 days ago' +%Y%m%d)
date2=$(date --date='1 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1=$(filename)$(date1)
file2=$(filename)$(date2)
file3=$(filename)$(date3)
#4.将文件建立
touch "$(file1)"
touch "$(file2)"
touch "$(file3)"
2.1.3、数值运算:简单的加减乘除
#!/bin/bash
#Program:
# User inputs 2 integer numbers; program will cross these two numbers.
#History
#2021/09/02 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo -e "You Should input 2 numbers, I will multiplying them! \n"
read -p "first number: " firstnu
read -p "second number: " secondnu
total=$((${first}*${secondnu}))
echo -e "\n The result of ${firstnu} * ${secondnu} is ==> ${total}"
2.1.4、通过 bc 计算 pi
#!/bin/bash
#Program:
# User inputs a scale number to calculate pi number.
#History
#2021/09/02 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo -e "This program will calculate pi value. \n"
echo -e "You should input a float number to calculate pi value. \n"
read -p "The scale number (10~10000) ?" checking
num=${checking:-"10"} #判断是否有输入数值
echo -e "Starting calculate pi value. Be patien."
time echo "scale=${num}; 4*a(1)" | bc -lp
2.2、script 的执行方式差异(source,sh script,./script)
2.2.1、利用直接执行的方式来执行 script
不论是直接指令下达,或者是利用 bash 来下达指令,该 script 都会使用一个新的 bash 环境来执行脚本内的指令!重点在于:当子程序完成后,在子程序内的各项变量或动作将会结束而不会传到父程序中!
如果使用 Windows 编辑的脚本,请使用:
sed -i ‘s/\r//’ 脚本名
[root@MiWiFi-R4A-srv bash]# echo ${firstname} ${lastname}
<== 确认了,这两个变量并不存在!
[root@MiWiFi-R4A-srv bash]# sh showname.sh
Please input your first name:VBird
Please input your last name:Tsai
Your full name is: VBird Tsai
[root@MiWiFi-R4A-srv bash]# echo ${firstname} ${lastname}
<== 事实上,这两个变量在父程序中的 bash 中还是不存在
[root@MiWiFi-R4A-srv bash]# chmod 744 showname.sh
[root@MiWiFi-R4A-srv bash]# ./showname.sh
Please input your first name:VBird
Please input your last name:Tsai
Your full name is: VBird Tsai
[root@MiWiFi-R4A-srv bash]# echo ${firstname} ${lastname}
<== 同样这两个变量在父程序中的 bash 中还是不存在
2.2.2、利用 source 来执行脚本:在父程序中执行
[root@MiWiFi-R4A-srv bash]# source showname.sh
Please input your first name:VBird
Please input your last name:Tsai
Your full name is: VBird Tsai
[root@MiWiFi-R4A-srv bash]# echo ${firstname} ${lastname}
VBird Tsai
3、善用判断式
3.1、利用 test 指令的测试功能
[root@MiWiFi-R4A-srv bash]# test -e /li
执行结果并不会显示任何信息,但最后我们可以通过 $? 或 && 及 || 来展示整个结果!
[root@MiWiFi-R4A-srv bash]# test -e /li && echo "exist" || echo "Not Exist"
Not Exist
[root@MiWiFi-R4A-srv bash]# test -e /home/li && echo "exist" || echo "Not Exist"
exist
测试的标志 | 代表意义 |
---|---|
1.关于某个文件名的 “文件类型” 判断 | |
-e | 该 “文件名” 是否存在? |
-f | 该 “文件名” 是否存在且为文件(file)? |
-d | 该 “文件名” 是否存在且为目录(directory)? |
-b | 该 “文件名” 是否存在且为一个 block device 设备? |
-c | 该 “文件名” 是否存在且为 character device 设备? |
-S | 该 “文件名” 是否存在且为 Socket 文件? |
-p | 该 “文件名” 是否存在且为一个 FIFO(pipe)文件? |
-L | 该 “文件名” 是否存在且为一个连结文件? |
2.关于文件的权限侦测 | |
-r | 侦测该文件名是否存在且具有 “可读” 的权限? |
-w | 侦测该文件名是否存在且具有 “可写” 的权限? |
-x | 侦测该文件名是否存在且具有 “可执行” 的权限? |
-u | 侦测该文件名是否存在且具有 “SUID” 的权限? |
-g | 侦测该文件名是否存在且具有 “SGID” 的权限? |
-k | 侦测该文件名是否存在且具有 “sticky bit” 的权限? |
-s | 侦测该文件名是否存在且为 “非空白文件”? |
3.两个文件的比较 | |
-nt | (newer than)判断 file1 是否比 file2 新 |
-ot | (older than)判断 file1 是否比 file2 旧 |
-ef | 判断 file1 与 file2 是否为同一个文件,可用在判断 hard link 上。主要意义在于判断两个文件是否均指向同一个 inode |
4.关于两个整数之间的判定 | |
-eq | 两数值相等(equal) |
-ne | 两数值不等(not equal) |
-gt | n1 大于 n2(greater than) |
-lt | n1 小于 n2(less than) |
-ge | n1 大于等于 n2(greater than or equal) |
-le | n1 小于等于 n2(less than or equal) |
5.判定字符串的数据 | |
test -z string | 判定字符串是否为 0?若 string 为空字符串,则为 true |
test -n string | 判定字符串是否非为 0?若 string 为空字符串,则为 false |
test str1 == str2 | 判定 str1 是否等于 str2,若相等,则返回 true |
test str1 != str2 | 判定 str1 是否不等于 str2,若相等,则返回 false |
6.多重条件判定 | |
-a | (and)两情况同时成立! |
-o | (or)两情况任何一个成立! |
! | 反相情况 |
#!/bin/bash
#Program:
# User input a filename, program will check the following:
# 1).exist?2).file/directory?3).file permissions.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1. 让使用者输入文件名,并且判断使用者是否真的有输入字符串?
echo -e "Please input a filename, I will check the filename's type and permission. \n\n"
read -p "Input a filename : " filename
test -z ${filename} && echo "You must input a filename." && exit 0
#2. 判断文件是否存在?若不存在则显示信息并结束脚本
test ! -e ${filename} && echo "The filename '${filename}' DO NOT exist" && exit 0
#3. 开始判断文件类型与属性
test -f ${filename} && filetype="regular file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm} writable"
test -x ${filename} && perm="${perm} executable"
#4. 开始输出信息
echo "The filename: ${filename} is a ${filetype}"
echo "And the permission for you are : ${perm}"
[root@MiWiFi-R4A-srv bash]# sed -i 's/\r//g' file_perm.sh
[root@MiWiFi-R4A-srv bash]# bash file_perm.sh
Please input a filename, I will check the filename's type and permission.
Input a filename : hello.sh
The filename: hello.sh is a regular file
And the permission for you are : readable writable
3.2、利用判断符号 [ ]
还可以利用判断符号 “[ ]” 来进行数据的判断!举例来说,如果我们想知道 ${HOME} 这个变量是否为空,可以这样做:
[root@MiWiFi-R4A-srv ~]# [ -z "${HOME}" ] ; echo $?
1
使用中括号必须要特别注意,因为中括号用在很多地方,包括通配符与正则表达式等等,所以如果要在 bash 的语法中使用中括号作为 shell 的判断式时,必须要注意中括号的两端需要有空格符来分隔。
- 在中括号 [] 内的每个组件都需要有空格键来分隔;
- 在中括号内的变量,最好都以双引号括号起来;
- 在中括号内的常量,最好都以单或双引号括号起来。
#!/bin/bash
#Program:
# This program shows the user's chioce.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
read -p "Please input (Y/N): " yn
[ "${yn}" == "Y" -o "${yn}" == "y" ] && echo "OK, continue" && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo "Oh, interrupt" && exit 0
echo "I don't know what your choice is" && exit 0
3.3、Shell script 的默认变量($0,$1…)
script 针对参数已经有设定好一些变量名称:
/path/to/scriptname opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
除了这些数字的变量之外,还有一些较为特殊的变量可以在 script 内使用来调用这些参数:
- $# : 代表后接的参数 “个数”,以上表为例这里显示为 “4”;
- $@ : 代表 “$1” “$2” “$3” “$4” 之意,每个变量都是独立的(用双引号括起来);
- $* : 代表 “$1c$2c$3c$4” ,其中 c 为分隔符,默认为空格键。
#!/bin/bash
#Program:
# Program shows the script name, parameters...
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo "The script name is ==> ${0}"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> ${1}"
echo "The 2nd parameter ==> ${2}"
[root@MiWiFi-R4A-srv bash]# sh how_paras.sh theone haha qout
The script name is ==> how_paras.sh
Total parameter number is ==> 3
Your whole parameter is ==> 'theone haha qout'
The 1st parameter ==> theone
The 2nd parameter ==> haha
3.3.1、shift:造成参数变量号码偏移
#!/bin/bash
#Program:
# Program shows the effect of the shift function.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift # 进行第一次 “一个变量的 shift”
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 # 进行第二次 “三个变量的 shift”
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
[root@MiWiFi-R4A-srv bash]# bash shift_paras.sh one two three four five six
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2
Your whole parameter is ==> 'five six'
4、条件判断式
4.1、利用 if…then
if [ 条件判断式 ]; then
当条件成立时,可以进行的指令工作内容;
fi
将多个条件写入一个中括号内的情况,还可以有多个中括号来隔开:
- && 代表 AND;
- || 代表 or;
if [ 条件判断式 ]; then
当条件成立时,可以进行的指令工作内容;
else
当条件不成立时,可以进行的指令工作内容;
fi
if [ 条件判断式一 ]; then
当条件一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件二成立时,可以进行的指令工作内容;
else
当条件一和二均不成立时,可以进行的指令工作内容;
fi
范例 1:
#!/bin/bash
#Program:
# This program shows the user's chioce.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
read -p "Please input (Y/N): " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK, continue"
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what your choice is"
fi
范例 2:
#!/bin/bash
#Program:
# Using netstat and grep to detect WWW,SSH,FTP and Mail services.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1.先做一些告知工作~
echo "Now, I will detect your Linux server's services"
echo -e "The www,ftp, ssh, and mail(smtp) will be detect! \n"
#2.开始进行一些测试工作,并且也输出一些信息
testfile=/dev/shm/netsta_checking.txt
netstat -tuln > ${testfile}
testing=$(grep ".80" ${testfile})
if [ "${testing}" != "" ]; then
echo "WWW is running in your system."
fi
testing=$(grep ".22" ${testfile})
if [ "${testing}" != "" ]; then
echo "WWW is running in your system."
fi
testing=$(grep ".21" ${testfile})
if [ "${testing}" != "" ]; then
echo "WWW is running in your system."
fi
testing=$(grep ".25" ${testfile})
if [ "${testing}" != "" ]; then
echo "WWW is running in your system."
fi
[root@MiWiFi-R4A-srv bash]# bash netstat.sh
Now, I will detect your Linux server's services
The www,ftp, ssh, and mail(smtp) will be detect!
WWW is running in your system.
4.2、利用 case…esac 判断
case $变量名称 in
"第一个变量内容")
程序段
;;
"第二个变量内容")
程序段
;;
*)
不包含第一个变量内容与第二个变量内容的其他程序执行段
exit 1
;;
easc
#!/bin/bash
#Program:
# This program shows the user's chioce.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
case ${1} in
"hello")
echo "Hello, how are you?"
;;
"")
echo "You must input parameters, ex> {${0} someword}"
;;
*)
echo "Usage ${0} {hello}"
;;
esac
[root@MiWiFi-R4A-srv bash]# bash hello-3.sh
You must input parameters, ex> {hello-3.sh someword}
[root@MiWiFi-R4A-srv bash]# bash hello-3.sh hello
Hello, how are you?
[root@MiWiFi-R4A-srv bash]# bash hello-3.sh li
Usage hello-3.sh {hello}
一般来说,使用 “case $变量 in” 这个语法中,当中的那个 “$变量” 大致有两种取得方式:
- 直接下达方式:例如上面提到的,利用 “script.sh variable” 的方式来直接给予 $1 这个变量的内容
- 交互式:通过 read 这个指令来让用户输入
#!/bin/bash
#Program:
# This program only accepts the following parameter: one, two or three.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo "This program will print your selection!"
read -p "Input your choice:" choice
case ${choice} in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[root@MiWiFi-R4A-srv bash]# bash show123.sh
This program will print your selection!
Input your choice:1
Usage show123.sh {one|two|three}
[root@MiWiFi-R4A-srv bash]# bash show123.sh
This program will print your selection!
Input your choice:one
Your choice is ONE
4.3、利用 function 功能
#!/bin/bash
#Program:
# This program only accepts the following parameter: one, two or three.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
function printit(){
echo -n "Your choice is " # 加上 -n 可以不断行继续在同一行显示
}
echo "This program will print your selection!"
case ${1} in
"one")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"two")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
"three")
printit; echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[root@MiWiFi-R4A-srv bash]# bash show123-2.sh
This program will print your selection!
Usage show123-2.sh {one|two|three}
[root@MiWiFi-R4A-srv bash]# bash show123-2.sh one
This program will print your selection!
Your choice is ONE
[root@MiWiFi-R4A-srv bash]# bash show123-2.sh two
This program will print your selection!
Your choice is TWO
function 也是拥有内建变量的~它的内建变量与 shell script 很类似,函数名称表示为 $0,而后续的变量也是以 $1,$2… 来取代的。
#!/bin/bash
#Program:
# This program only accepts the following parameter: one, two or three.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
function printit(){
echo "Your choice is ${1}"
}
echo "This program will print your selection!"
case ${1} in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
[root@MiWiFi-R4A-srv bash]# bash show123-3.sh
This program will print your selection!
Usage show123-3.sh {one|two|three}
[root@MiWiFi-R4A-srv bash]# bash show123-3.sh one
This program will print your selection!
Your choice is 1
[root@MiWiFi-R4A-srv bash]# bash show123-3.sh two
This program will print your selection!
Your choice is 2
5、循环(loop)
5.1、while do done,until do done(不定循环)
while [ condition ] #当条件成立时开始循环,直到 condition 不成立才停止
do
程序段落
done
until [ condition ] #刚好相反,当 condition 成立时停止循环
do
程序段落
done
#!/bin/bash
#Program:
# Please question until user input correct answer.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
while [ "${yn}" != "yes" -a "${yn}" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[root@MiWiFi-R4A-srv bash]# bash yes_to_stop.sh
Please input yes/YES to stop this program: jfdsaf
Please input yes/YES to stop this program: no
Please input yes/YES to stop this program: yes
OK! you input the correct answer.
如果换成 until 呢?
#!/bin/bash
#Program:
# Please question until user input correct answer.
#History
#2021/09/05 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
[root@MiWiFi-R4A-srv bash]# bash yes_to_stop-2.sh
Please input yes/YES to stop this program: no
Please input yes/YES to stop this program: dsfsdf
Please input yes/YES to stop this program: YES
OK! you input the correct answer.
如果我想计算 1+2+3+…+100 这个数据呢?
#!/bin/bash
#Program:
# Use loop to calculate "1+2+3+...+100".
#History
#2021/09/010 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
s=0
i=0
while [ "${i}" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
[root@MiWiFi-R4A-srv bash]# bash cal_1_100.sh
The result of '1+2+3+...+100' is ==> 5050
5.2、for…do…done(固定循环)
for 这种语法,则是 “已经知道要进行几次循环” 的状态,它的语法是:
for var in con1 con2 con3 ...
do
程序段
done
#!/bin/bash
#Program:
# Using for ... loop to print 3 animals.
#History
#2021/09/010 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
for animal in dog cat elephant
do
echo "There are ${animal}s ..."
done
[root@MiWiFi-R4A-srv bash]# bash show_animal.sh
There are dogs ...
There are cats ...
There are elephants ...
#!/bin/bash
#Program:
# Using for ... loop to print 3 animals.
#History
#2021/09/010 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
users=$(cut -d ':' -f1 /etc/passwd)
for username in ${users}
do
id ${username}
done
[root@MiWiFi-R4A-srv bash]# bash userid.sh
uid=0(root) gid=0(root) groups=0(root)
uid=1(bin) gid=1(bin) groups=1(bin)
uid=2(daemon) gid=2(daemon) groups=2(daemon)
...
5.3、for…do…done 的数值处理
for (( 初始值; 限制值; 执行步阶 ))
do
程序段
done
#!/bin/bash
#Program:
# Try do calculate 1+2+...+${input}.
#History
#2021/09/010 Li First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
read -p "Please input a number, I will count for 1+2+...+your_input: " nu
s=0
for (( i=1; i<=${nu}; i=i+1 ))
do
s=$((${s}+${i}))
done
echo "The result of '1+2+...+${nu}' is ==> ${s}"
[root@MiWiFi-R4A-srv bash]# bash cal_1_100-2.sh
Please input a number, I will count for 1+2+...+your_input: 1000
The result of '1+2+...+1000' is ==> 500500
6、shell script 的追踪与 debug
[root@MiWiFi-R4A-srv bash]# sh [-nvx] scripts.sh
选项与参数:
-n : 不要执行 script,仅查询语法的问题
-v : 再执行 script 前,先将 script 的内容输出到屏幕上
-x : 将使用到的 script 内容显示到屏幕上,这是很有用的参数!
范例一:测试 cal_1_100-2.sh 有无语法问题
[root@MiWiFi-R4A-srv bash]# sh -n cal_1_100-2.sh
# 若无语法问题,则不会显示任何信息!
范例二:将 cal_1_100-2.sh 的执行过程全部列出来
[root@MiWiFi-R4A-srv bash]# sh -x cal_1_100-2.sh
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
+ read -p 'Please input a number, I will count for 1+2+...+your_input: ' nu
Please input a number, I will count for 1+2+...+your_input: 10
+ s=0
+ (( i=1 ))
+ (( i<=10 ))
+ s=1
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=3
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=6
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=10
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=15
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=21
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=28
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=36
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=45
+ (( i=i+1 ))
+ (( i<=10 ))
+ s=55
+ (( i=i+1 ))
+ (( i<=10 ))
+ echo 'The result of '\''1+2+...+10'\'' is ==> 55'
The result of '1+2+...+10' is ==> 55
在输出信息中,在加号后面的数据起始都是指令串,由于 sh -x 的方式来将指令执行过程也显示出来,如此用户可以判断程序代码执行到哪一段时会出现相关的信息!