This question already has answers here:
Bash syntax error: unexpected end of file
(21 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 7 years ago.
I tried almost all solutions suggested here but this very simple code of mine keeps showing this error
x=1
echo $x
while [$x -le 5];
do
echo $x
x=$(($x+1))
done
:
-sh-4.1$ sh test1.sh
1
test1.sh: line 10: syntax error: unexpected end of file
line 10 is one line after the "done" command. I know it has something to do with the spaces, but there aren't any spaces in my program.
You must have spaces around [ ] to do the whole script working, so :
x=1
echo $x
while [ $x -le 5 ]; do
# ^ ^
# space space
echo $x
x=$(($x+1))
done
or with bash arithmetic :
x=1
echo $x
while ((x < 5)); do
echo $((x++))
done
try put a header into the script
#!/bin/bash
this way linux will automatically interpret the script as a bash script.
I found the problem - carriage-return characters in the file.
I was typing the script in Windows (text pad) and executing it on Linux. The editor on Windows ends lines with \r\n; when running the script on Linux, the presence of \r was creating a problem.
Using vi as editor helped me resolve this issue.
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
I have the simplest issue, but I can't get this to work, and it's driving me crazy. So I am running a script called execute.sh that has a help function. I swear my syntax is correct below; I have tried just one equal sign and now trying two. I have tried the double brackets and continue to get the error "[-h: command not found" . To execute the script I run the following the command.
sh execute.sh -h
Where -h is the argument I am passing int that forces the bash script to call the help function and then exist. But it keeps erroring on the IF Statement. Not sure what else to do, any suggestions?
arg=$1
if ["$arg" == "-h"]; then
helpFunction
exit1;
fi
In
["$arg" == "-h"]
line, you should separate the [ from " also " from the ] with a space. Because [ is actually a command, so it should have a space after it, and the syntax of [ command arguments require a space before ].
arg=$1
if [ "$arg" == "-h" ]; then
helpFunction
exit 1;
fi
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I trying to control that my file exist or not in directory using if-elif-else statement in for loop. But in if and elif command line gives me this error: No command
Below is an example of the codes:
#! /bin/bash
controlfile="String"
firstfile="String"
lastfile="String"
nsolve=false
for i in $(ls $file1| grep /*.png)
do
firstfile=${i:0:21} #satır 10
if ["$firstfile"!="$file2"]; then #ERROR LINE
#something doing...
nsolve=false
for j in $(ls $file2| grep /*.jpeg)
do
if [${j:0:31}==${controlfile:0:31}]; then #.if already jpeg file exist like png
nsolve=true
continue
else
nsolve=false
fi
done
elif [$nsolve==true] #ERROR LINE
then
#something doing...
continue
fi
lastfile=${i:0:21}
done
printf "%s\n" "Successfully"
You are missing spaces around [ and ].
This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 4 years ago.
What is the problem with the following bash code ?
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 {file}"
fi
Generates an error of "Unexpected EOF".
Cygwin 2.10.0(0.325/5/3) in Windows 10.
You have DOS line endings in your file, which means the bash parser sees
if [ -z "$1" ]; then\r
echo "Usage: $0 {file}"\r
fi\r
Rather than a complete if statement, it sees the beginning of one, one whose condition consists of (so far) the commands [ -z "$1" ], then\r, echo "Usage: $0 {file}"\r, and fi\r. The parser is still looking for the then keyword to terminate the condition list, but finds the end of the file instead.
Save your script as a POSIX text file using \n as the line endings, not \r\n.
This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 5 years ago.
Source code:
#!/bin/bash
echo "Parametro is $1"
if [ $1 -gt 9 ]
then
echo "entre al if";
fi
echo"Fin del script";
My bash file has execute permission:
-rwxrwxr-x 1 mzadmin mzadmin 108 Feb 15 13:07 test.sh
I run my bash file like:
bash test.sh 9
And the output is:
Parametro is 9
test.sh: line 8: syntax error: unexpected end of file.
You need a space after echo command : echo"Fin del script";
So :
echo "Fin del script"
Next time, before asking human help, please pass your script online on http://www.shellcheck.net/ (even if this particular error is not detected, because the shell think echo"Fin del script"; is a unknown command...)
Edit
If you don't have output it's because you use in your test -gt (is greater) but instead you need -ge is greater or equal.
Better use bash arithmetic like this, it's more readable and less error prone :
if ((arg >= 9)); then
You need to put a newline at the end of your script, because sometimes things don't work properly if they can't find a newline character. See Why should text files end with a newline?
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
if [[ "$PROXY_URL"==https* ]]; then
echo "Woohoo"
else
echo "Woohoo"
fi
Running $PROXY_URL = "https://yolo" ; ./proxyEnv.sh gives me output of:
bash: =: command not found
Woohoo
What does the "bash: =: command not found" refer to?
Your string comparison should have spaces around the comparator:
if [[ "$PROXY_URL" == https* ]]; then
echo "Woohoo https"
else
echo "Woohoo no https"
fi
Also, that's not how you pass environment variables to bash scripts. You have two options:
PROXY_URL="https://yolo" ./proxyEnv.sh
or
export PROXY_URL="https://yolo"; ./proxyEnv.sh
The first option assigns (without the $) the value to the symbol and then uses that environment for the script (without the ; separating them). It only exists for the script.
The second option exports that symbol to the current environment, which the script inherits.