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 Code:
Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ? Code:
#!/bin/bash Code:
Do you want to see Swap Usage(1) or both Memory and Swap Usage(2) ? 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 Code:
Do you want the output sorted by Memory Usage(M) or Swap Usage(S)? Any help is highly appreciated. |
Quote:
Code:
if [ $VAL -eq 1 ] Code:
#!/bin/bash |
Code:
if [ $VAL -eq 1 ] |
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 Code:
echo "test" Code:
#!/bin/bash |
Quote:
|
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" Code:
# echo "test" # Comment it out, don't print |
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 |
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:
Meanwhile i am modifying my code accordingly. Thanks alot |
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. |
Thanks all for all your reposnses. I finally solved my issue using fuctions and it worked just fine. Appreciate all your inputs.
|
All times are GMT -5. The time now is 10:41 AM. |