Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I’m using bash shell on Amazon Linux. When I run the below block of code
if [ $rc -eq 0 ] then
passed=`tr ' ' '\n' < $TFILE | grep -c PASSED`
error=`tr ' ' '\n' < $TFILE | grep -c ERROR`
warning=`tr ' ' '\n' < $TFILE | grep -c WARNING`
subject="Automated Results - $passed passed, $error errors, $warning warnings."
else
subject="Failed to run any tests."
fi
I get the error, “syntax error near unexpected token `else’”. What do I need to do to write this if-then-else block correctly?
To quote the syntax definition from help if in bash (which is quite close to the relevant POSIX spec):
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
There can be multiple commands used in the conditional part of an if statement, and a command separator (represented as a semicolon here) is mandatory between the last of them and the list of commands to given should that conditional part return a successful status.
Comparing the code given against that syntax definition, it's missing such a separator:
if [ $rc -eq 0 ]; then
# ^
# \- this semicolon, or a newline, is mandatory before "then"
As it is, then is being passed as an argument to the [ command, not parsed as syntax.
(Since you're tagged bash, consider also using native math syntax: if (( rc == 0 )); then is both more readable and less buggy).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The program takes input but show the following errors, how to resolve those errors ?
[ is not mere syntax, it's a command, therefore it needs space to separate it from its arguments.
while [ $counter -le $number ]
# .....^....................^
Make sure you validate that number is actually only digits.
Some other comments:
Use $(...) instead of `...` -- see
https://github.com/koalaman/shellcheck/wiki/SC2006
for more details.
bash can do arithmetic, you don't need to call out to expr. See
Arithmetic Expansion
and Shell Arithmetic
in the manual.
There is also an arithmetic conditional construct (analogous to the
string-oriented [[...]] conditional construct) -- see
Conditional Constructs
and scroll down to ((...)) (there's no direct link).
I think you need to add a space after [ and before ]:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter + 1`
done
echo $total
Working here on Ubuntu:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
Actually, on line 7, you forgot the completing backtick.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I try and use if [ "$1" =~ "$regex" ]; then
I keep on getting back the same error: [: =~: binary operator expected
Here is an example function where I get the error:
char_check() {
regex='^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'
if [ "$1" =~ "$regex" ]; then
echo "No illegal characters."
else
echo "Illegal characters."
fi
}
Any response would be a great help.
Regular expressions require [[ expression ]] as shown in the man page of bash.
Also you must not quote the regex itself, e.g. you should prefer $regex over "$regex"
char_check() {
regex='^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'
if [[ "$1" =~ $regex ]]; then
echo "No illegal characters."
else
echo "Illegal characters."
fi
}
If you quote the regex then you are trying to match the string, not the pattern. In other words it will match the weird string '^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I'm learning Bash and I have no idea why my if statement isn't working (working in putty if it makes any difference). I've tried looking online for how an if statement is supposed to be built and followed it making sure I got all the details correct. When I run it both of the if's come out positive and both of them get executed rather than just 1.
echo -n "Enter file name: "
read x
echo "file $x with numbers (Y/N)?"
read y
if [ "$y"="n" ];
then
cat $x
fi
if [ "$y"="y" ];
then
cat -n $x
fi
exit
can anyone help please?
Remember there needs to be a whitespace in your if condition else it becomes an assignment. https://uvesway.wordpress.com/2013/03/11/some-whitespace-pitfalls-in-bash-programming/
"$y"="n" is neither a conditional test nor an assignment. It's just a single string e.g, if $y is 'y', then "$y"="n" is the same as the literal 'y=n' and this literal string is being treated as a single parameter to the test ([) command.
A condition like below is always evaluated to true:
if [ abcd ]; then echo true; fi
while an empty string makes it false:
if [ '' ]; then echo true; else echo false; fi
White spaces aren't optional (everywhere) in bash. Even though the [ abcd thingy looks like a syntax, it's not; [ is a command (test) and abcd is its first positional parameter.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting a "command not found" error after checking a variable for the substring .txt.
Here's a simple version of my script.
myscript.sh
#!/bin/sh
if [["$1" == *.txt]]
then
echo $1
fi
Result:
> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
The error is because of a space needed around the brackets [[ and ]]:
#!/bin/sh
if [[ "$1" == *.txt ]]
then
echo $1
fi
That is, instead of:
if [["$1" == *.txt]]
use
if [[ "$1" == *.txt ]]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh
I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by removing the ; in line 13 but it didn't help.
test.sh
#!/bin/bash
echo "Enter app name"
read y
$y &
top -b -n 1 > topLog.log
#-w checks for the whole word not and sub string from that word
grep -w "$y" topLog.log > p1
#-s option checks if the file p1 is present or not
if [ -s "p1"]; #line 13
then
echo "Successful "
else
echo "Unsuccessful"
fi
rm p1
I am new to bash scripting.So if there is any silly mistake please excuse me.
Change
if [ -s "p1"]; #line 13
into
if [ -s "p1" ]; #line 13
note the space.
I got this error while trying to use the && operator inside single brackets like [ ... && ... ]. I had to switch to [[ ... && ... ]].
You're missing a space after "p1":
if [ -s "p1" ];
add a space before the close bracket
If you created your script on windows and want to run it on linux machine, and you're sure there is no mistake in your code, install dos2unix on linux machine and run dos2unix yourscript.sh. Then, run the script.