Quick question regrading if/else statements in shell, Right now I am trying to test if two different values are equal to a third
echo "CompareDateValues"
if [ "${TodaysDate}" = "${prevDate}" & "${currDate}" ]; then
echo "Dates Are A Match : TodaysDate:${TodaysDate} = savedStateRunDates:${prevDate}"
else
echo "Dates Are Not A Match : TodaysDate:${TodaysDate} = savedStateRunDates:${prevDate}"
echo Exit
exit 1
fi
As you can see from the code above I am trying to test if prevdate and currdate match Todaysdate but I can seem to get it working any help would be great
You have to use two conditions:
if [ "$TodaysDate" = "$prevDate" ] && [ "$TodaysDate" = "$currDate" ] ; then
or the -a operator (not recommended)
if [ "$TodaysDate" = "$prevDate" -a "$TodaysDate" = "$currDate" ] ; then
or, if you're in bash, switch to [[ ... ]] conditions:
if [[ $TodaysDate = $prevDate && $TodaysDate = $currDate ]] ; then
if test "$TodaysDate" = "$prevDate" && test "$TodaysDate" = "$currDate"; then ...
Related
I have a code similar to below:
while [ $k = 0 ];
do
if [ $Year1 = $Year2 ] && [ $Month1 = $Month2 ]; then
echo "abcd"
else
echo"erty"
The error is line highlighted line > line 144]: [: argument expected
At least one of your variables contains unexpected data (e.g. is empty, or has spaces or new line characters), causing the [ command to not receive the arguments it expects.
Always quote your variables ("$Year1" instead of $Year1) to avoid surprises like this one.
while [ "$k" = 0 ]; do if [[ $Year1 == $Year2 && $Month1 == $Month2 ]]; then echo "abcd"; else echo "erty"; fi; done
Helpfull could be: https://www.shellcheck.net
You should use == for comparisons instead of = and also please use -eq whenever you want to use == if your code is for integer comparison. For example,
if [ $FLAG -eq 1 ];then
For strings, you can use =, like for example NOTE : Edited as pointed by Sir Athos
if [ "$Year1" = "$Year2" ] && [ "$Month1" = "$Month2" ]; then
or if you are comparing variable with string then, you can also use ==,
if [ "$STA" == "Completed" ];then
Adding test script to further clarify :
-bash-4.2$ cat string.sh
#!/bin/bash -x
str1="Hello There"
str2="Hello There"
if [ "$str1" = "$str2" ];then
echo "Matched"
else
echo "Not Matched"
fi
if [ "$str1" == "Hello There" ];then
echo "Matched"
else
echo "Not Matched"
fi
-bash-4.2$ ./string.sh
+ str1='Hello There'
+ str2='Hello There'
+ '[' 'Hello There' = 'Hello There' ']'
+ echo Matched
Matched
+ '[' 'Hello There' == 'Hello There' ']'
+ echo Matched
Matched
-bash-4.2$
What is wrong with the below syntax, I keep getting [: too many arguments
All the variables are strings, also I have introduced var3 and str3, two make sure we are not comparing two null values, is there any other better option to deal with it
if [ "$var1" = "$var2" = "$var3" && "$str1" = "$str2" = "$str3" ]
then
echo " something"
elif [ "$var1" = "$var2" = "$var3"]
then
echo something else
elif [ "$str1" = "str2" = "str3" ]
then
echo something else
else
exit
fi
You cannot do a = b = c check in a test ([...])
You cannot use &&, || in [...] test, instead, use -a (and) -o (or)
so fix your codes as something like:
if [ "$a" = "$b" -a "$a" = "$c" -a "$d" = "$x" ]; then
...
If you want to use &&, || you could use [[...]] condition expression evaluation, which is bash built-in
I have a variable(stageVar) getting from Datastage, and need to check whether that variable is equal to zero then replace the 100 else stageVar.
After that I need to find the mod and save in variable.
I have tried the below code but am not successful.
var= if [stageVar -eq 0] ; then "100" ; else stageVar ; fi; var2='expr $var % 100'; echo $var2;
You can use this in bash:
[[ $stageVar -eq 0 ]] && var=100 || var=$stageVar
((var2 = var % 100))
echo $var2
If you want to keep the if/then/else construction, you can use:
var=$(if [ "$stageVar" = 0 ]; then echo "100"; else echo $stageVar; fi)
In bash scripting the if condition statement is not working properly with using "&&"
ARGCOUNT=$#
if (( "$ARGCOUNT" != "2" )) ;then
echo "number of arguments must be two"
fi
DFLAG=$1
HFLAG=$2
if (((( $DFLAG = "Mon" )) || (( $DFLAG = "MON" )) || (( $DFLAG = "mon" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
echo " CS599 "
cd CS599
elif (((( $DFLAG = "Wed" )) || (( $DFLAG = "WED" )) || (( $DFLAG = "wed" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
cd CS699
echo " CS699 "
elif (((( $DFLAG = "Fri" )) || (( $DFLAG = "FRI" )) || (( $DFLAG = "fri" ))) && ((( HFLAG = "2" )) || (( HFLAG = "3" )) || (( HFLAG = "4" ))));then
cd CS799
echo " CS799 "
else
echo "."
fi
my program is executing only else statement irrespective of arguments. means it evaluating if block false.
What is the problem ?
The parenthesis you use are for arithmetic evaluation. I think you are over using them, and it makes your script complicated.
This snippet below does work:
#!/bin/bash
ARGCOUNT=$#
if [ "$ARGCOUNT" -ne 2 ] ;then echo "number of arguments must be two"; fi
# put DFLAG in lower case (see man bash).
DFLAG=${1,,}
HFLAG=$2
if [ "$DFLAG" = 'mon' -a "$HFLAG" -ge 2 -a "$HFLAG" -le 4 ]; then
echo ok
else
echo failed
fi
As you can see, I optimized your expression:
Except for the case of $ARGCOUNT (which is safe because you initialized it to $#), don't forget to encase variable with double quote to avoid expansion.
In the declaration of DFLAG, I used the convert to lower case string operator (?). With that you won't have to check for each permutation of case in DFLAG. This might not work in bash3.
If you use the test or [ builtin, you can use -a between each expression to do a and.
Arithmetic evaluation with the test/[ builtin use the following operators: -ne (inequality), -eq (equality)-ge (greater or equal), -le (lesser or equals), -lt (lesser), -gt (greater).
As said in another answer, you can replace "$DFLAG" = 'mon' by "$DFLAG" == 'mon'. But this is not POSIX conformant (as said in my comment below) and I'm not enough knowledgeable on that to know if it's a good idea or not.
On a side note, if $HFLAG condition should always be the same, you can write your code like this:
if [ "$HFLAG" -ge 2 -a "$HFLAG" -le 4 ]; then
case "$DFLAG" in
mon|Mon|MON)
echo "monday";
;;
fry|Fry|FRY)
echo "friday";
;;
*)
echo "other"
;;
esac
fi
If that case, I putted back all permutation of case in case you were in bash3, to show you an example to do without ${DFLAG,}.
If you are looking for what mistake you did which is making condition to go to else part ...then it's simple mistake which almost every programmer do once in life ... using single "=" instead of "==" during comparison. Modify it accordingly and you should get expected flow/result in your script.
One eg.
$DFLAG = "Mon"
change to below notice the double equal sign
"$DFLAG" == "Mon"
First, you should go easy on the parenthesis. Those are fragile things.
Using Bash syntax (non-POSIX, less portable), you can write:
ARGCOUNT=$#
DFLAG=${1,,} # lower case
HFLAG=$2
# don't need the quotes in (( )) as we test arithmetic value
(( $ARGCOUNT != 2 )) && echo "number of arguments must be two"
shopt -s extglob # for #(1|2|3) below, see http://mywiki.wooledge.org/glob#extglob
if [[ $DFLAG = "mon" && $HFLAG == #(2|3|4) ]]; then
echo " CS599 "
cd CS599
if [[ $DFLAG = "wed" && $HFLAG == #(2|3|4) ]]; then
cd CS699
echo " CS699 "
if [[ $DFLAG = "fri" && $HFLAG == #(2|3|4) ]]; then
cd CS799
echo " CS799 "
else
echo "."
fi
Now you see how much you repeat yourself and can improve the algorithm. For instance, test HFLAG, if valid test DFLAG otherwise …
Read
Tests And Conditionals
Arithmetic Expression
Globbing extglob
why this do not work:
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE=$(echo "Outbound");
fi
or
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE=echo "Outbound";
fi
or
if [ $_TYPE == 1 ] || [ $_TYPE == 2 ]; then
$_TYPE="Outbound";
fi
I'm receiving this error: line 251: 2=Outbound: command not found
In POSIX shells such as Bash, $ is not part of the variable-name, it's just the notation for expanding the variable (to obtain its value); so, for example, echo "$_TYPE" prints the value of the variable _TYPE. You don't use the $ when you're assigning to the variable. So you just need:
if [[ "$_TYPE" = 1 || "$_TYPE" = 2 ]] ; then
_TYPE=Outbound
fi
$ is used to access the value, but if you have to assign a value, the syntax is :
_TYPE="newValue"