LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 11:02 AM   #1
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Rep: Reputation: 9
echo not being execute under if in bash script


Hi Friends,

I am writing the following shell script but for some reason i am not able to get one particular thing as desired. I am digging on this from last 3-4 hours but i am not able to understand. Please note i am pretty new to scripting so if i have made some basic mistake or missed some very basic thing forgive me for that and do correct me. below is my script


Code:
#!/bin/bash

clear
echo -e "Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?"
read VAL
 
for i in /proc/* 
do
 
        if [ -e $i/smaps ]
        then    
                COM=`cat $i/cmdline`
                PID=`basename $i`
                if [ $VAL = 1 ]
                        then    
echo "test"     
SR="6"
echo "$SR"
                        awk  -v CMD="$COM" -v J="$PID" '/Rss/ {rss +=$2}; END { if (rss > 0 )print "PID: "J"  Process_Name  " CMD "  Rss= " rss/1024"M"}' $i/smaps 
                else
                        echo "fail"
                fi              
        fi              
#done
done 2> /dev/null | sort -nr -k  6| head -10
Output for above script is

Code:
Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?
1
PID: 4130  Process_Name  /usr/bin/Xorg:0-br-audit0-auth/var/gdm/:0.Xauth-nolistentcpvt7  Swap= 46.5938M
PID: 4334  Process_Name  /usr/bin/python-tt/usr/bin/puplet  Swap= 20.3008M
PID: 4293  Process_Name  /usr/lib/vmware-tools/sbin32/vmtoolsd-nvmusr--blockFd3  Swap= 17.9844M
PID: 4432  Process_Name  gnome-terminal  Swap= 16.8359M
PID: 4272  Process_Name  nautilus--no-default-window--sm-client-iddefault3  Swap= 14.8984M
PID: 29656  Process_Name  bash  Swap= 13.4688M
PID: 4270  Process_Name  gnome-panel--sm-client-iddefault2  Swap= 11.6562M
PID: 4393  Process_Name  /usr/libexec/mixer_applet2--oaf-activate-iid=OAFIID:GNOME_MixerApplet_Factory--oaf-ior-fd=29  Swap= 11.0352M
PID: 4365  Process_Name  /usr/libexec/wnck-applet--oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory--oaf-ior-fd=19  Swap= 10.2773M
PID: 3402  Process_Name  /usr/sbin/restorecond  Swap= 10.1719M
Here is if i make little change to this

Code:
#!/bin/bash
 
clear
echo -e "Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?"
read VAL
 
for i in /proc/* 
do
 
        if [ -e $i/smaps ]
        then    
                COM=`cat $i/cmdline`
                PID=`basename $i`
                if [ $VAL = 1 ]
                        then    
						echo "test"  
						SR="6"
						echo "$SR"
                        awk  -v CMD="$COM" -v J="$PID" '/Rss/ {rss +=$2}; END { if (rss > 0 )print "PID: "J"  Process_Name  " CMD "  Rss= " rss/1024"M"}' $i/smaps 
                else
                        echo "fail"
                fi              
        fi              
done 2> /dev/null | sort -nr -k  `echo SR`| head -10
Below is the output

Code:
Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?
1
sort: option requires an argument -- k
Try `sort --help' for more information.

I hope by now you should have understood what is the unexpected result i am talking about, in both the cases "echo" command above "awk" didn't execute, the behavior which i am not able to understand.

However below is a little variation of the same script where the whole thing seems to works just fine.

Code:
#!/bin/bash

clear
echo -e "\n Do you want the output sorted by Memory Usage(M) or Swap Usage(S)?"
read SRT

if [ $SRT = "M" ] || [ $SRT = "m" ]
then
        SRT="6"
elif [ $SRT = "S" ] || [ $SRT = "s" ]
then
        SRT="8"
else
        echo -e "You enetered invalid response; default sort would be Memory based !! \n\n"
        SRT="6"
fi
for i in /proc/* 
do  
COM=`cat $i/cmdline`
PID=`basename $i`
awk  -v CMD="$COM" -v J="$PID" '/Size/ {mem +=$2} /Swap/ {swap +=$2}; END { if (mem > 0)print "PID: "J"  Process_Name  " CMD "   Memory= " mem/1024 "M" "  Swap= " swap/1024"M"}' $i/smaps 
done 2> /dev/null | sort -nr -k `echo $SRT`| head -10
And here is the output

Code:
 Do you want the output sorted by Memory Usage(M) or Swap Usage(S)?
s
PID: 3830  Process_Name  sendmail: accepting connections   Memory= 9.01953M  Swap= 0M
PID: 3958  Process_Name  avahi-daemon: chroot helper   Memory= 2.53906M  Swap= 0M
PID: 3957  Process_Name  avahi-daemon: running [localhost.local]   Memory= 2.53906M  Swap= 0M
PID: 3663  Process_Name  hald-addon-storage: polling /dev/hdc   Memory= 1.92969M  Swap= 0M
PID: self  Process_Name  cat/proc/self/cmdline   Memory= 4.12891M  Swap= 0M
PID: 592  Process_Name  /sbin/udevd-d   Memory= 2.84766M  Swap= 0M
PID: 5729  Process_Name  bash   Memory= 4.5625M  Swap= 0M
PID: 4462  Process_Name  gnome-screensaver   Memory= 16.0469M  Swap= 0M
PID: 4447  Process_Name  bash   Memory= 4.5625M  Swap= 0M
PID: 4446  Process_Name  gnome-pty-helper   Memory= 2.42578M  Swap= 0M

Any help is highly appreciated.
 
Old 02-11-2013, 11:23 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
if [ $VAL = 1 ]
Instead of this, use numaric comparision, (check man test):
Code:
if [ $VAL -eq 1 ]
Also once add debugging in the script, to check where the problem is:
Code:
#!/bin/bash
set -xv
 
Old 02-11-2013, 11:26 AM   #3
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Code:
if [ $VAL -eq 1 ]
if that is the problem then how awk executes below the same if statement ? And to rule out any possibility i tried it the one you just mentioned but it didn't work; same result as before no change at all !!

Last edited by Rohit_4739; 02-11-2013 at 11:38 AM.
 
Old 02-11-2013, 12:31 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
I didn't mention that if condition is wrong, but I mentioned that you should use correct expression.

Well, problem is with following statement:
Code:
done 2> /dev/null | sort -nr -k  6| head -10
You're sorting script output on basis on 6th column of output, but there will be only one column in output of following lines while printing, so output do not print test and 6.
Code:
echo "test"     
SR="6"
echo "$SR
So just remove the sort and head part as follow, and try again:
Code:
#!/bin/bash
clear
echo -e "Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?"
read VAL

for i in /proc/* 
do
if [ -e $i/smaps ]
then    
COM=`cat $i/cmdline`
PID=`basename $i`
if [ $VAL -eq 1 ]
then    
echo "test"     
SR="6"
echo "$SR"
awk  -v CMD="$COM" -v J="$PID" '/Rss/ {rss +=$2}; END { if (rss > 0 )print "PID: "J"  Process_Name  " CMD "  Rss= " rss/1024"M"}' $i/smaps 2> /dev/null
else
echo "fail"
fi              
fi              
done

Last edited by shivaa; 02-11-2013 at 12:44 PM.
 
Old 02-11-2013, 12:36 PM   #5
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by shivaa View Post
I didn't mention that if condition is wrong, but I mentioned that you should use correct expression.

Well, problem is with following statement:
Code:
done 2> /dev/null | sort -nr -k  6| head -10
You're sorting script output on basis on 6th column of output, but there will be only one column in output of following lines while printing, so output do not print test and 6.
Code:
echo "test"     
SR="6"
echo "$SR
So remove the sort and head part as follow try again:
Code:
#!/bin/bash
clear
echo -e "Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ?"
read VAL

for i in /proc/* 
do
if [ -e $i/smaps ]
then    
COM=`cat $i/cmdline`
PID=`basename $i`
if [ $VAL -eq 1 ]
then    
echo "test"     
SR="6"
echo "$SR"
awk  -v CMD="$COM" -v J="$PID" '/Rss/ {rss +=$2}; END { if (rss > 0 )print "PID: "J"  Process_Name  " CMD "  Rss= " rss/1024"M"}' $i/smaps 2> /dev/null
else
echo "fail"
fi              
fi              
done
Thanks !!, ok agreed with this explanation but i still see variable SR not getting set, i have requirement of sort and i want to use value of this $SR in the sort expression but that doesn't seem to get set at all.
 
Old 02-11-2013, 12:47 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
I can't see the use of SR variable in your script. You have just declared and printed it, but not used.
Code:
SR="6"
echo "$SR"
If you have to do sorting, then better put the output in some temporary file and then do sorting, as:

Code:
# echo "test"    # Comment it out, don't print
SR=6
# echo "$SR"     # Comment it out, don't print
awk  -v CMD="$COM" -v J="$PID" '/Rss/ {rss +=$2}; END { if (rss > 0 )print "PID: "J"  Process_Name  " CMD "  Rss= " rss/1024"M"}' $i/smaps > /tmp/output.txt 2>/dev/null

sort -nr -k $SR /tmp/output.txt | head -10   # Add this line

Last edited by shivaa; 02-11-2013 at 12:53 PM. Reason: Code added
 
Old 02-11-2013, 01:07 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Well the usual story would probably be, why reinvent the wheel as something like top probably already does this for you.

However, the answer to the original question is, your first choice to use SR in the output from the for loop does not work for 2 reasons:

1. If you look at the example you showed where it works just fine for the SRT value, you will notice it is set outside the loop so the value exists outside the for loop

2. When you called it you did not place the $ sign in front of the variable name and hence it should just echo the letters "SR"

Another nit pick would be that the use of echo in this instance is redundant as the variable can be used as is, hence, assuming you set it correctly you could do:
Code:
done 2> /dev/null | sort -nr -k  `echo SR`| head -10

# becomes

done 2> /dev/null | sort -nr -k $SR | head -10
 
Old 02-11-2013, 10:43 PM   #8
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Thanks Grail for the explaination. I have some questions here, please forgive me if they sound silly but considering i am pretty new to scripting i hope you would answer them gentely

Quote:
1. If you look at the example you showed where it works just fine for the SRT value, you will notice it is set outside the loop so the value exists outside the for loop
So does that mean we can't declare a variable inside loop in scripting unlike a programming language. Or if we have to set the value of a variable like SR in my case, how would i do it; like say i want to set value of a variable inside a loop.

Meanwhile i am modifying my code accordingly. Thanks alot

Last edited by Rohit_4739; 02-11-2013 at 11:30 PM.
 
Old 02-12-2013, 03:34 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
The issue you face here is that the variable does not survive the pipes you are passing through. I would also point out that in this current example it
does not seem to serve any purpose to set this variable inside the loop.

Variables can exist and be updated in a loop and be used outside but as indicated, changing this one inside serves no real purpose and I see no reason why you would not simply
set it outside.

You may also wish to look at ** expansion or using find to feed a while loop as opposed to the current for loop scenario.
 
Old 02-12-2013, 06:55 AM   #10
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Original Poster
Rep: Reputation: 9
Thanks all for all your reposnses. I finally solved my issue using fuctions and it worked just fine. Appreciate all your inputs.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
echo commands in BASH script to STDOUT tospo Programming 9 11-03-2010 11:12 AM
Echo cells from a spreadsheet in BASH script General Linux - Software 3 08-05-2009 07:59 PM
How to make DOS' echo on in bash script? Dims Linux - Newbie 2 06-01-2009 02:09 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 01:08 AM


All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration