Chinaunix

标题: 『请教』下面这段代码为什么报错8.txt: line 8: syntax error near unexpected token [打印本页]

作者: gtluck    时间: 2015-11-27 19:48
标题: 『请教』下面这段代码为什么报错8.txt: line 8: syntax error near unexpected token
本帖最后由 gtluck 于 2015-11-27 21:10 编辑

#/bin/bash
IPS="111111;
22222222;"
echo "------while test------"
echo $IPS | while read line
do
    echo $line
done


初次接触shell,求老司机指点。
作者: 扯淡的青春要扯淡对待    时间: 2015-11-27 20:08
没错啊,
------while test------
111111; 22222222;
作者: gtluck    时间: 2015-11-27 20:18
回复 2# 扯淡的青春要扯淡对待

谢谢你的答复。
[~/123]$ bash 8.txt
------while test------
8.txt: line 8: syntax error near unexpected token `done'
8.txt: line 8: `done '

上面是我这边运行后Terminal上显示的。
我也很纳闷,这很简单的几行代码。

   
作者: baby_神    时间: 2015-11-27 23:48
本帖最后由 baby_神 于 2015-11-27 23:49 编辑

没错啊

  1. [root@03 ~]# cat hehe.sh
  2. #/bin/bash
  3. IPS="111111;
  4. 22222222;"
  5. echo "------while test------"
  6. echo $IPS | while read line
  7. do
  8.     echo $line
  9. done
  10. [root@03 ~]# sh hehe.sh
  11. ------while test------
  12. 111111; 22222222;
复制代码

作者: Herowinter    时间: 2015-11-28 05:12
回复 1# gtluck


如果你的脚本是Windows格式的文件的话,因为line ending的问题是会有这个错。
我的cygwin环境下运行这个命令转换下就ok了。
  1. sed -i 's/\r/\n/' test.sh
复制代码

作者: Shell_HAT    时间: 2015-11-28 11:13
如果你的脚本是从Windows上传到Linux里面的,需要先转换格式再运行。
  1. dos2unix test.sh
  2. chmod u+x test.sh
  3. ./test.sh
复制代码

作者: gtluck    时间: 2015-12-03 23:22
回复 5# Herowinter

    已解决。确实是这个问题
    多谢多谢,老师傅。

   一句话让我少折腾很长时间。

    谢谢大家。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2