Linux Mint中写了一个简单的shell脚本,利用for..do..done结构计算1+2+3......+100的值,结果执行"sh -n xxx.sh"检测语法时总是报错,但在PC机上可正常运行;
脚本:
#!/bin/bash
#information PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH read -p "Please input a num " num
sum=0
for ((a=0; a<=$num; a++))
do
sum=$(($sum + $a))
done
echo "the sum is ==> $sum"
exit 0
错误如下:

Syntax error: Bad for loop variable
 分析: 从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell;其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash。

allen@allen-lql ~/workspace/script $ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 12 14:29 /bin/sh -> dash

所以在使用sh执行检测的时候实际使用的是dash,而dash不支持这种C语言格式的for循环写法。

解决办法: 1、将默认shell更改为bash。(bash支持C语言格式的for循环)

sudo dpkg-reconfigure dash

在选择项中选No

2.这样执行就没问题

bash test.sh

3.那如果我们只想用sh test.sh 这样的方式执行,怎么办呢?

修改一下代码.

for i in `seq 10`
do echo Good Morning ,this is $i shell program. done

这个时候,你再执行 sh test.sh,就不会报错误啦.

 
 

shell脚本:Syntax error: Bad for loop variable错误解决方法(转)的更多相关文章

  1. linux shell 报错 Syntax error: Bad for loop variable

    在linux下写了一个简单的shell,循环10次. test.sh #!/bin/bash ## ##循环10次 ## ; i<; i++)); do echo Good Morning ,t ...

  2. linux shell scripts:Syntax error: Bad for loop variable

    执行脚本报错 #!/bin/bash s=0 for (( i=1; i<=100; i++ )) do s=$(( $s + $i )) done echo $s sh add.sh 报错: ...

  3. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  4. 解决shell脚本“syntax error near unexpected token `fi'”的问题。

    执行shell脚本的时候,提示如下错误: 查询资料后发现: 执行: vi finddir.sh 然后,输入 :set ff 结果是: 解决方案就是,修改为unix: :set ff=unix 执行保存 ...

  5. Syntax error, insert "}" to complete ClassBody错误解决

    Syntax error, insert "}" to complete ClassBody 报该错误是因为我从网页上粘贴了别人的代码,并没有发现什么异常但还是编译器报红叉. 解决 ...

  6. Syntax error: Bad for loop variable解决办法

    从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell:其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash. ...

  7. DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016错误解决方法

    这个错误是:表处于"装入暂挂"状态. 经多次尝试 总结方法: 1:reorg table <表>: 假如不好使 则下面方法 2,先前尝试装入(LOAD)此表失败.表的状 ...

  8. Import error: no module named cv2 错误解决方法

    Windows: 将opencv安装目录下的cv2.pyd拷贝到Python安装目录里Lib中site-packages Linux: (1)将opencv安装目录下的cv2.so拷贝到Python安 ...

  9. gdb远程debug A syntax error in expression, near `variable)'.

    今天调试有个linux环境的应用时,gdb提示A syntax error in expression, near `variable)'.,最后经查,gdb版本过低(比如7.2)或者源代码不匹配所致 ...

随机推荐

  1. rrr

    # coding:utf-8 import pika username = 'feng' password = 'nihao' host = '192.168.1.144' credentials = ...

  2. UnicodeEncodeError: 'latin-1' codec can't encode characters,python3 中文乱码

    UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-13: ordinal not in range(2 ...

  3. JSON序列化和反序列化 对decmail 取值问题

    地图API返回经纬度:经度: 纬度: lng":114.03483089395202,"lat":22.615589046911805 decmail 接收数据后两位会截 ...

  4. Tomcat中加载不到项目 项目构建Deployment Assembly报错:The given project is not a virtual component project

    转:   The given project is not a virtual component project The given project is not a virtual compone ...

  5. sudo passwd root:没有相关指令

    在linux里如果想要修改密码,就输入这个指令,但是会出现没找到这个指令,是因为环境变量里没有passwd. 解决:查找passwd的位置,sudo find / -name passwd 然后进入p ...

  6. Robot Framework 读取控制面板安装的程序,判断某个程序是否已经安装

    wmic /output:D:\\DOAutomationTest\\automation_do_robotframework\\installList.txt product get name

  7. 使用Python快速实现简单的人脸检测

    最近有个比较要好的朋友问我能不能从监控视频里识别到从监控跟前经过的指定的人.因为他们单位的监控室经常要花大量的人力跟时间去找某个人在哪个位置出现过的证据.听起来像是一份比较有挑战性的任务,就答应他试试 ...

  8. Goroutines和线程对比

    目录 栈不同 调度不同 GOMAXPROCS Goroutine没有ID号 栈不同 线程:每一个OS线程都有一个固定大小的内存块(一般会是2MB)来做栈,这个栈会用来存储当前正在被调用或挂起(指在调用 ...

  9. springMvc之常用注解介绍

    @requestbody和@requestparam的用法  获取请求参数的方法 get请求: 直接获取request    如: public String getHtml(HttpServletR ...

  10. [jsp学习笔记] jsp过滤器