Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Echo not displaying variable in If-Else condition

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 02-22-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2015, 8:19 PM EDT
Location: San Jose, CA, USA
Posts: 6,400
Thanks: 269
Thanked 2,142 Times in 1,832 Posts
How are you setting x_oug?
Sponsored Links
    #3  
Old Unix and Linux 02-22-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 May 2015, 3:00 PM EDT
Location: Aachen, Germany
Posts: 5,993
Thanks: 108
Thanked 1,607 Times in 1,509 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 Unix and Linux 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 Unix and Linux 02-22-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 May 2015, 3:00 PM EDT
Location: Aachen, Germany
Posts: 5,993
Thanks: 108
Thanked 1,607 Times in 1,509 Posts
You know you can make THAT statement way more legible and efficient?

Pls post output of echo "$x_oug"
Sponsored Links
    #6  
Old Unix and Linux 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 Unix and Linux 02-22-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 May 2015, 8:19 PM EDT
Location: San Jose, CA, USA
Posts: 6,400
Thanks: 269
Thanked 2,142 Times in 1,832 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:08 AM
echo the NAME of the variable ajp7701 Shell Programming and Scripting 5 04-25-2012 06:57 PM
redirect stdout echo command in condition A run in condition B jao_madn Shell Programming and Scripting 3 11-02-2011 09:01 PM
Variable not displaying while running in cron job josephroyal Shell Programming and Scripting 4 06-22-2011 06:52 AM
multiple echo statements in if condition wizardofoz Shell Programming and Scripting 2 03-12-2011 03:00 PM



All times are GMT -4. The time now is 08:21 PM.