shell script: Syntax error: Bad for loop variable error Solution (rpm)

When Linux Mint write a simple shell scripts, calcd + 1 + 2 + 3 ...... 100 using for..do..done structure, a result of executing "sh -n xxx.sh" syntax detection always being given, but can be run properly on a PC;
script:
#!/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
Error as follows:
Syntax error: Bad for loop variable
 Analysis: Beginning ubuntu 6.10, ubuntu will replace the previous default bash shell became the dash shell; its performance for the / bin / sh link down / bin / dash rather than the traditional / bin / bash.
allen@allen-lql ~/workspace/script $ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 12 14:29 /bin/sh -> dash
So when using sh perform the detection of the actual use of dash, and dash are not supported for this loop written in C language format.
Solution: 1, change the default shell to bash. (Bash supports C language format for circulation)
sudo dpkg-reconfigure dash

In the options selected No

2. Such execution no problem

bash test.sh

3. That if we want to perform in such a way sh test.sh, how to do it?

Modify the code.

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

This time, you then execute sh test.sh, it will not report the error.

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lh03061238/p/11106581.html