LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-04-2010, 10:15 PM   #1
wahming
Member
 
Registered: Jan 2007
Posts: 133

Rep: Reputation: 15
Bash Scripting Question


I'm writing a script that relaunches itself with root permissions. It starts like this:

#!/bin/sh

CMDLN_ARGS="$@"
if [ $UID -ne 0 ]; then
echo 'Please enter root password'
echo su root -c "$0 $CMDLN_ARGS"
echo su root -c "$0 $@"
exec su root -c "$0 $CMDLN_ARGS"
fi

In this case, it works fine. However, if I replace $CMDLN_ARGS in the exec line with $@, it breaks for multiple input arguments. But the echo statements tell me that both lines should be identical. What gives? I can get my script to work, but I'd really like to understand the issue here.
 
Old 10-05-2010, 01:16 AM   #2
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
The arguments passed to the script needs to be passed using double quotes:
Code:
scriptname.sh "/bin/ls -ltr"
http://www.geocities.ws/murugesan/te...g/passArg.html

Last edited by murugesan; 10-05-2010 at 01:17 AM. Reason: Edited for using [code] and [url]
 
Old 10-05-2010, 03:55 AM   #3
wahming
Member
 
Registered: Jan 2007
Posts: 133

Original Poster
Rep: Reputation: 15
Thanks. But assuming I want the script to handle it gracefully, instead of the user modifying his input? I've tried quoting my input \"$@\" in script but it doesn't work.
 
Old 10-05-2010, 04:01 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
if it's bash it should be like this:
Code:
#!/bin/bash

CMDLN_ARGS=("$@")
if [[ $UID -ne 0 ]]; then
    echo 'Please enter root password'
    echo su root -c "$0" "${CMDLN_ARGS[@]}"
    echo su root -c "$0" "$@"
    exec su root -c "$0" "${CMDLN_ARGS[@]}"
fi
You might also find -n useful:
Code:
    echo -n 'Please enter root password: '
 
Old 10-05-2010, 04:31 AM   #5
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Updated code using $@:

Using ksh (USEksh.sh)
Code:
#!/bin/ksh
CMDLN_ARGS="$@"
if [[ $UID -ne 0 ]]; then
    echo -n 'Please enter root password'
    echo su root -c "$0" "${CMDLN_ARGS[@]}"
    echo su root -c "$0" "$@"
    exec su root -c "$0 $@"
else
        echo  You cab execute: $@ : if required
        #$@
fi
Using bash(USEbash.sh)
Code:
#!/bin/bash
CMDLN_ARGS="$@"
if [[ $UID -ne 0 ]]; then
    echo -n 'Please enter root password'
    echo su root -c "$0" "${CMDLN_ARGS[@]}"
    echo su root -c "$0" "$@"
    exec su root -c "$0 $@"
else
        echo  You cab execute: $@ : if required
        #$@
fi
Example usage:
Code:
USEksh.sh ls -latr
USEbash.sh ls -latr
http://www.geocities.ws/murugesan/te...g/passArg.html
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting question rmarkin Linux - General 3 11-13-2008 01:52 PM
Bash Scripting Question fiod Linux - Newbie 4 11-19-2005 05:09 AM
bash scripting question bmfan Linux - Software 6 11-04-2005 08:54 PM
A Bash Scripting Question steve_f60 Linux - Newbie 4 05-26-2005 06:57 PM
BASH Scripting question rootking Linux - Software 14 12-15-2004 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:22 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