This question already has answers here:
In Bash, how can I check if a string begins with some value?
(13 answers)
Closed 6 years ago.
I need to check if a word is starting with ">" character or not
for example A=">abcd" should return true, B="assdf" should not.
I Tried the following snippets but it does not work
if [ "$A" == "\>*" ]; then
echo "True"
fi
The following does not work also
A=">dfssdfsd"
if [[ "$A" =~ "\>*" ]]; then
echo "aaa"
fi
Thank you
You can use the following :
expr "$A" : '>'
$ if [ 0 -ne $(expr 'test' : '>') ]; then echo "True"; else echo "False"; fi
False
$ if [ 0 -ne $(expr '>test' : '>') ]; then echo "True"; else echo "False"; fi
True
Related
I would like to write in one line this:
if [$SERVICESTATEID$ -eq 2]; then echo "CRITICAL"; else echo "OK"; fi
So to do a test in my shell I did:
if [2 -eq 3]; then echo "CRITICAL"; else echo "OK"; fi
The result is
-bash: [2: command not found
OK
So it doesn't work.
Space -- the final frontier. This works:
if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi
Note spaces after [ and before ] -- [ is a command name! And I removed an extra $ at the end of $SERVICESTATEID.
An alternative is to spell out test. Then you don't need the final ], which is what I prefer:
if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi
Write like this, space is required before and after [ and ] in shell
if [ 2 -eq 3 ]; then echo "CRITICAL"; else echo "OK"; fi
Shorter format.
( [ 2 -eq 3 ] && echo "CRITICAL" ) || echo "OK"
Regex pattern type numbers : 10,12.1,+3.33,-1,0004,-48.9
Oneliner attacks again!
( [ `echo $number 2>/dev/null | grep -E "^[ ]*(\+|\-){0,1}[0-9]+(\.[0-9]+)?$"` ] && echo "NUMBER" ) || echo "NOT NUMBER"
This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 3 years ago.
I'm writing a bash script where i need to combine two conditions with && operator
var1=value
var2=1
if [-z $var1 ] && [$var2=="1"]; then
do something
else
do something else
fi
but it always executes the else part.
My research
google gave me bash conditions like this but its not working for me.
if [condition1] && [condition2]; then
do something
fi
Another method i tried is this but it still completely ignored the true part
if [[-z $var1 ]] && [$var2=="1"]; then
do something
else
do something else
fi
Tried with -a operator like this
if [-z $var1 -a $var2=="1" ]; then
tried nested if
if [-z $var1 ]; then
if [$var2=="1"]; then
do something
fi
else
do something else
fi
So i know i am doing something wrong but my goal is i want to check for any value in $var1 and also want $var2 condition to be true and execute the true part.
UPDATE
i tried this
#! /bin/bash
set -o nounset
#set -o errexit
set -o pipefail
set -o xtrace
var1=pop
var2=1
if test -z "$var1" && test "$var2" -eq 1; then
echo Y1
fi
if [ -z "$var1" ] && [ "$var2" -eq 1 ]; then
echo Y2
fi
and this is the output
root#c847b6423295:/# ./test.sh
+ var1=pop
+ var2=1
+ test -z pop
+ '[' -z pop ']'
root#c847b6423295:/#
What am i doing wrong ?
You want to encode "$var1 is not empty and $var2 is equal to 1", you can do:
if test -z "$var1" && test "$var2" -eq 1; then
echo Y
fi
which is equivalent to:
if [ -z "$var1" ] && [ "$var2" -eq 1 ]; then
echo Y
fi
If you want to compare strings, use a single equals sign:
if test "aaa" = "aaa"; then
echo Y
fi
This question already has answers here:
Why do shell script comparisons often use x$VAR = xyes?
(7 answers)
Closed 4 years ago.
I'm trying to understand a script that will stop when executed as root:
#!/usr/bin/env bash
if [ x"$(whoami)" = x"root" ]; then
echo "Error: don't run this script as root"
exit 1
fi
I have tested this and it works as intended even if I remove the x in the if statement. My question is why is the x in x"$(whoami)" and x"root" needed?
basically the [ is a softlink to an external program called test, therefore the condition is passed to it as program arguments, and doing so if you don't surround a $variable with "$quotes" , and the variable happens to be empty it won't be considered as an empty argument, it will be considered as no argument (nothing)
#!/bin/bash -eu
var=bla
if [[ $var == bla ]];then
echo first test ok
fi
var=""
if [[ $var == "" ]];then
echo second test ok
fi
if [ "$var" == "" ];then
echo third test ok
fi
if [ x$var == "x" ];then
echo fourth test ok
fi
echo this will fail:
if [ $var == "" ];then
echo fifth test ok
fi
echo because it is the same as writing:
if [ == "" ];then
echo sixth test is obviously eroneous
fi
echo but also you should quote your variables because this will work:
var="a b"
if [ "$var" == "a b" ];then
echo seventh test ok
fi
echo ... but this one won\'t as test now has four arguments:
if [ $var == "a b" ];then
echo eighth test ok
fi
This question already has answers here:
Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ]
(7 answers)
Closed 8 years ago.
I don't set any values for $pass_tc11; so it is returning null while echoing. How to compare it in if clause?
Here is my code. I don't want "Hi" to be printed...
-bash-3.00$ echo $pass_tc11
-bash-3.00$ if [ "pass_tc11" != "" ]; then
> echo "hi"
> fi
hi
-bash-3.00$
First of all, note you are not using the variable correctly:
if [ "pass_tc11" != "" ]; then
# ^
# missing $
Anyway, to check if a variable is empty or not you can use -z --> the string is empty:
if [ ! -z "$pass_tc11" ]; then
echo "hi, I am not empty"
fi
or -n --> the length is non-zero:
if [ -n "$pass_tc11" ]; then
echo "hi, I am not empty"
fi
From man test:
-z STRING
the length of STRING is zero
-n STRING
the length of STRING is nonzero
Samples:
$ [ ! -z "$var" ] && echo "yes"
$
$ var=""
$ [ ! -z "$var" ] && echo "yes"
$
$ var="a"
$ [ ! -z "$var" ] && echo "yes"
yes
$ var="a"
$ [ -n "$var" ] && echo "yes"
yes
fedorqui has a working solution but there is another way to do the same thing.
Chock if a variable is set
#!/bin/bash
amIEmpty='Hello'
# This will be true if the variable has a value
if [ $amIEmpty ]; then
echo 'No, I am not!';
fi
Or to verify that a variable is empty
#!/bin/bash
amIEmpty=''
# This will be true if the variable is empty
if [ ! $amIEmpty ]; then
echo 'Yes I am!';
fi
tldp.org has good documentation about if in bash:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
I found an interesting Bash script that will test if a variable is numeric/integer. I like it, but I do not understand why the "0" is not recognized as a number? I can not ask the author, hi/shi is an anonymous.
#!/bin/bash
n="$1"
echo "Test numeric '$n' "
if ((n)) 2>/dev/null; then
n=$((n))
echo "Yes: $n"
else
echo "No: $n"
fi
Thank you!
UPDATE - Apr 27, 2012.
This is my final code (short version):
#!/bin/bash
ANSWER=0
DEFAULT=5
INDEX=86
read -p 'Not choosing / Wrong typing is equivalent to default (#5): ' ANSWER;
shopt -s extglob
if [[ $ANSWER == ?(-)+([0-9]) ]]
then ANSWER=$((ANSWER));
else ANSWER=$DEFAULT;
fi
if [ $ANSWER -lt 1 ] || [ $ANSWER -gt $INDEX ]
then ANSWER=$DEFAULT;
fi
It doesn't test if it is a numeric/integer. It tests if n evaluates to true or false, if 0 it is false, else (numeric or other character string) it is true.
use pattern matching to test:
if [[ $n == *[^0-9]* ]]; then echo "not numeric"; else echo numeric; fi
That won't match a negative integer though, and it will falsely match an empty string as numeric. For a more precise pattern, enable the shell's extended globbing:
shopt -s extglob
if [[ $n == ?(-)+([0-9]) ]]; then echo numeric; else echo "not numeric"; fi
And to match a fractional number
[[ $n == #(?(-)+([0-9])?(.*(0-9))|?(-)*([0-9]).+([0-9])) ]]