Save the output of the command run as other user

Dear All,

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

Thanks

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

Hi,

Yes i did that. but this happened.

./myscript: syntax error at line 2: `check=$' unexpected

Try backticks:

check=`su - user1 -c "pwd"`

Hi,

OMG i don't know why i didn't try this :slight_smile: 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
check=`su - user1 -c "pwd" | tail -1`

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)?

What operations? You could use awk instead of tail to achieve that.

For example i have a command whose output is like this:

# PARTITION | MASTER_ROLE |STATE_M|STATE_S|MASTER_ATOMICID | SLAVE_ATOMICID | DIFF_ATOMICID |
#-----------|-------------|-------|-------|----------------|----------------|---------------|
 acc1  |     PRIMARY |VALID  |VALID  |     1718408937 |     1718408861 |            76 |
 acc2  |     PRIMARY |VALID  |VALID  |     1718408937 |     1718408861 |            76 |
 acc3  |     PRIMARY |VALID  |VALID  |     1718408937 |     1718408861 |            76 |
 acc4  |     PRIMARY |VALID  |VALID  |     1718408937 |     1718408861 |            76 |
 acc5  |     PRIMARY |VALID  |VALID  |     1701569594 |     1701569423 |           171 |
 acc6  |     PRIMARY |VALID  |VALID  |     1701569594 |     1701569423 |           171 |
 acc7  |     PRIMARY |VALID  |VALID  |     1701569594 |     1701569423 |           171 |
 acc8  |     PRIMARY |VALID  |VALID  |     1701569594 |     1701569423 |           171 |
 acc9  |     PRIMARY |VALID  |VALID  |     1749603110 |     1749603038 |            72 |

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

Do you want all the selected values of the last column in your variable in this case?

Yes, and check if any value from the last column is greater than 500.

Can you explain what you're trying to achieve with your script instead of asking how to use which command?

There could be another (better) approach to achieve your task.

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.

And what to do if you have more then one value > 500? In that case you'll get several lines.

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

:slight_smile: Thanks alot buddy. I hope it would work.