虽然很简单,因为是第一次写,还是费了比较长的时间。
下面就将出错的地方标示出来,以提醒自己吧。
#!/bin/bash
echo "nexus verify.."
testing8081= `(netstat -an|grep 8081) ` -----这儿是`不是单引号',因为是可执行的脚本,所以必须是`号(就是数字键1左边,Tab键上面那个按键)
if [ "NULL$testing8081" = "NULL" ] ; then ------- shell中的相等判断是用“=”而不是“==” ,注意在“=”前后要留出空格,我就是在这儿卡了半天
echo "nuxus will be starting..."
/home/sdh/maven/Nexus/nexus-oss-webapp-1.9.0.2/bin/jsw/linux-x86-32/nexus start
else
echo "nexus had started.."
fi
附:单引号(')双引号(")及quote(`)的区别
单引号('):我的理解就是强引用了,在单引号中的都被解释为普通的字符
双引号("):应该就是弱引用了,双引号中的内容,如果为变量,则会继续解析变量值的
quote(`):是可执行命令,``中的字符是按可行性命令来解析的
如:
[sdh@localhost ~]$ name=sdh
[sdh@localhost ~]$ echo "$name"
sdh ----name作为变量来解析
[sdh@localhost ~]$ echo '$name'
$name ----直接将name变量解析为普通字符串
[sdh@localhost ~]$ echo `ls` -----ls是作为普通的命令来执行的
androidDev android_source bin eclipse.log hs_err_pid10060.log hs_err_pid3807.log hs_err_pid8651.log java maven MongoDB seamDev thunder workspace 公共的 模板 视频 图片 文档 下载 音乐 桌面
[sdh@localhost ~]$
而且一般来说,在一串命令当中,被``修饰的是最先执行的。
再一个就是=的赋值运算,在一般的shell中,如果是作赋值运算,则"="左右不能有空格,如果是做验证判断的话,则必须有空格。
改良版的内容
#!/bin/bash #this is a script foe nexus start,if the nexus is startting,return. echo "nexus verify.." testing8081=`(netstat -an|grep 8081)` if [ "NULL$testing8081" = "NULL" ] ; then echo "nuxus starting..." /home/sdh/maven/Nexus/nexus-oss-webapp-1.9.0.2/bin/jsw/linux-x86-32/nexus start else echo -n "nexus had started,do you need restart it?(Y/N):" read verify if [ "$verify" = "Y" ] ; then /home/sdh/maven/Nexus/nexus-oss-webapp-1.9.0.2/bin/jsw/linux-x86-32/nexus restart echo "nexus restared.." fi fi