+ -
当前位置:首页 → 问答吧 → 请问如下脚本错在哪里?搞了老半天了。

请问如下脚本错在哪里?搞了老半天了。

时间:2011-03-02

来源:互联网

请问如下脚本错在哪里?搞了老半天了。请老大们指点啊!

脚本内容:
#!/bin/bash
ip=$1
if [ -s /tmp/a.txt ];then
case "$ip" in
"10.10.1.1")
  echo "IPaddress is 10.10.1.1" >> a.txt
  ;;
case "$ip" in
"10.10.2.1")
  echo "IPaddress is 10.10.2.1" >> a.txt
;;
esac
fi
exit

执行结果:
[root@RHEL54 tmp]# sh -x test.sh "10.10.1.1"
+ ip=10.10.1.1
test.sh: line 8: syntax error near unexpected token `"$ip"'
test.sh: line 8: `case "$ip" in '

[root@RHEL54 tmp]# sh -x test.sh "10.10.2.1"
+ ip=10.10.2.1
test.sh: line 8: syntax error near unexpected token `"$ip"'
test.sh: line 8: `case "$ip" in '
[root@RHEL54 tmp]#

作者: anderlee   发布时间: 2011-03-02

去掉 第8行

作者: chenbin200818   发布时间: 2011-03-02

case "$ip" in
两个 ?

作者: 南极雨   发布时间: 2011-03-02

#!/bin/bash
ip=$1
if [ -s /tmp/a.txt ];then
case "$ip" in
"10.10.1.1")
  echo "IPaddress is 10.10.1.1" >> a.txt
  ;;
esac
case "$ip" in
"10.10.2.1")
  echo "IPaddress is 10.10.2.1" >> a.txt
;;
esac
fi
exit

作者: gregorian   发布时间: 2011-03-02

我需要实现根据需输入的IP(也就是$1),作不同的操作,gregorian老兄的可以实现,感觉看着怪怪的,
两个case...esac。头脑迷糊了,清醒一点再看看。

作者: anderlee   发布时间: 2011-03-02

回复 anderlee


    2楼正解。

作者: where27   发布时间: 2011-03-02

都说了你去掉第8行  就是不信

作者: chenbin200818   发布时间: 2011-03-02

註釋了第八行的“case "$ip" in”即可。

作者: L_kernel   发布时间: 2011-03-02

热门下载

更多