Kwiatkowski
Kwiatkowski

Reputation: 569

Syntax error: Bad for loop variable

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

Upvotes: 26

Views: 82737

Answers (4)

Aniruddha Ghosh
Aniruddha Ghosh

Reputation: 93

Another way you can achieve this is by giving the file execution permission. Below are the commands:

Instead of sh filename.sh

Do this:

chmod +x filename.sh
./filename.sh

You can see in this picture

for the same code that didn't run with sh but runs after giving execution permission.

The shell script that I used here is:

#!/bin/bash
for ((i = 1; i <= 10 ; i++)); 
do
  echo $i
done

Upvotes: 2

Muhammad Ahsan Mangi
Muhammad Ahsan Mangi

Reputation: 55

try using

#!/usr/bin/bash

instead of

#!/bin/bash

Upvotes: 3

Rishab
Rishab

Reputation: 67

use bash instead of sh

bash alarmclock-vol

Upvotes: 4

geirha
geirha

Reputation: 6181

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 

Upvotes: 42

Related Questions