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

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

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

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

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

西瓜.M

赞同来自:

学习
0

xzzlamp

赞同来自:

11
0

timfeng3535

赞同来自:

ddd
0

nihao426181

赞同来自:

………………………………………………………………
0

Toornix

赞同来自:

重定向标准错误到文件,然后判断文件是否为空
0

菜菜鸟

赞同来自:

学习。。。
0

zzfeng2012

赞同来自:

{:4_106:}
0

Charley

赞同来自:

看看
0

17051036506

赞同来自:

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


0

luckytodd

赞同来自:

本帖最后由 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
没有明显的语法错误
0

陈龙

赞同来自:

看一看
0

jonnylin

赞同来自:

学习
0

hsm

赞同来自:

学习
0

不远晴空

赞同来自:

get
0

monga

赞同来自:

学习
0

xzzlamp

赞同来自:

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

Bullet_Point

赞同来自:

1
0

riverxyz

赞同来自:

学习
0

linuxcp

赞同来自:

{:4_91:}
0

标哥

赞同来自:

不会,看看答案
0

kw是id

赞同来自:

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

0

vanjle

赞同来自:

mak

0

大雁

赞同来自:


if [ -f $1 ]
then
  sh -n $1 2> 1.swq
  if [ $? -ne 0 ]
  then
    /bin/cat 1.swq
    read -p "please input a character(q|Q exit,others vim):" n
    if [ $n == "q" ] || [ $n == "Q" ]
    then
      exit 0
    else
      /usr/bin/vim $1
    fi
  else
    echo "the shell scripts is good."
  fi
else
  echo "$1 is not exist."
fi

0

王斌

赞同来自:

芝麻开门

0

杨银根

赞同来自:

学习

0

ZYT

赞同来自:

看看,学习下

0

小白啦啦啦

赞同来自:

学习

0

demo.sh

赞同来自:

hi

0

1881077

赞同来自:

看看

0

cc97

赞同来自:

学习

回复帖子,请先登录注册

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