cygwin bash: unexpected EOF [duplicate] - bash

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.

Related

bash if statement command not found [duplicate]

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

how can I write if condition in bash? [duplicate]

This question already has answers here:
How to check if a file is empty in Bash?
(11 answers)
Closed 4 months ago.
in bash regarding "if" condition. How can I write in a script that will check if a text file is empty continue the script else < perform some command>?
You probably want to this:
if [ -s "$file" ]
then
echo 'file exist and non-empty'
# perform some command then exit?
fi
The [ is a short-hand for the command test. See the man bash in the "SHELL BUILTIN COMMANDS" for details. man test is what I end up using most of the time, though.
If it is important that it is a text file, then preferably check the file has the expected extension (here .txt):
if [ "${file: -4}" = ".txt" ]
then
echo file is a text file (extension)
fi
"${file: -4} means extract the last 4 letters from the variable file, and we then compare that with ".txt".
Failing that you use the file utility to inspect the content of the file:
if [ "`file -b "$file"`" = "ASCII text" ]
then
echo file is a text file (content)
fi

İ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 conditional returning an error of bash: =: command not found [duplicate]

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.

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