LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2014, 09:01 AM   #1
homer_3
Member
 
Registered: May 2008
Posts: 99

Rep: Reputation: 15
Executing command in bash script as a different user?


I'm running a bash script as root and want to execute a command in the script as a different user. The lines of interest in the script are below. If I copy and paste the results of the echo to my command line, the script runs as the other user. But If I run the script, the command never executes and no errors are logged to the screen.

Code:
echo "su otheruser -c '$COMMAND'"
$(su otheruser -c '$COMMAND')
I've also tried

Code:
echo "su otheruser -c '$COMMAND'"
su otheruser -c '$COMMAND'
 
Old 03-14-2014, 09:24 AM   #2
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 55
Perhaps put the path to su in your script ? (/bin/su)
 
Old 03-14-2014, 09:49 AM   #3
homer_3
Member
 
Registered: May 2008
Posts: 99

Original Poster
Rep: Reputation: 15
no luck
 
Old 03-14-2014, 09:53 AM   #4
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 55
Does it give any errors in the output of the command?
 
Old 03-14-2014, 09:58 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this is what i get:
Code:
[root@hyper ~]# cat homer.ksh 
#!/bin/bash

echo su - schneidz -c 'whoami':
su - schneidz -c 'whoami'

echo su - schneidz -c '$c':
c=whoami
su - schneidz -c '$c'

[root@hyper ~]# ./homer.ksh 
su - schneidz -c whoami:
schneidz
su - schneidz -c $c:
i think the single quotes are preventing the value of the variable from being expanded so the script is trying to run it literally (i'm surprised its not returning '$c': command not found to stderr).
 
1 members found this post helpful.
Old 03-14-2014, 09:58 AM   #6
homer_3
Member
 
Registered: May 2008
Posts: 99

Original Poster
Rep: Reputation: 15
No, I simply see the command I am trying to run echoed to the screen and then my script exits (since it's the last line of the script). When I copy paste the exact command that was echoed, it runs.

Based on schneidz's findings, this works
Code:
#!/bin/bash
X="echo hello"

su otheruser -c "$X"
Thanks.

Last edited by homer_3; 03-14-2014 at 10:32 AM.
 
Old 03-14-2014, 10:47 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by schneidz View Post
(i'm surprised its not returning '$c': command not found to stderr).
su -c runs the command with a shell, the subshell doesn't get the value of $c, so it expands to an empty string.
 
  


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
[SOLVED] Bash script: su another user and ssh in the single-command Droa Programming 9 10-13-2011 03:43 AM
bash script: how to assign command ouput to a variable without executing it? bostonantifan Programming 1 02-12-2011 11:55 PM
Check if the user is in the correct directory before executing bash script. jsullivan44 Programming 7 12-10-2010 08:27 PM
bash script: executing string command chuckj Programming 8 06-17-2010 07:22 AM
Bash script run via cron not executing MYSQL command mackstar Linux - Server 4 04-23-2009 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

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
Open Source Consulting | Domain Registration