关于shell 的一些调试问题

1,  如果你在shell脚本里看见了这样一句话:

set     -e

        这句话一般是写在shell脚本的开头第二句的,  就是说从 set    -e 之后出现的语句如果执行错误就是返回值为非0,  程序就直接退出。

2, 如果你在shell脚本里看见了这样一句话:

set    -x

 这句话的意思就是说在执行命令前会打印命令

例: filename :  hello_shell.sh

#!/usr/bin/env bash

set    -x

read   -p   "input  your   name: "  your_name

if  [ $your_name  -eq  "wanger" ];  then

echo   -e   "your name is $your_name!"

fi

如果你执行这句话就会在你的屏幕上打印这样的格式:

     + read   -p  "input your namew: "your_name

     + if   [];then

+echo -e  "yourname is $your_name"

     +fi

但是你可以在执行这个脚本前先可以检查他的语法有没有什么问题你可以这样做 sh  -n   hello_shell.sh

这样你就可以检查你的脚本语法的问题,如果有问题就会报错,  你还可以用 sh    -x   hello_shell.sh   来检查你的脚本运行了什么东西,但是不去执行你的脚本,这些都可以检查你的脚本的问题,很好用。

3,你经常用的for循环的几种表达方法:

#1;  for  i   in   1  2  3  4  5;   do

echo -e "for   this   is  $i!"

   don

#2;   for   i   in   $(seq 5); do

echo -e "for this is $i"

done

#3:   for ((i=1; i<=5;i++ )); do

echo -e "for this is $i"

done


注意这里有个小细节,就是#3的用法是2个()(),在一块使用的,不要忘了。

4,  shello  中echo的2中表示方法比较长用:

                 #1;   echo  -e    (表示打开反斜杠ESC转义)

#2;     echo -n    (表示不自动换行, shell默认是自动换行的)







猜你喜欢

转载自blog.csdn.net/qq_34765864/article/details/59154876