Bash Syntax Error Bad For Loop Variable
Contents |
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct syntax error bad for loop variable shell Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu
Bash Syntax Error Near Unexpected Token Done'
Official Documentation User Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: bash syntax error near unexpected token else' Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk bash syntax error near unexpected token fi' script.sh: 1: Syntax error: Bad for loop variable Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 7 of 7 Thread: script.sh: 1: Syntax error: Bad for loop variable Thread Tools Show Printable Version Subscribe to this
Bash Syntax Error Invalid Arithmetic Operator
Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode August 16th, 2009 #1 colau View Profile View Forum Posts Private Message Dark Roasted Ubuntu Join Date Apr 2009 Location Dhaka,Bangladesh Beans 1,010 DistroUbuntu 10.04 Lucid Lynx script.sh: 1: Syntax error: Bad for loop variable Code: for (( i = 0; i <= 4; i++ )) do echo $i done Adv Reply August 16th, 2009 #2 unutbu View Profile View Forum Posts Extra Roomy Joe Join Date Mar 2008 Beans 4,715 DistroUbuntu 9.10 Karmic Koala Re: script.sh: 1: Syntax error: Bad for loop variable /bin/sh is a symlink to dash, not bash. Code: (( ... )) is a bash-ism, not recognized by dash. So try either PHP Code: Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; https://ubuntuforums.org/showthread.php?t=1241407 it only takes a minute: Sign up Syntax error: Bad for loop variable up vote 3 down vote favorite 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: http://stackoverflow.com/questions/30358065/syntax-error-bad-for-loop-variable alarmclock-vol.sh: Syntax error: Bad for loop variable bash sh share|improve this question asked May 20 '15 at 18:51 Kwiatkowski 4217 6 Because sh isn't bash. for (( … )) is not available in sh. –kojiro May 20 '15 at 18:53 @kojiro: sh may or may not be bash; on some systems, /bin/sh is a symlink to /bin/bash, and the above script may work. In any case, you certainly shouldn't assume that it is. –Keith Thompson May 20 '15 at 19:14 1 @KeithThompson, though even if sh is a symlink to bash, bash behaves differently when invoked as sh (posix mode enabled). Therefore, even when sh is bash, "sh is not bash" still applies. –geirha May 20 '15 at 19:40 @geirha: On my Debian 6 system, /bin/sh is a symlink to /bin/bash, and the for (( ... )) syntax works in a script with #!/bin/sh; with #!/bin/dash it gives me "Syntax error: Bad for loop variable". (It's bash 4.1.5 if that matters.) –Keith Thompson May 20 '15 at 19:47 @KeithThomp here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/22863783/bash-script-cant-get-for-loop-working or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http://www.unix.com/shell-programming-and-scripting/135947-syntax-error-bad-loop-variable.html Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bash script - can't get for loop working up vote 3 down vote favorite Background Info: I'm trying to follow the example posted here: http://www.cyberciti.biz/faq/bash-for-loop/ I would like loop 9 times using a syntax error control variable called "i". Problem Description My code looks like this: for i in {0..8..1} do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done When I run this code, the debug prints show me: "i is {0..8..1}" instead of being a value between 0 and 8. What I've Checked So Far: I've tried to check my version of bash to make sure it supports this type of syntax. I'm running version 4,2,25(1) I also tried bash syntax error using C like syntax where you do for (i=0;i<=8;i++) but that doesn't work either. Any suggestions would be appreciated. Thanks. EDIT 1 I've also tried the following code: for i in {0..8}; do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done And... for i in {0..8} do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done They all fail with the same results. I also tried: #!/bin/bash for ((i=0;i<9;i++)); do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done And that gives me the error: test.sh: 4: test.sh: Syntax error: Bad for loop variable FYI. I'm running on ubuntu 12 EDIT 2 Ok... so i think Weberick tipped me off to the issue... To execute the script, I was running "sh test.sh" when in the code I had defined it as a BASH script! My bad! But here's the thing. Ultimately, I need it to work in both bash and sh. so now that I'm being careful to make sure that I invoke the script the right way... I've noticed the following results: when defined as a bash script and i execute using bash, the C-style version works! when defined as an sh script and i execute using sh, the C-style version fails me@devbox:~/tmp/test$ sh test.sh test.sh: 5: test.sh: Syntax error: Bad for loop variable when defined as an sh script and i execute usi Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here. Search Forums Show Threads Show Posts Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... unix and linux commands - unix shell scripting Syntax error: Bad for loop variable Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 05-07-2010 dman Registered User Join Date: May 2010 Last Activity: 7 February 2011, 8:37 AM EST Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Syntax error: Bad for loop variable Hi Can any one help, I'm trying to run a script that beeps out the ip address from the PC internal speaker with the following script. It keeps throwing the error "Syntax error: Bad for loop variable" on line 16. I know its picking up the IP ADDRESS correctly. Any ideas on whats wrong. I'm running this on a ubuntu box. Thanks. Code: #!/bin/sh # Beep ip address trough internal pc speaker # Tested on Debian Linux @NSLU2 # Author: Sebastiaan Giebels, 2007 # Retreive dot-separated ipv4 address from the ifconfig information, using grep and cut: IPADDRESS=`/sbin/ifconfig | grep -A1 "eth0" | grep "inet" | cut -d: -f 2 |cut -d" " -f1` # Enable next line for debugging: #echo $IPADDRESS #Uncomment this for an example with zeroes in the ip address: #IPADDRESS="10.0.0.1" # For all characters in dotted ipv4 address: for ((a=0; a < ${#IPADDRESS} ; a++)) do # Get the next character to process: NUMBER=${IPADDRESS:$a:1} if [ "." == "$NUMBER" ] then # Different note to signal a '.' in the address: `/usr/bin/beep -l 20 -f 25 -d 20` else if [[ "$NUMBER" -ge "0" && "$NUMBER" -le "9" ]] then # Beep 'NUMBER' times, except for 0 (=beep 10 times): if [ "$NUMBER" == "0" ] then NUMBER=10 fi `/usr/bin/beep -l 45 -f 15 -d 150 -r $NUMBER` fi fi sleep 1 done Remove advertisements Sponsored Links dman View Public Profile Find all posts by dman #2 05-08-2010 dunkar70 Registered User Join Date: Mar 2 #!/bin/bash
or PHP Code:
for((i=0;i<=4;i++))
do
echo$i
done
Bash Syntax Error Near Unexpected Token Newline'