-1

Im running a command to get information about FC cards names and saving to variable. Some servers those cards might not be available. on those machines the Variable saves a whitespace character. I want to use the If condition to check if the variable has whitespaces and then act accordingly.

The below one doesnt seems to work .

if [ "$VAR" == null ]
then
print "No special card Found"
else
#Run  a command
fi  

closed as unclear what you're asking by Kusalananda, Thomas, Jeff Schaller, Romeo Ninov, RalfFriedl Nov 18 '18 at 15:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What makes you believe a space character and the string "null" are the same string? – jlliagre Feb 28 '18 at 5:03
  • In what way did it "not work"? Could you please help us with your input? – Kusalananda Mar 1 '18 at 6:11
1

To specifically test for a single space character, use

if [ "$VAR" = ' ' ]; then
    print 'No special card found'
else
    # do something else
fi

If you want to check whether the variable is empty, then use

if [ -z "$VAR" ]; then
   # etc.
  • Hello Kusalananda, It didnt work – satsensort Mar 1 '18 at 4:31
  • @satsensort Ah, I see. What makes you think that your "it did not work" will help us understand what happened? It is the most useless type of bug report you can ever give a developer, and the only thing it does is that it shows a lack of interest in actually wanting to solve the problem at all. We can't read your mind or see what's going on on your screen. Could you please help us solve your problem? – Kusalananda Mar 1 '18 at 6:10
0

The first line stands for if the variable $VAR is empty

if [[ ! $VAR ]]; then
    print "No special card Found"
else
    #Run  a command
fi 

or explicitly with -z switch

if [[ -z $VAR ]]; then
    print "No special card Found"
else
    #Run  a command
fi 
  • Hello Giles, It didnt work. – satsensort Mar 1 '18 at 4:31
  • Have you tested since @ikkachu edited to correct my typo ? – Gilles Quenot Mar 1 '18 at 11:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.