Echo not displaying variable in If-Else condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo not displaying variable in If-Else condition
# 8  
Old 02-22-2013
Brilliant, Don! I didn't see that x7, took it for $7 (although suspected an empty variable). Soooo - x_oug being the empty string which is -ne "0" yielding the output as posted!
# 9  
Old 02-22-2013
Hello Friends

Thank you so much for your quick help

I got the issue

In the following line

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

the logic upto following part is fine
Code:
cat /u01/zopepx/vmstat1.txt|grep -v kthr|grep -v '-'|grep -v r|awk '{print $8}'|grep -Ev '(#.*$)|(^$)'|grep -v System|

till this point we are getting values as
Code:
0
0
0

and when logic is applied over it to get MAX as following, to my surprise it is giving us MAX of 3 zeros as NULL
Code:
awk '{if($1>mx)mx=$1;}END{print mx}'

Regards
Chetanz

---------- Post updated at 07:30 AM ---------- Previous update was at 07:19 AM ----------

Hello

The results in previous post can be checked as following
Code:
#> cat >tst.txt
0
0
0
 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt

Now change values in tst.txt and run following

Code:
 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt


Now that the error is found, the problem is not solved .. how to get max of multiple 0 values?

Regards
Chetanz
# 10  
Old 02-22-2013
Quote:
Originally Posted by Chetanz
... ... ...
---------- Post updated at 07:30 AM ---------- Previous update was at 07:19 AM ----------

Hello

The results in previous post can be checked as following
Code:
#> cat >tst.txt
0
0
0
 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt

Now change values in tst.txt and run following

Code:
 #> awk '{if($1>mx)mx=$1;}END{print mx}' <tst.txt


Now that the error is found, the problem is not solved .. how to get max of multiple 0 values?

Regards
Chetanz
Try:
Code:
printf "%d\n" 0 0 0|awk 'mx==""||$1>mx{mx=$1}END{print mx}'

# 11  
Old 02-22-2013
Clear and efficient is a BEGIN section - the loop section stays small:
Code:
awk 'BEGIN {mx=0} $1>mx {mx=$1} END {print mx}'

In this case you can also do a cast to integer by a +0:
Code:
awk '$1>mx {mx=$1} END {print mx+0}'


Last edited by MadeInGermany; 02-22-2013 at 09:27 AM..
# 12  
Old 02-22-2013
Quote:
Originally Posted by MadeInGermany
Clear and efficient is a BEGIN section - the loop section stays small:
Code:
awk 'BEGIN {mx=0} $1>mx {mx=$1} END {print mx}'

In this case you can also do a cast to integer by a +0:
Code:
awk '$1>mx {mx=$1} END {print mx+0}'

Probably not an issue for this thread, but note that neither of these will work correctly if all of the values fed into these scripts are negative.
If you try the script I suggested with negative values, such as:
Code:
printf "%d\n" -4 -1 -2|awk 'mx==""||$1>mx{mx=$1}END{print mx}'

it still works.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 02-22-2013
Hello Don, Rudic, MadeInGermany

Thank you so much for your help

@Don - I used the snippet mentioned in your post.. it is really charming !

Regards
Chetanz
# 14  
Old 02-23-2013
Quote:
Originally Posted by Don Cragun
. . . if all of the values fed into these scripts are negative. . . .
I was thinking about sth. like initializing max to getconf INT_MINbut there may be negative REAL numbers (-1E20) so this approach wouldn't work either.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable not displaying in subject line of mailx email

Hi Newbie here with first post. I've got a shell script (ksh) whereby I run a SQL*Plus script and output the results to a file. I then check the output file in an if statement that looks like this: if ]; then export GAPNUM=`awk '{print $4}' $OUTFILE` if ] then mailx -s... (10 Replies)
Discussion started by: ltzwoman
10 Replies

2. UNIX for Dummies Questions & Answers

Displaying contents with Echo

Hello, How can i display the below ... say echo "Start \n Analyse \n ~/temp/input.txt \nStop" Now my output should look like below Start Analyse This is from input file Stop I tried echo "Start \n Analyse \n $var \nStop where $var = "~/temp/input.txt".. It did not work out..... (3 Replies)
Discussion started by: sathyaonnuix
3 Replies

3. Shell Programming and Scripting

echo the NAME of the variable

#!/bin/bash varA="AAA1" varB="BBB2" varC="CCC3" for proccx in $varA $varB $varC do echo "the current name is ????? , the value is $proccx" echo " " done #end of script I want the output to look something like this: the current name is varA, the value is AAA1 the current name is... (5 Replies)
Discussion started by: ajp7701
5 Replies

4. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

5. Shell Programming and Scripting

Variable not displaying while running in cron job

hi , While running my script individually the variable is working fine. But when i am running the same script from cron job it's displaying nothing. Can any one comment on this. (4 Replies)
Discussion started by: josephroyal
4 Replies

6. Shell Programming and Scripting

read variable after echo

Hi, i want to create an user-friendly script where you are asked for two numbers. i would like that these two number to be separated with "--" for example, but i can't figure out how to do this. for example read -p "Insert lowest and highest value: " min ; echo -n "-- "; read max so... (3 Replies)
Discussion started by: ezitoc
3 Replies

7. Shell Programming and Scripting

multiple echo statements in if condition

Hi , I have a peculiar problem. i have an if block like this if ; then echo " todays date is " ${date} >> log_file echo " file count is " $ count >> log_file mv filename1 filename 2 else echo "no files available ">> log_file fi the echo statement "no files available " is not... (2 Replies)
Discussion started by: wizardofoz
2 Replies

8. Shell Programming and Scripting

assigning variable value using echo

I am modifying an existing script and it has the following line: export SomeEnvVar=`echo ${SomeLocalVar}` Why wouldn't it just read: export SomeEnvVar=${SomeLocalVar} Is there some reason to use echo instead of a direct assignment? :confused: (2 Replies)
Discussion started by: shellburger
2 Replies

9. Shell Programming and Scripting

echo variable problem

hi I have say five variable. I would ask the user which one they want me to print and then print accordingly. TEST_1='10.2.3.4' TEST_2='11.2.3.5' TEST_3='12.2.3.5' TEST_4='13.2.3.5' TEST_5='14.2.3.5' print_var() { echo "Accessing var num $1" echo TEST$1 #??? But How do... (6 Replies)
Discussion started by: sabina
6 Replies

10. UNIX for Dummies Questions & Answers

Display from a variable using echo.

I have a variable that is outputting a lot of space. here has been 45 lines returned ... how can I remove the spaces between the "been and the 45" CODE: fil_len=`wc -l < coshb.txt` if ; then cat coshb.txt | more echo " " echo "There has been ${fil_len} lines... (4 Replies)
Discussion started by: jagannatha
4 Replies
Login or Register to Ask a Question