LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Running A Single Command In A Script As A Different User (https://www.linuxquestions.org/questions/slackware-14/running-a-single-command-in-a-script-as-a-different-user-649462/)

Woodsman 06-15-2008 01:11 PM

Running A Single Command In A Script As A Different User
 
I'm running a script as root. Within that script I want to run a single command as the person logged in, which usually is not root. The single command slaps a conky display item on the user's desktop.

The problem is the conky braces and parentheses that need to be passed as actual characters and not shell symbols. I've unsuccessfully tried various methods of backslashes and the like to properly pass the command.

In the script I want to run:

Code:

su - `logname` -c conky -t '${color green}'$DRIVEMODEL'('${BACKUPDISK:5:3}') Temp:${color white} ${hddtemp '$BACKUPDISK'}' -x 280 -y 297 &>/dev/null
Where:
DRIVEMODEL=WDK7500AAKS
BACKUPDISK=/dev/sdb


When I try running the complete command I receive "bad substitution" error messages. The error message makes sense to me and I understand what is happening. Yet I am struggling with the syntax I need to run the complete command.

By itself the conky command runs fine from the command line or from within my script, but not in conjunction with the su command.

Of course, I cannot split the su and conky command because the moment I su as a different user, the script terminates. I need to run both commands together.

The challenge is the su command requires the text following the -c option be enclosed in single or double quotation marks, otherwise the su command stops reading the text after the first space. When I enclose the entire conky portion in double quotation marks, I receive the shell error messages.

I appreciate help. As always, thanks.

MQMan 06-15-2008 03:03 PM

Try using sudo instead of su. I had some strange issues trying to use su inside a script.

Cheers.

Woodsman 06-16-2008 12:15 PM

Thanks for the idea. Originally I had tried sudo but that did not succeed, for whatever reasons my mind currently cannot remember. :scratch:

While using the sudo command might provide a better long-term solution, which I'll again investigate, I still would appreciate help from anybody with learning how to properly format the su command in my script to pass the conky directive. Thanks again.

mRgOBLIN 06-16-2008 04:31 PM

Not having conky to test with...

Can you enclose the command in the usual "" and just backslash escape the single quotes?

Not sure if it will work but worth a try.

shadowsnipes 06-16-2008 06:08 PM

If nothing else you can put that long conky command in a small script that you simply execute with su.


All times are GMT -5. The time now is 07:31 AM.