İf-elif-else statement in Bash [duplicate] - bash

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 ].

Related

bash read file with a path gives error: No such file or directory [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
When to wrap quotes around a shell variable?
(5 answers)
sh read command eats backslashes in input?
(2 answers)
Closed 1 year ago.
So I am trying to read each line of a file but it gives me an error.
No such file or directory
But the file does exist
here is the code:
echo the file path is $pathToGo
while read p
do
echo $p
done < $pathToGo
output:
Now if I hard code the path it works just fine:
pathToGo="C:\Users\sorel\Bash\CW3\4\files\indexFiles\3346"
echo the file path is $pathToGo
while read p
do
echo $p
done < $pathToGo
I have also tried this code on a Linux machine, with a different path, and the same error is showing...
any help would be much appreciated.
try below : I just added -r option .
echo the file path is $pathToGo
while read -r p
do
echo $p
done < $pathToGo

cygwin bash: unexpected EOF [duplicate]

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.

“No such file or directory” when comparing numbers in bash [duplicate]

This question already has answers here:
Less than operator '<' in if statement results in 'No such file or directory'
(3 answers)
Closed 6 years ago.
I'm getting an strange error.
#!/bin/bash
echo "Please enter a number"
read var
declare -i num
num=0
while ($num<$var)
do
echo "$num"
done
./loop: line 5: 6: No such file or directory
What am i mistaking?
This is the correct syntax:
while [ "$num" -le "$var" ]
do
echo "$num"
done
What you wrote, $num<$var, is the syntax for running a program with a file as input. Like this:
cat < file.txt
The error is telling you that $var (the content of $var, not literally "var") was not found when Bash attempted to open it as a file.

Unexpected end of file Bash(last line) [duplicate]

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.

unix bash - unexpected end of file [duplicate]

This question already has answers here:
Bash syntax error: unexpected end of file
(21 answers)
Closed 8 years ago.
#!/bin/bash
maxDate='00000000'
fileDate='20140507'
if [[ $maxDate == '00000000' ]]; then
echo "right"
fi
echo $fileDate
This make me really crazy, I have spend whole day to deal with this format stuff.
the script is like above it print out unexpected end of file
if I delete the last line, it will not print anything which is not correct.
I really don't know what wrong with it.
Remove the DOS line endings from your script with dos2unix. If that is not available, the following can be used:
tr -d '\r' < myscript > myscript.tmp
mv myscript.tmp myscript

Resources