检查指定shell脚本是否有语法错误

回复 收藏
写一个shell脚本,检查指定的shell脚本是否有语法错误,若有错误,首先显示错误信息,然后提示用户输入q或者Q退出脚本,输入其他内容则直接用vim打开该shell脚本。

提醒: 检查shell脚本有没有语法错误的命令是  sh -n   xxx.sh

参考脚本:
{{{密码回复可见}}}

2015-11-18 15:36 举报
已邀请:
0

两天

赞同来自:

看看
0

zhangzihao

赞同来自:

123
0

llzdwyp

赞同来自:

看看
0

balich

赞同来自:

扩展
0

杭州小白

赞同来自:

面试题目看看
0

hlymlv

赞同来自:

学习
0

yanggang

赞同来自:

0

krven

赞同来自:

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"
0

wuhen

赞同来自:

看看。。。。
0

tutu

赞同来自:

查看
0

HwangChen

赞同来自:

look
0

googleqicq

赞同来自:

{:4_92:}
0

googleqicq

赞同来自:

#!/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
0

wzwyql

赞同来自:

学习
0

andreking

赞同来自:

学习下
0

415222090

赞同来自:

虽然还没学到 shell,先看看
0

Liemer_Lius

赞同来自:

芝麻开门///
0

汤小东

赞同来自:

learn
0

lky

赞同来自:

学习
0

xteplinux

赞同来自:

{:4_91:}
0

无限期等待

赞同来自:

shell
0

泡沫。

赞同来自:

ddddddddd
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
0

落、雨寒

赞同来自:

参考一下
0

huguihua2002

赞同来自:

看看看
0

xebszw

赞同来自:

学习
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
0

boy461205160

赞同来自:

正在学shell
0

yangjian319

赞同来自:

再看一眼。
0

echo

赞同来自:

看看
0

loveyouhyf

赞同来自:

aaaaaa
0

ldp840611

赞同来自:

看目的地
0

黄大伟

赞同来自:

看看
0

沈诚

赞同来自:

学习
0

追风者

赞同来自:

学习
0

jimmyliang

赞同来自:

学习一下
0

xueyongbo

赞同来自:

本帖最后由 xueyongbo 于 2015-12-6 19:57 编辑
  1. #!/bin/bash

  2. # By xueyongbo
  3. # 2015/12/6 19:00

  4. read -p "please input yours shell scripts_dir:" scripts_dir

  5. cd `dirname $scripts_dir`

  6. while [ -f `basename $scripts_dir` ]
  7. do
  8.         sh -n `basename $scripts_dir`
  9.         read -p 'input "q" or "Q" exit or input other keys to edit the sctipt:' comm
  10.         case "$comm" in
  11.                 "q" | "Q")
  12.                         exit
  13.                 ;;

  14.                 *)
  15.                         vim $scripts_dir
  16.                          exit
  17.                 ;;
  18.         esac

  19. done


0

minlyf

赞同来自:

答案
0

王焱峰

赞同来自:

1
0

猫腻

赞同来自:

扩展
0

猫腻

赞同来自:

{:4_92:}
0

圣手书生

赞同来自:

学习
0

拉卡

赞同来自:

学习了!
0

oneliang

赞同来自:

1
0

hzsnone

赞同来自:

look look
0

qiaoxin360

赞同来自:

本帖最后由 qiaoxin360 于 2015-12-17 14:01 编辑
  1. #!/bin/bash

  2. read -p "where is your shell file:" n
  3. if [ -f $n ];then
  4.     sh -n $n
  5.     if [ $? != 0 ];then
  6.     read -p "input q or Q to exit;input other to use VIM:" m
  7.         if [ $m == q || $m == Q ];then
  8.             exit
  9.         else
  10.             vim $n
  11.         fi
  12.     else
  13.         echo "sh -n check OK!"
  14.     fi
  15. else
  16.     echo "error!no such file or not a file!"
  17. fi

0

乌贼的帽子

赞同来自:

学习
0

jinm

