unix bash - unexpected end of file [duplicate] - bash

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

Related

echo and read with pipe issue [duplicate]

This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?

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

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

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

Bash script: Comparing number of lines with integer argument [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
im new to bash scripting, appreciate if you can help.
Im trying to write a script to compare the lines in file with integer argument.
Here is what i've got so far but i make some mistakes and get error.
#!/bin/bash
a="$1"
b="wc -l < /filepath/filename.txt"
if (( $a < $b )); then
echo "file has more lines than integer"
else
echo "file has less lines than integer"
fi
Appreciate if you can point to where i make mistake.
b="wc -l < /filepath/filename.txt"
should instead be:
b=$(wc -l < /filepath/filename.txt)
...if you want to run that command and store its output in the variable.

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.

Resources