Echo not displaying variable in If-Else condition | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Echo not displaying variable in If-Else condition

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2013
Chetanz Chetanz is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 July 2013, 11:09 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Echo not displaying variable in If-Else condition


Code:
 
if [ "${x_oug}" -ne 0 ]
then
echo "Entry Valid                  :  ${x_oug}"
else
echo "Entry Invalid                  : " 0
fi

In the above code the 3rd line is not working... it does not print anything

I tried following as well .. but no luck!


Code:
echo "Entry Valid                   :   ${x_oug}"
echo "Entry Valid                   :   " ${x_oug}
echo "Entry Valid                   :   $x_oug
echo "Entry Valid                   : " "${x_oug}"

However, if I put a constant like following it works correctly


Code:
echo "Entry valid                  : 5 "

In fact I am forced to use if else as echo not prinitng the variable when it has 0 value

Please help me on this

Thanks and Regards
Chetanz
Sponsored Links
    #2  
Old 02-22-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 4:15 AM EST
Location: San Jose, CA, USA
Posts: 5,080
Thanks: 195
Thanked 1,699 Times in 1,442 Posts
How are you setting x_oug?
Sponsored Links
    #3  
Old 02-22-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 5:07 AM EST
Location: Aachen, Germany
Posts: 4,672
Thanks: 78
Thanked 1,154 Times in 1,084 Posts
What's the value of x_oug ? How do you assign it? Did you run the script with the xtrace option set?
    #4  
Old 02-22-2013
Chetanz Chetanz is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 July 2013, 11:09 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for quick replies

I am retriving it from a file which is in fact output of vmstat... eliminating header, awking a particular column values and then getting max of that to set x_oug

Code:
x_oug=`cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print x7}'|grep -Ev '(#.*$)|(^$)'|grep -v System|awk '{if($1>mx)mx=$1;}END{print mx}'`

Regards
Chetanz
Sponsored Links
    #5  
Old 02-22-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 5:07 AM EST
Location: Aachen, Germany
Posts: 4,672
Thanks: 78
Thanked 1,154 Times in 1,084 Posts
You know you can make THAT statement way more legible and efficient?

Pls post output of echo "$x_oug"
Sponsored Links
    #6  
Old 02-22-2013
Chetanz Chetanz is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 3 July 2013, 11:09 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Hello

Output remains as following i.d. NULL


Code:
Entry valid                  :

Regards
Chetanz
Sponsored Links
    #7  
Old 02-22-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 4:15 AM EST
Location: San Jose, CA, USA
Posts: 5,080
Thanks: 195
Thanked 1,699 Times in 1,442 Posts
Quote:
Originally Posted by Chetanz View Post
Thanks for quick replies

I am retriving it from a file which is in fact output of vmstat... eliminating header, awking a particular column values and then getting max of that to set x_oug

Code:
x_oug=`cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print x7}'|grep -Ev '(#.*$)|(^$)'|grep -v System|awk '{if($1>mx)mx=$1;}END{print mx}'`

Regards
Chetanz
I have no idea what you thought you should get out of this, but the awk in the middle of this pipeline ( awk '{print x7}' ) prints an empty line for every line it reads. The two greps that follow that are no-ops, and the awk after that converts one or more empty lines into a single empty line. The ksh test -eq operator is intended to compare two numeric values. Since an empty string is not a numeric value, I'm not sure the results of the comparison are defined, but I expect that ksh will say that [ "" -eq 0 ] evaluates to false rather than giving a syntax error.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Displaying contents with Echo sathyaonnuix UNIX for Dummies Questions & Answers 3 09-04-2012 07:08 AM
echo the NAME of the variable ajp7701 Shell Programming and Scripting 5 04-25-2012 07:57 PM
redirect stdout echo command in condition A run in condition B jao_madn Shell Programming and Scripting 3 11-02-2011 10:01 PM
Variable not displaying while running in cron job josephroyal Shell Programming and Scripting 4 06-22-2011 07:52 AM
multiple echo statements in if condition wizardofoz Shell Programming and Scripting 2 03-12-2011 04:00 PM



All times are GMT -4. The time now is 06:17 AM.