写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
参考脚本:
{{{密码回复可见}}}
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
参考脚本:
{{{密码回复可见}}}
0
#!/bin/bash
sh -n $1 2> /tmp/$1_error.txt
if [ `echo $?` -ne 0 ]
then
echo `cat /tmp/$1_error.txt`
read -p "Please input 'q' or 'Q' to quit." n2
case $n2 in
q)
exit
;;
Q)
exit
;;
*)
vim $1
;;
esac
else
echo "test $1 ok!"
fi
sh -n $1 2> /tmp/$1_error.txt
if [ `echo $?` -ne 0 ]
then
echo `cat /tmp/$1_error.txt`
read -p "Please input 'q' or 'Q' to quit." n2
case $n2 in
q)
exit
;;
Q)
exit
;;
*)
vim $1
;;
esac
else
echo "test $1 ok!"
fi
0
本帖最后由 luckytodd 于 2016-6-26 16:04 编辑
#!/bin/bash
echo "这个是检查脚本语法的script"
sleep 2
if sh -n $1 >/dev/null 2>/tmp/$1.error
then
echo "没有明显的语法错误"
exit 0
else
cat /tmp/$1.error
read -p "Please input Q or q to exit script,others will open the script " x
if [ $x == "q" -o $x == "Q" ]
then
exit
elif [ $x != "Q" -o $x != "q" ]
then
vim $1
fi
fi
下面是执行结果
[root@aliyun sbin]# sh 2.sh while.sh
这个是检查脚本语法的script
while.sh: line 10: syntax error: unexpected end of file
Please input Q or q to exit script,others will open the script q
[root@aliyun sbin]# vim while.sh
[root@aliyun sbin]# vim 2.sh
[root@aliyun sbin]# sh 2.sh while.sh
这个是检查脚本语法的script
没有明显的语法错误
#!/bin/bash
echo "这个是检查脚本语法的script"
sleep 2
if sh -n $1 >/dev/null 2>/tmp/$1.error
then
echo "没有明显的语法错误"
exit 0
else
cat /tmp/$1.error
read -p "Please input Q or q to exit script,others will open the script " x
if [ $x == "q" -o $x == "Q" ]
then
exit
elif [ $x != "Q" -o $x != "q" ]
then
vim $1
fi
fi
下面是执行结果
[root@aliyun sbin]# sh 2.sh while.sh
这个是检查脚本语法的script
while.sh: line 10: syntax error: unexpected end of file
Please input Q or q to exit script,others will open the script q
[root@aliyun sbin]# vim while.sh
[root@aliyun sbin]# vim 2.sh
[root@aliyun sbin]# sh 2.sh while.sh
这个是检查脚本语法的script
没有明显的语法错误
0
#!/bin/bash
until bash -n $1 &>/dev/null
do
read -p "Please input Q or q to exit script,others will open the script " x
case $x in
Q|q)
quit
;;
*)
vim $1
;;
esac
done
until bash -n $1 &>/dev/null
do
read -p "Please input Q or q to exit script,others will open the script " x
case $x in
Q|q)
quit
;;
*)
vim $1
;;
esac
done
0
#!/bin/bash
sh=/usr/local/sbin/mysql.sh
a=`sh -n $sh`
if [ -n "$a" ]
then
sh -n $sh
fi
read -p "please input q or Q for exist: " n
case $n in
q)
exit
;;
Q)
exit
;;
*)
/usr/bin/vim $sh
esac
sh=/usr/local/sbin/mysql.sh
a=`sh -n $sh`
if [ -n "$a" ]
then
sh -n $sh
fi
read -p "please input q or Q for exist: " n
case $n in
q)
exit
;;
Q)
exit
;;
*)
/usr/bin/vim $sh
esac
编辑回复