Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error: Bad for loop variable

Tags:

bash

sh

I'm trying to write a script that will vol up radio in the background

#!/bin/sh

for (( i = 80 ; i <= 101; i++ )) 
 do 
  amixer cset numid=1 i$% sleep 60;
done 

But i have problem:

alarmclock-vol.sh: 3: alarmclock-vol.sh: Syntax error: Bad for loop variable
like image 415
Kwiatkowski Avatar asked May 20 '15 18:05

Kwiatkowski


People also ask

What is !/ Bin bash?

#!/bin/bash The most common shebang is the one referring to the bash executable: #!/bin/bash. Essentially it tells your terminal that when you run the script it should use bash to execute it.

What is the difference between sh and bash?

bash is sh, but with more features and better syntax. Bash is “Bourne Again SHell”, and is an improvement of the sh (original Bourne shell). Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems.


3 Answers

The for (( expr ; expr ; expr )) syntax is not available in sh. Switch to bash or ksh93 if you want to use that syntax. Otherwise, the equivalent for sh is:

#!/bin/sh

i=80
while [ "$i" -le 101 ]; do
    amixer cset numid=1 "$i%"
    sleep 60
    i=$(( i + 1 ))
done 
like image 163
geirha Avatar answered Oct 14 '22 12:10

geirha


use bash instead of sh

bash alarmclock-vol
like image 27
Rishab Avatar answered Oct 14 '22 12:10

Rishab


try using

#!/usr/bin/bash

instead of

#!/bin/bash
like image 34
Muhammad Ahsan Mangi Avatar answered Oct 14 '22 13:10

Muhammad Ahsan Mangi