Save the output of the command run as other user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save the output of the command run as other user
# 1  
Old 02-13-2010
Data 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,
Code:
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.

Code:
#!/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

Last edited by Franklin52; 02-13-2010 at 09:12 AM.. Reason: Please use code tags!
# 2  
Old 02-13-2010
You're using the comparison operator for integers (-eq). To store the output in a variable, do something like:
Code:
#!/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

# 3  
Old 02-13-2010
Quote:
Originally Posted by Franklin52
You're using the comparison operator for integers (-eq). To store the output in a variable, do something like:
Code:
#!/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.

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

# 4  
Old 02-13-2010
Try backticks:

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

# 5  
Old 02-13-2010
Quote:
Originally Posted by Franklin52
Try backticks:

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

Hi,

OMG i don't know why i didn't try this Smilie 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"

Code:
Sun Microsystems Inc.     SunOS 5.9       Generic May 2002
You have new mail.
/export/home/rtp99      # this is the result of pwd command

# 6  
Old 02-13-2010
Quote:
Originally Posted by suhail.sadaqat
Hi,

OMG i don't know why i didn't try this Smilie 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"

Code:
Sun Microsystems Inc.     SunOS 5.9       Generic May 2002
You have new mail.
/export/home/rtp99      # this is the result of pwd command

Code:
check=`su - user1 -c "pwd" | tail -1`

# 7  
Old 02-13-2010
Quote:
Originally Posted by Franklin52
Code:
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)?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies

2. Shell Programming and Scripting

How to execute command remotely as sudo and save the output locally?

Hello , I am trying to run a NetBackup command in remote server. Also this command can only be run by root so I am using sudo . Also I want the output of the command locally in a file. The below command asked for password , ran successfully and showed Output on my local server screen ... (2 Replies)
Discussion started by: rahul2662
2 Replies

3. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

4. Shell Programming and Scripting

Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I... (13 Replies)
Discussion started by: zubairom
13 Replies

5. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

6. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

7. UNIX for Dummies Questions & Answers

how to save the output of command in tcl/expect

hi, everyone: I just wonder how to save the output of command, I mean everything, save as a string into a variable. another question is I try to ls the details of a directory, but it works in the shell, not in the script. for example code: ls -ltr *se100* | grep ^- | tail -1 | awk '... (1 Reply)
Discussion started by: allenxiao7
1 Replies

8. Shell Programming and Scripting

to save output of a command in hash variable

Hi all, is it possible to save the output of a unix command executed in perl to be saved in hash variable.. like i have the command `find $mypath ! -user mainuser -printf \"\%u \%h\\n\"`; this will print all the users other than mainuser with their paths. so is possible to capture... (2 Replies)
Discussion started by: saapa
2 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. Shell Programming and Scripting

How to run a command with some other user id

Hi Say I am running a script using my user id csaha. How can I run any specific command in the same sctipr using any other user id (say root). Definitely I have the password of root. Any idea how the same can be achieved ??? Example: I need to run a script using my id (csaha) only on... (4 Replies)
Discussion started by: csaha
4 Replies
Login or Register to Ask a Question