Bash For Loop Syntax Error
Contents |
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 or posting ads with us Stack bash syntax error near unexpected token done' Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Bash Syntax Error Near Unexpected Token Else'
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error: Bad
Bash Syntax Error Near Unexpected Token Fi'
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
Bash Syntax Error Invalid Arithmetic Operator
But i have problem: alarmclock-vol.sh: 3: 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 bash syntax error near unexpected token then' '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 @KeithThompson, yes, some syntax still works (like for ((...)) in this case), some acts a little differently (the source and . builtins), while some syntax is disabled outright (like process substitution <(...) and >(...) ). Those are just examples that came to mind right now. –geirha May 20 '15 at 19:52 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted 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 share|improve this answer answered May 20 '15 at 18:54 geirha 2,6121021 work, thank you –Kwiatkowski May 20 '15 at 18:58 FYI, to change to bash, change your hashbang (the first line of your script) to #!
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 bash syntax error near unexpected token do' more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users bash syntax error near unexpected token newline' Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping bash syntax error unexpected end of file each other. Join them; it only takes a minute: Sign up Bash For Loop Syntax Error up vote 0 down vote favorite 1 I am trying to perform a simple for loop, but it keeps telling me there http://stackoverflow.com/questions/30358065/syntax-error-bad-for-loop-variable is a syntax error near do. I have tried to find some answers online, but nothing seems to be quite answering my question. The for loop is as so. All it wants to do is find the differences between two folders: #!/bin/bash for word in $LIST; do diff DIR1/config $word/config done exit The syntax error is near do. It says "Syntax error near unexpected token 'do '". $LIST is set outside of this script by the http://stackoverflow.com/questions/16987098/bash-for-loop-syntax-error program that calls it. Does anyone know what might be happening here? bash for-loop syntax-error share|improve this question edited Jun 7 '13 at 15:18 asked Jun 7 '13 at 14:55 user2463959 11 What does $LIST expand to? Also, can you give the exact error? –chepner Jun 7 '13 at 14:57 $LIST expands to a list like so: DIR2 DIR3 DIR4 –user2463959 Jun 7 '13 at 14:59 This doesn't contribute to the syntax error, but you need to prefix DIR1 (assuming it is a variable) and word with a dollar sign to get their value: diff "$DIR1/config" "$word/config". –chepner Jun 7 '13 at 14:59 Also, the syntax error is: Syntax error near unexpected token 'do' –user2463959 Jun 7 '13 at 15:00 1 This is not the code, the full code and nothing but the code. We need more information to debug this. –l0b0 Jun 7 '13 at 15:10 | show 1 more comment 1 Answer 1 active oldest votes up vote 2 down vote That's certainly valid syntax for bash so I'd be checking whether you may have special characters somewhere in the file, such as CR/LF at the ends of your lines. Assuming you're on a UNIXy system, od -xcb scriptname.sh should show you this. In addition, you probably also want to use $word rather than just word sin
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://askubuntu.com/questions/400936/loop-variable-error-in-for-loop and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges http://www.cyberciti.biz/faq/bash-for-loop/ Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: syntax error Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Loop variable error in for loop up vote 1 down vote favorite When I’m using for like for i in 1 2 3 4 5 then my file contains #!/bin/sh at the top. But when I’m using for(( i = 0; i<=5; i++)) bash syntax error then it is showing error Syntax error: Bad for loop variable and running properly when I remove shebang. Please tell me the reason behind this. scripts share|improve this question edited Jan 6 '14 at 11:58 Florian Diesch 46.7k12105131 asked Jan 6 '14 at 11:56 Gaurav Rai 814 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted for(( i = 0; i<=5; i++)) is Bash specific and doesn't work with plain Bourne shell (/bin/sh). If you remove the shebang the script is run by your current shell (which likely is Bash) so it works. Replace #!/bin/sh with #!/bin/bash to make the shebang work. share|improve this answer answered Jan 6 '14 at 12:05 Florian Diesch 46.7k12105131 Is there any way to do it in sh mode? Thanks! –Ziyaddin Sadigov Feb 26 '14 at 18:48 1 i=0; while [ $i -le 5 ]; do echo $i; i=$((i+1)); done –Florian Diesch Feb 26 '14 at 19:32 add a comment| up vote 0 down vote Try this out may be this could solve your problem #!/bin/bash j=0 for (( i
Ubuntu, FreeBSD, Linux, Solaris-Unix, Suse, Ubuntu Linux, UNIXHow do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression? A ‘for loop' is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.
for loop syntaxNumeric ranges for syntax is as follows:for VARIABLE in 1 2 3 4 5 .. N do command1 command2 commandN doneORfor VARIABLE in file1 file2 file3 do command1 on $VARIABLE command2 commandN doneORfor OUTPUT in $(Linux-Or-Unix-Command-Here) do command1 on $OUTPUT command2 on $OUTPUT commandN doneExamplesThis type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:#!/bin/bash for i in 1 2 3 4 5 do echo "Welcome $i times" doneSometimes you may need to set a step value (allowing one to count by two's or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:#!/bin/bash for i in {1..5} do echo "Welcome $i times" doneBash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:#!/bin/bash echo "Bash version ${BASH_VERSION}..." for i in {0..10..2} do echo "Welcome $i times" doneSample outputs:Bash version 4.0.33(0)-release... Welc