Use Conditional in Bash Script to Check String Argument

use conditional in bash script to check string argument

What about the shorter :

#!/bin/bash

[[ $1 == A ]] && echo "A" || echo "not A"

?

And a beginner version (identical logic) :

#!/bin/bash

if [[ $1 == A ]]; then
echo "A"
else
echo "not A"
fi

Like Scott said, you have a syntax error (missing space).

explanations

  • I use boolean logic here. [[ $1 == A ]] is executed, and then if its true, echo "A" is executed, and if it's false, echo "not A" is executed, See http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
  • [[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals Unless you're writing for POSIX sh, I recommend [[.

Shell script if statement compraising of string comparision and boolean check

@xaiwi gave you the right answer: use [[ instead of [ -- check Conditional Constructs
in the bash manual.

Your logic is wrong though:

if [[ ( "$1" = "ez" &&  $EMS ) || ( "$1" = "coll"  &&  $COLL) ]]; 

With the value $EMS inside the [[...]], you get a "true" result if the value is non-empty -- see -n in Conditional Expressions in the manual

Since "true" is a bash command, you probably want one of

if ([[ $1 = "ez" ]] &&  $EMS) || ([[ $1 = "coll" ]] && $COLL); then ...
if { [[ $1 = "ez" ]] && $EMS; } || { [[ $1 = "coll" ]] && $COLL; }; then ...

The first one uses subshells for command grouping; the second uses the current-shell grouping syntax -- reference.

Check existence of input argument in a Bash shell script

It is:

if [ $# -eq 0 ]
then
echo "No arguments supplied"
fi

The $# variable will tell you the number of input arguments the script was passed.

Or you can check if an argument is an empty string or not like:

if [ -z "$1" ]
then
echo "No argument supplied"
fi

The -z switch will test if the expansion of "$1" is a null string or not. If it is a null string then the body is executed.

checking if argument passed equals a string in bash script

because of the logical or ||, you should use and && otherwise the condition is always true thinking to the negation string can't be equals to the three value.

How to check if a string contains a substring in Bash

You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:

string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi

Note that spaces in the needle string need to be placed between double quotes, and the * wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==), not the regex operator =~.

How to check if an argument is passed to bash script

And finally found the answer with the help of all

if(($#==1));#check if number of arguments is 1 and return a boolean value
then
<code>
fi

An and operator for an if statement in Bash

What you have should work, unless ${STATUS} is empty. It would probably be better to do:

if ! [ "${STATUS}" -eq 200 ] 2> /dev/null && [ "${STRING}" != "${VALUE}" ]; then

or

if [ "${STATUS}" != 200 ] && [ "${STRING}" != "${VALUE}" ]; then

It's hard to say, since you haven't shown us exactly what is going wrong with your script.

Personal opinion: never use [[. It suppresses important error messages and is not portable to different shells.

How do I compare two string variables in an 'if' statement in Bash?

For string equality comparison, use:

if [[ "$s1" == "$s2" ]]

For string does NOT equal comparison, use:

if [[ "$s1" != "$s2" ]]

For the a contains b, use:

if [[ $s1 == *"$s2"* ]]

(and make sure to add spaces between the symbols):

Bad:

if [["$s1" == "$s2"]]

Good:

if [[ "$s1" == "$s2" ]]

How to check if a file contains a specific string using Bash

if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi

You don't need [[ ]] here. Just run the command directly. Add -q option when you don't need the string displayed when it was found.

The grep command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.

$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0

You can specify commands as an condition of if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.

$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$

As you can see you run here the programs directly. No additional [] or [[]].



Related Topics



Leave a reply



Submit