赞同来自:

学习
0

我是学渣

赞同来自:

0

qiqige

赞同来自:

学习
0

weifeng1463

赞同来自:

ok
0

zql254

赞同来自:

本帖最后由 zql254 于 2016-1-5 22:46 编辑
  1. sh -n $1 2> /tmp/e
  2. if [ $? -eq 0 ];then
  3.     echo "The script is OK !"
  4. else
  5. cat /tmp/e
  6. read -p "input q or Q:" n
  7. if [ $n = q ]||[ $n = Q ];then
  8.     exit
  9. else
  10.     vim $1
  11. fi
  12. fi


0

Landon

赞同来自:

asd
0

inzaghidai

赞同来自:

学习
0

Burgess

赞同来自:

学习学习
0

licengceng

赞同来自:

学习
0

老咸菜

赞同来自:

xuexi
0

t0ny1988

赞同来自:

用if 写的 错误的时候OK,但是正确的时候发现无法自己退出
0

wildfire

赞同来自:

#!/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
0

faith

赞同来自:

..
0

lin19890913

赞同来自:

看看
0

syk

赞同来自:

本帖最后由 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
0

大漠之烟

赞同来自:

{:4_91:}
0

lyhabc

赞同来自:

本帖最后由 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
0

KICAZ629

赞同来自:

学习
0

zkq_315

赞同来自:

#!/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
0

miaojianbo

赞同来自:

学习
0

初秋飞马

赞同来自:

  1. #!/bin/bash
  2. # 2016-01-28 23:08:58
  3. bash -n "$1" 2>/tmp/err
  4. result=`grep error /tmp/err |wc -l`
  5. if [ $result -lt 1 ]
  6. then
  7.     exit
  8. fi
  9. while :
  10. do
  11. read -p "Input 'q' or 'Q' to quit :" choose
  12. if [ "$choose" == "Q" ] || [ "$choose" == "q" ]
  13. then
  14.     exit
  15. else
  16.     vim "$1"
  17. fi
  18. done
0

落涧飞鹰

赞同来自:

看看
0

乐橙306

赞同来自:

1
0

不怕不怕

赞同来自:

0

镂空的记忆

赞同来自:

学习
0

木头爱木头媳妇

赞同来自:

1
0

蔡炳森

赞同来自:

q
0

cxiaodian

赞同来自:

OK
0

大喵喵66

赞同来自:

看看
0

has

赞同来自:

看看
0

HMOM

赞同来自:

本帖最后由 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
0

dongteng

赞同来自:

学习


0

linux-小莫

赞同来自:

学习
0

vb3328998

赞同来自:

1
0

keluo

赞同来自:

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"
0

duyanbin

赞同来自:

#!/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
0

wangzai

赞同来自:

学习
0

贾刚

赞同来自:

1231213123
0

wsw13640218682

赞同来自:

学习一下
0

shoswj001

赞同来自:

?
0

andyaaa

赞同来自:

{:4_91:}
0

郭贞

赞同来自:

{:4_92:}
0

397705152

赞同来自:

本帖最后由 397705152 于 2016-4-22 16:13 编辑

#!/bin/bash
read -p "请输入一个脚本路径" a
sh -n $a
if [ $? -ne 0 ]
then
read -p "输入q或者Q退出,输入其他任意字符编辑该脚本" b
if [[ "$b" == "q" ]]||[[ "$b" == "Q" ]]
then
exit
else
vim $a
fi
else echo "脚本没有错误"
fi
0

we14578

赞同来自:

自己写的有点问题,看下老师的
0

we14578

赞同来自:

老师加了对空字符串的判断,学习了
0

阿凯

赞同来自:

看看
0

youlianqing

赞同来自:

学习
0

xufanyunwei

赞同来自:

学习
0

CNS2016

赞同来自:

学习
0

chumingjie

赞同来自:

学习
0

等风来

赞同来自:

学习
0

branttsai

赞同来自:

study,tks
0

mind_sky

赞同来自:

学习

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: