If condition problem

Hi All,

I am using below if condition to check whether null is passed as a parameter to the program

  if ["$3"="null"] or ["$4"="null"];
 then
 echo "ABC">>$FILE
 else 
 echo "CDF">>$FILE
 fi
 

However it is saying me null=null command not found . Please help me with this

Hello Hypesslearner,

Could you please try following and let me know if this helps.

if [[ "$3"="null" || "$4"="null" ]]
then
     echo "ABC" >> "$FILE"
else
     echo "CDF" >> "$FILE"
fi
 

Thanks,
R. Singh

1 Like

Just querying:-

Are you sure you are looking for the four ASCII characters 'null' or do you really mean "" as a null.

If so this might be what you are looking for:-

if [ "$3" = "" ] || [ "$4" = "" ]

I am looking for 4 ascii characters and its working now with above code . Thanks all .

Have a try with:

[ -z "$3$4" ] && echo "Var 3+4 are empty"
[ 3 -eq $# ] && echo "Script has 3 args"
echo "Arg 1 has ${#1} Chars

Note that [[ condition ]] is called bashism, as it allows the 'not using' quotes, which is bad for code portability.
Instead, try to get used to [ condition ] which requires proper quoteing but beeing POSIX standard, it is much more compatible among different shells.

Hope this helps

Parameter checking is not working

I am passing as below
$2=ABC and $3,$4 as empty but this is executing step1

 if [["$2"!="" && "$3"!="" && "$4"!=""]]
 then
 echo "Execute step1
 else 
   echo "Execute step2"
 fi

Separate all the quoted strings with spaces from their neighbourhood.

Still it is executing step1 only

 if [["$2"!=" " && "$3"!=" " && "$4"!=" "]]
 then
 echo "Execute step1
 else 
echo "Execute step2"
 fi 

You did NOT separate ALL quoted strings from ALL their neighbours by inserting spaces!

---------- Post updated at 15:58 ---------- Previous update was at 15:58 ----------

Actually, you inserted spaces into the empty strings which is not what you wanted.

1 Like

Yes Got it now and here it looks like

 if [[ "$2" != "" && "$3" != "" && "$4" != "" ]]