I am writing a script and kind of stuck in a small thing. Cannot figure it out. so please help
I am logged in as root user. I want to switch user to "user1" inside the script and execute a specific command lets say "pwd" and come back where i started.
I know how to switch user, run command and come back i.e,
su - user1 -c "pwd"
I just want two things.
1). Save the output of the command given as user1 in a variable inside a script and use it somewhere else lets say for comparing etc
I have tried several things but didn't work out....
2). I do not want the output of this command to appear on screen while running the script but saving the output of the command in some variable.
#!/bin/sh
su - user1 -c "pwd" # Store the output of this command in variable "check"
if [ "/export/home" -eq $check ]
then
echo "The path is correct\n"
else
echo "The path is not correct\n"
fi
You're using the comparison operator for integers (-eq). To store the output in a variable, do something like:
#!/bin/sh
check=$(su - user1 -c "pwd") # Store the output of this command in variable "check"
if [ "$check" == "/export/home" ]
then
echo "The path is correct\n"
else
echo "The path is not correct\n"
fi
OMG i don't know why i didn't try this Thanks alot. There is another slight issue. Two extra lines always stored in variable "check". so how to remove first two lines of the variable data. following is the output when i echo "check"
Sun Microsystems Inc. SunOS 5.9 Generic May 2002
You have new mail.
/export/home/rtp99 # this is the result of pwd command
Yes you are right Franklin, but this works only for the command having 1 line output like "pwd". I have to run other commands whose output length may vary. so how do i do some operations on the variable ("check" in this case)?
Now if this value is stored in variable "check" then how would i delete the first few un-necessary lines, cut all the columns except last column and check if the value isn't greater than 500.
These type of operations i have to perform. Please suggest some way. Thanks
Well I am performing some system checks, and the system is being used in a live GSM network. The command i quoted in last is one of the checks. I've to check if the value of last column isn't greater than 500. Because if it is, then we got some serious issues regarding synchronization. and i am writing the script for these checks. And the only problem is with some commands i have to run as other user like user1.
Just point me in the right direction. Thank you very much spending your time on this.
Even if one value is > 500 in last column, i am supposed to perform some specific system diagnostics. But for now in the script, i just want the script to tell me via " echo " that one or more values are > 500. I hope you understand now what i am trying to do and what is the purpose. Thanks
I'm not shure if the format of the given output is different from your original output, do you get the values of the desired column with this command?
su user -c "YourCommand | awk '{print \$(NF-1)}'"
If you don't get the correct values try to vary the column number e.g. \$(NF-2),\$(NF-3)... and so forth.
Anyway, this should do what you want assuming the column is correct:
#!/bin/sh
check=`su user -c "YourCommand | awk 'int(\$(NF-1))>500{print \"YES\";exit}' file"`
if [ "$check" == "YES" ]
then
echo "There are one or more values > 500!!"
else
echo "Relax..."
fi