以例子的形式进行叙述,没有添加debug代码的初始脚本如下所示:
#!/bin/sh
#set -x
echo -n "Can you write device drivers? "
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow, you must be very skilled"
else
echo "Neither can I, I'm just an example shell script"
fi
注:简而言之,就是通过交互的方式输入Y,y 或者其它的字符,并将小写转为大写字母,然后打印出相应的语句。
这段脚本的输入结果如下:
$ ./example
Can you write device drivers? y
Wow, you must be very skilled
or
$ ./example
Can you write device drivers? n
Neither can I, Im just an example shell script
通过在代码中添加set -x 命令进行shell脚本跟踪,如上面红色部分所示。将#去掉,则执行脚本后输出结果如下:
$ ./example
+ echo -n 'Can you write device drivers? '
Can you write device drivers? + read answer
y
++ tr '[a-z]' '[A-Z]'
++ echo y
+ answer=Y
+ '[' Y = Y ']'
+ echo Wow, you must be very skilled
Wow, you must be very skilled