写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
参考脚本:
{{{密码回复可见}}}
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
参考脚本:
{{{密码回复可见}}}
0
bin/sh
read -p "pleas inside scrpert" a
sh -n $a 2>4.txt
if [ $? != 0 ]
then
echo "sh -n $a"
cat 4.txt
else
echo "the scper is ok"
read -p "pleas inside scrpert" a
sh -n $a 2>4.txt
if [ $? != 0 ]
then
echo "sh -n $a"
cat 4.txt
else
echo "the scper is ok"
0
#!/bin/bash
sh -n $1 2>/tmp/err
if [ $? -eq "0" ]
then
echo "The script is OK."
else
cat /tmp/err
read -p "Please inpupt Q/q to exit, or others to edit it by vim. " n
if [ $n == "q" -o $n == "Q" ]
then
echo
exit
else
vim $1
fi
fi
sh -n $1 2>/tmp/err
if [ $? -eq "0" ]
then
echo "The script is OK."
else
cat /tmp/err
read -p "Please inpupt Q/q to exit, or others to edit it by vim. " n
if [ $n == "q" -o $n == "Q" ]
then
echo
exit
else
vim $1
fi
fi
0
#!/bin/bash
read -p "请输入要检查的脚本路径:" aa
script_dir=$aa
sh -n $script_dir 2>/root/debug.txt
if [ $? -ne 0 ]
then
echo "脚本语法错误是: `cat /root/debug.txt`"
else
echo "正"
fi
read -p "请输入要检查的脚本路径:" aa
script_dir=$aa
sh -n $script_dir 2>/root/debug.txt
if [ $? -ne 0 ]
then
echo "脚本语法错误是: `cat /root/debug.txt`"
else
echo "正"
fi
0
#!/bin/bash
read -p "please input the shell name": file1
touch 22.txt
sh -n $file1 2> 22.txt
#cat 22.txt
if [ -s 22.txt ]
then
/bin/cat 22.txt
read -p "please input Q or q to exit shell": a
# if [ "$a" = "Q" -o "$a" = "q" ] 这种写法和下面这种写法都可以 实现多条件IF
if [ "$a" = "Q" ] || [ "$a" = "q" ]
then
exit
else
vim $file1
fi
else
echo "$file1 is ok"
fi
read -p "please input the shell name": file1
touch 22.txt
sh -n $file1 2> 22.txt
#cat 22.txt
if [ -s 22.txt ]
then
/bin/cat 22.txt
read -p "please input Q or q to exit shell": a
# if [ "$a" = "Q" -o "$a" = "q" ] 这种写法和下面这种写法都可以 实现多条件IF
if [ "$a" = "Q" ] || [ "$a" = "q" ]
then
exit
else
vim $file1
fi
else
echo "$file1 is ok"
fi
0
本帖最后由 xueyongbo 于 2015-12-6 19:57 编辑
- #!/bin/bash
- # By xueyongbo
- # 2015/12/6 19:00
- read -p "please input yours shell scripts_dir:" scripts_dir
- cd `dirname $scripts_dir`
- while [ -f `basename $scripts_dir` ]
- do
- sh -n `basename $scripts_dir`
- read -p 'input "q" or "Q" exit or input other keys to edit the sctipt:' comm
- case "$comm" in
- "q" | "Q")
- exit
- ;;
- *)
- vim $scripts_dir
- exit
- ;;
- esac
- done
0
本帖最后由 qiaoxin360 于 2015-12-17 14:01 编辑
- #!/bin/bash
- read -p "where is your shell file:" n
- if [ -f $n ];then
- sh -n $n
- if [ $? != 0 ];then
- read -p "input q or Q to exit;input other to use VIM:" m
- if [ $m == q || $m == Q ];then
- exit
- else
- vim $n
- fi
- else
- echo "sh -n check OK!"
- fi
- else
- echo "error!no such file or not a file!"
- fi
0
本帖最后由 zql254 于 2016-1-5 22:46 编辑
- sh -n $1 2> /tmp/e
- if [ $? -eq 0 ];then
- echo "The script is OK !"
- else
- cat /tmp/e
- read -p "input q or Q:" n
- if [ $n = q ]||[ $n = Q ];then
- exit
- else
- vim $1
- fi
- fi
0
#!/bin/bash
# to test the shell script true or false
if [[ $# -ne 1 ]];then
echo "usage: $0 script_file"
else
sh -n $1
fi
if [[ $? -ne 0 ]];then
read -p "please enter the q or Q to exit the $1, or enter any other to v im the $1:" choice
case $choice in
q)
;;
Q)
;;
*)
vim $1
esac
else
echo "the script of $1 is okay"
fi
# to test the shell script true or false
if [[ $# -ne 1 ]];then
echo "usage: $0 script_file"
else
sh -n $1
fi
if [[ $? -ne 0 ]];then
read -p "please enter the q or Q to exit the $1, or enter any other to v im the $1:" choice
case $choice in
q)
;;
Q)
;;
*)
vim $1
esac
else
echo "the script of $1 is okay"
fi
0
本帖最后由 syk 于 2016-1-26 16:00 编辑
#!/bin/bash
vim + $1
until bash -n $1 &> /dev/null; do
read -p "error, q|Q for quit,others for vim $1: " opt
case $opt in
q|Q)
echo "quit."
exit 8
;;
*)
vim + $1
;;
esac
done
chmod +x $1
#!/bin/bash
vim + $1
until bash -n $1 &> /dev/null; do
read -p "error, q|Q for quit,others for vim $1: " opt
case $opt in
q|Q)
echo "quit."
exit 8
;;
*)
vim + $1
;;
esac
done
chmod +x $1
0
本帖最后由 lyhabc 于 2016-1-27 18:06 编辑
写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
vi check.sh
#!/bin/bash
sh -n $1 2>/tmp/err
if [ $? == 0 ]
then
echo "The script is OK."
else
cat /tmp/err
read -p "pls input q exit the script or other to vi the script: " n
if [ $n == q ]
then
exit
elif [ ! -z $n ]
then
vi $1
fi
fi
写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。
提醒: 检查shell脚本有没有语法错误的命令是 sh -n xxx.sh
vi check.sh
#!/bin/bash
sh -n $1 2>/tmp/err
if [ $? == 0 ]
then
echo "The script is OK."
else
cat /tmp/err
read -p "pls input q exit the script or other to vi the script: " n
if [ $n == q ]
then
exit
elif [ ! -z $n ]
then
vi $1
fi
fi
0
#!/bin/bash
read -p "please input file :" a
if ! [ -e $a ];then
echo "qing shuru youxiao lujing"
exit 5
else
bash -n $a
fi
read -p "input q or Q :" b
if [ "$b" == "Q" ];then
exit 100
elif [ "$b" == "q" ];then
exit 101
else
vi $a
fi
read -p "please input file :" a
if ! [ -e $a ];then
echo "qing shuru youxiao lujing"
exit 5
else
bash -n $a
fi
read -p "input q or Q :" b
if [ "$b" == "Q" ];then
exit 100
elif [ "$b" == "q" ];then
exit 101
else
vi $a
fi
0
- #!/bin/bash
- # 2016-01-28 23:08:58
- bash -n "$1" 2>/tmp/err
- result=`grep error /tmp/err |wc -l`
- if [ $result -lt 1 ]
- then
- exit
- fi
- while :
- do
- read -p "Input 'q' or 'Q' to quit :" choose
- if [ "$choose" == "Q" ] || [ "$choose" == "q" ]
- then
- exit
- else
- vim "$1"
- fi
- done
0
本帖最后由 HMOM 于 2016-3-20 19:11 编辑
#!/bin/bash
/bin/bash -n $1 2>/tmp/err
if [ $? -eq 0 ];then
echo "The script maybe is OK."
else
cat /tmp/err
read -p "The script has syntax errors. Enter[y] edit it [y]?" y
fi
case $y in
y)
vim $1 ;;
*)
exit
esac
#!/bin/bash
/bin/bash -n $1 2>/tmp/err
if [ $? -eq 0 ];then
echo "The script maybe is OK."
else
cat /tmp/err
read -p "The script has syntax errors. Enter[y] edit it [y]?" y
fi
case $y in
y)
vim $1 ;;
*)
exit
esac
0
read -p '输入脚本' i
sh -n $i 2>1.txt
if [ $? -nq "0" ]
then
cat 1.txt
read -p "输入q或Q退出" y
if [ $y =="q"||$y=="Q" ]
then
exit
else
vim $i
fi
fi
echo "ok"
sh -n $i 2>1.txt
if [ $? -nq "0" ]
then
cat 1.txt
read -p "输入q或Q退出" y
if [ $y =="q"||$y=="Q" ]
then
exit
else
vim $i
fi
fi
echo "ok"
0
#!/bin/bash
FILE=$1
[ $# -ne 1 ] && echo "Usage 10.sh sh_file" && exit
bash -n $FILE 2>/dev/null
if [ $? -ne 0 ] ; then
echo -e "\033[31mThere is an error:=================\033[0m"
bash -n $FILE
echo -e "\033[31m=======================\033[0m" #\033[31m用来显示红色
read -p "Please input your choice,q or Q to quit , others to edit the script ==>"
case $REPLY in
q|Q) exit ;;
*) vi $FILE
esac
else
echo -e "\033[32m$FILE syntax is ok!\033[0m"
fi
FILE=$1
[ $# -ne 1 ] && echo "Usage 10.sh sh_file" && exit
bash -n $FILE 2>/dev/null
if [ $? -ne 0 ] ; then
echo -e "\033[31mThere is an error:=================\033[0m"
bash -n $FILE
echo -e "\033[31m=======================\033[0m" #\033[31m用来显示红色
read -p "Please input your choice,q or Q to quit , others to edit the script ==>"
case $REPLY in
q|Q) exit ;;
*) vi $FILE
esac
else
echo -e "\033[32m$FILE syntax is ok!\033[0m"
fi
编辑回复