【Linux】shell编程5(Shell脚本常见错误示例、Shell脚本调试技巧、shell脚本开发环境配置和优化、vim常用快捷操作、seq 序列化产生数字)

一、Shell脚本调试

1、Shell脚本常见错误示例

1.1 if条件语句缺少结尾关键字

1 #!/bin/bash
2 if [ 1 -lt 2 ];then
3 echo "Yes"
4
[root@node1 ~]# sh test.sh =====>执行之后报错如下
test.sh: line 4: syntax error: unexpected end of file
#第四行报语法错误,这不是所期待的文件结尾,发现缺少了fi

1.2 循环语句缺少关键字

1 #!/bin/bash
2 if [ 1 -lt 2 ];the <=========== 这里缺少一个n,应该为then
3 echo "Yes"
4 fi
[root@node1 ~]# sh test.sh
test.sh: line 4: syntax error near unexpected token `fi'
test.sh: line 4: `fi'
#报错第四行附近有错误

1.3 成对的符号落单

成对的符号有[],(),{},"",'',``,如果落单了,也会导致一些错误

1 #!/bin/bash
2 a="123
3 b="456"
4 c="a+b"
5 echo $c
[root@node1 ~]# sh test.sh
test.sh: line 4: unexpected EOF while looking for matching `"' <======提
示缺少了"号
test.sh: line 6: syntax error: unexpected end of file
报错是在4和6 最后发现是第二行缺少了",因为在第四行有调用第二行数据

1.4 方括号两端没空格

1 #!/bin/bash
2 if [1 -lt2 ];then
3 echo "YES"
4 fi
[root@node1 ~]# sh test.sh
test.sh: line 2: [1: command not found <======提示第二行[1有问题

2、Shell脚本调试技巧

2.1使用echo命令调试

#!/bin/bash
#create by nebula
#date:20190525
#dec:int compare
read -p "please input teo num: " a b
echo $a $b # 增加打印输出,确认变量值是否符合要求。
#exit #退出脚本
#if [ $a -lt $b ]
if (($a < $b))
then
    echo "$a < $b"
        elif [ $a -eq $b ]
        then
            echo "$a=$b"
else
echo "$a>$b"
fi
[root@nebulalinux03 ~]# . debug.sh
please input teo num: 99 100
99 100
99 < 100

2.2 使用bash命令参数调试

[root@nebulalinux03 ~]# bash [-nvx] script.sh
#-n:不会执行该脚本,仅查询脚本语法是否有问题,并给出错误提示
#-v:执行脚本时,先将脚本的内容输出到屏幕上,然后执行脚本,如果有错误,会给出错误提示
#-x:将执行的脚本的内容及输出显示到屏幕上,

二、shell脚本开发环境配置和优化

1、优化vim编辑配置

在这里我只给当前用户vim优化了配置;

配置完成后要保存退出,然后之前reboot命令重新加载;

vi /root/.vimrc

" ~/.vimrc
" vim config file
" date 2019-08-29
" Created by xiaoxing
"
""&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LKsTaRt~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值