syntax error: unexpected end of file in for loop - string

I have a very large script, and I'm trying to introduce a for loop.
To test that I have the right syntax, at line 120 say, I have:
for M in "1 2 3 4"; do
echo $M
end
However I get the error:
./script.sh: line 120: syntax error: unexpected end of file
Even more worryingly, I get the same if I run:
for M in $( ls ); do
echo $M
end
What am I doing wrong here?

You need to use done not end to finish a for loop

Related

Bash script : $x=$x+2 is not getting recognised

When I am executing the below script, I am getting the following error :-
The script executes infintely and below line is printed everytime.
"line 9: 1=1+2: command not found". Why?
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
$x=$x+2
done
echo "End Of Script 1"
exit 0
Also if I change the $x=$x+2 to x+$x+2 then also I am getting the below error.
line 6: [: 1+2: integer expression expected
Same script when executed like this runs fine.
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
let x=x+2
done
echo "End Of Script 1"
exit 0
You get line 9: 1=1+2: command not found because 1=1+2 is what $x=$x+2 is expanded into.
Use expr or let or ((...)) for integer calculations and bc for floating point:
let x=x+2
((x=x+2)) #same as above
((x+=2)) #same
((x++)) #if adding just one
((++x)) #if adding just one
x=$((x+2))
x=`expr $x + 2` #space before and after +
x=$(echo $x+2|bc) #using bc
x=$(echo $x+2.1|bc) #bc also works with floating points (numbers with decimals)
Since this part of the question isn't cleared yet, and not fine to post in a comment, I add this partial answer:
x=1; for i in 1 2 3 ; do x=$x+2; echo $x; done
1+2
1+2+2
1+2+2+2
As a side note: Don't use exit 0 at the end of your script without a good reason. When the script is done, it exits by itself without your help. The exit status will be the exit status of the last command performed, in your case a simple echo, which will almost always succeed. In the rare cases it fails, you will probably without intention hide that failure.
If you source the script, the exit will throw you out of your running shell.
But you can rewrite your while loop like this:
x=0
while (($((x)) < 9))
do
echo x : $x
x=$x+2
done
echo $((x))
x : 0
x : 0+2
x : 0+2+2
x : 0+2+2+2
x : 0+2+2+2+2
10
Because that's not the Bourne shell syntax for setting a variable; it looks more like Perl or PHP. The $ is used for parameter expansion and is not part of the variable name. Variable assignment simply uses =, and let evaluates arithmetic expressions (much like $((expression))). Another syntax that should work is x=$((x+2)). Note that these arithmetic evaluations are a bash feature; standard unix shells might require use of external tools such as expr.

I keep receiving this error message: del: line 13: syntax error: unexpected end of file

I keep receiving this error message:
del: line 13: syntax error: unexpected end of file
This is my script:
1 echo -e "Enter a filename"
2 read filename
3 if [$filename = myfirst]
4 then
5 echo -e "do you want to delete?"
6 read answer
7 if [answer= Y]
8 then rm myfirst
9 else [answer = N]
10 echo -e "file not deleted"
11 fi
12 exit0
Your if/fi syntax is not complete or closed (missing fi).
To detect such bugs prior to running your script you should always use
bash -n scriptname
This performs a syntax check detecting such problems without actually running the script.

Bash script increments variable, but throws error

I'm trying to process some files in increments of 50. It seems to work, but I'm getting an error that the command isn't found.
File sleepTest.sh:
#!/bash/bin
id=100
for i in {1..5}; do
$((id+=50))
sh argTest.sh "$id"
sleep 2
done
File argTest.sh:
#/bash/bin
id=$1
echo "processing $id..."
The output is
sleepTest.sh: line 6: 150: command not found
processing 150
sleepTest.sh: line 6: 200: command not found
processing 200
sleepTest.sh: line 6: 250: command not found
processing 250
sleepTest.sh: line 6: 300: command not found
processing 300
sleepTest.sh: line 6: 350: command not found
processing 350
So it clearly has an issue with how I'm incrementing $id, but it is still doing it. Why? And how can I increment $id. I tried simply $id+=50, but that did not work at all.
Leave out the $.
((id+=50))
((...)) performs arithmetic. $((...)) performs arithmetic and captures the result as a string. That would be fine if you did echo $((...)), but if you write just $((...)) then the shell treats that number as the name of a command to execute.
var=$((21 + 21)) # var=42
echo $((21 + 21)) # echo 42
$((21 + 21)) # execute the command `42`
Such assignments are legal inside arithmetic expressions. However, bash still tries to interpret the result of the expression as the name of a command. Either pass it as an argument to the : command (the POSIX way)
: $((id+=50))
or use a bash arithmetic statement instead of an arithmetic expression
((id+=50))

Bash function call error

I need some help in Bash function while passing arguments. I need to pass arguments into SQL query in Bash but I am getting error message.
#!/bin/bash --
whw='mysql -uroot -proot -habczx.net'
function foo() {
for v in "$#"; do
eval "$v"; # line 9
echo "${v}";
done
${whw} -e"select id, idCfg, idMfg, $DATE from tblMfg"
}
foo DATE="curdate()"
Below is the error message I get:
$ sh test4.sh
test4.sh: eval: line 9: syntax error near unexpected token `('
test4.sh: eval: line 9: `DATE=curdate()'
test4.sh: line 9: warning: syntax errors in . or eval will cause future versions of the shell to abort as Posix requires
DATE=curdate()
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from tblMfg limit 4' at line 1
==
If I change in function call to below I do not get any error message:
foo DATE="2014-12-21"
==
Any help?
Thanks!
The problem is the evaluated expression. You can see that by typing it:
$ DATE=bla()
-bash: syntax error near unexpected token `('
If you put it in quotes, it will work:
$ DATE="bla()"
In order to pass the quotes to the eval, they need to be protected from evaluation at callsite:
foo 'DATE="curdate()"'
should do the trick.
BTW: this is rather dangerous to eval a string, especially if the arguments are from untrusted users, one could use foo "rm -rf /" :)
I am not sure why you need the eval, instead of $DATE you could also use $1 and then
${whw} -e"SELECT ... $1...."
foo "curdate()"

Empty Body For Loop Linux Shell

Hi I want to write and empty body loop. I just want the loop counter to increment so I want the cpu to stay busy without any IO operation. Here is what I have written but it gives me an error:
#!/bin/bash
for (( i = 0 ; i <= 1000000; i++ ))
do
done
root#ubuntu:~# ./forLoop
./forLoop: line 4: syntax error near unexpected token `done'
./forLoop: line 4: `done'
You must specify at least one command in a loop body.
The best command for such a purposes is a colon :, commonly used as a no-op shell command.
You could put a no op command inside the loop like true or false (do nothing successfully or unsuccessfully respectively).
This will be a tight loop and will burn CPU. Unless you want to warm up your computer on a cold morning, you can simply say i=1000000 and have the same effect as the loop.
What is it that you're trying to achieve?
#!/bin/bash
let i=0
while [[ $i -le 1000000 ]]; do
let i++
done
You could use sleep x if you want to delay for x number of seconds.

Resources