This question already has answers here:
Syntax error near unexpected token 'then'
(2 answers)
Closed 6 years ago.
I can't understand why this simple if statement to check whether a string is empty is not working. It's giving me:
syntax error near unexpected token `then'
#!/bin/bash
str="Hello World!"
if[ ! -z "$str"]; then
echo "str is not empty"
fi
I tried it on my PC and an online editor as well but it shows the same issue. Any idea?
#!/bin/bash
str="Hello World!"
if [ ! -z "$str" ]; then
echo "str is not empty"
fi
Maybe,
insert space after "if" statement.
and, before "]".
[[ ]] is better than [ ] for tests:
#!/bin/bash
str="Hello World!"
if [[ ! -z $str ]]; then
echo "str is not empty"
fi
Shorter form:
#!/bin/bash
str="Hello World!"
[[ ! -z $str ]] && echo "str is not empty"
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
Good evening (or day. Whichever)! I ran into a problem while writing a bash script. I have the code
#!/bin/bash
echo "Available options:"
echo "1. text"
echo "2. exit"
read var1
echo
if [[$var -ge 0]]; then
echo "hello world"
elif [[$var -ge 1]]; then
echo "good bye"
else
echo "error"
fi
and all the time it puts the emphasis on the operator
./proba3.sh: line 9: [[: command not found
./proba3.sh: line 11: [[: command not found
I tried many variations of the branching operation, but still get the same error. I will be very glad for advice or hints on how to fix this error. Thanks in advance
You need spaces inside the brackets.
Also you misspelled the variable name.
Finally, you should probably test for equality not ge
Fixed:
#!/bin/bash
echo "Available options:"
echo "1. text"
echo "2. exit"
read var
echo
if [[ $var -eq 1 ]]; then
echo "hello world"
elif [[ $var -eq 2 ]]; then
echo "good bye"
else
echo "error"
fi
This question already has answers here:
Test for empty string with X"" [duplicate]
(2 answers)
Closed 2 years ago.
Hello I have a simple example which I am confused about:
#!bin/bash
#test_str is a script to test whether a string is empty or not
x=''
if [ -n $x ]
then
echo "'$x' is not empty"
else
echo "'$x' empty"
fi
chmod u+x test_str
./test_str
The output:
'' is not empty
This occurs also if I've not declared a variable (x here).
If I use the flag -z to test for emptiness it works fine:
if [ -z $x ]
then
echo "'$x' is empty"
else
echo "'$x' not empty"
fi
The output:
'' is empty
So how can I use -n correctly?
Thank you!
This is because [ -n $n ] is expanded to [ -n ] so there is nothing to compare and it will return true.
Instead, you should use [ -n "${n}" ] to prevent such errors and to prevent globbing.
I wrote this shell code, but it doesn't get the good output.
Even though the $csoport gets the "...: No such user" output, id doesn't echoes the following line I wrote there.
read felhasznalo
while [ "$felhasznalo" != "exit" ]
do
csoport=`groups $felhasznalo`
echo "$csoport"
if [[ "$csoport" == *": No such user"* ]] ; then
echo -n "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
echo -n "Felhasznalo: "
read felhasznalo
done
You shouldn't try to match the error messsage since you only care if groups fails. You ought to do:
if ! csoport=$(groups "$felhasznalo"); then
printf "Nincs ilyen felhasznalo a rendszerben"
else
echo "$csoport"
fi
This question already has an answer here:
Weird behavior of BASH glob/regex ranges
(1 answer)
Closed 4 years ago.
I'm writing a script to check a password if it has upper case or not, but it always shows that it has upper case.
I tried to use
if [[ $password =~ [A-Z] ]]; then
echo "Contains Upper Case"
but it always prints that.
This is my script:
read -p "Enter Password: " password
if [ ${#password} -lt 8 ]; then
echo "TOO SHORT , MAN! "
fi
if [[ $password =~ [A-Z] ]]; then
echo "Contains Upper Case"
fi
Do not use [A-Z]. It works only in some language settings / locales (the ones where ordering looks like ABCD...Zabcd...z, intead of like AaBb...Zz).
For your code to work everywhere, use [[:upper:]] instead.
#!/usr/bin/env bash
[ -n "$BASH_VERSION" ] || { echo "ERROR: Shell is not bash" >&2; exit 1; }
if [[ $password =~ [[:upper:]] ]]; then
echo "Contains at least one upper-case character"
else
echo "Does not contain one or more upper-case characters"
fi
So I am trying to test a few things coding in bash. As a n00b I am having some problems with the basics and how irritating it can get using shell dealing with numbers.
if $((echo $?)) > 0 ;then
echo "there is an error";
else
echo "it passed";
fi
the error I get is :
bash: echo 0: syntax error in expression (error token is "0")
I tried without the echo and I get:
bash: ./0: Permission denied
so I am a bit confused what the shell is trying to do.
Thanks
The whole arithmetic evaluation must be within double parentheses:
if (($? > 0)) ;then
echo "there is an error";
else
echo "it passed";
fi
try this:
OUT=$?
if [ $OUT -eq 0 ];then
echo "OK!"
else
echo "NOT OK!"
fi