Shell sIs there something special I need to do when using sudo in a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell sIs there something special I need to do when using sudo in a script?
# 1  
Old 09-03-2014
Shell sIs there something special I need to do when using sudo in a script?

I have a script in which I used "sudo -s" I notice some extremely strange behavior when executing this script. To investigate this I decided to recreate the problem in the following script. I notice that "sudo -s" is only being executed one time. Soon after completely falls apart. Is there something special I need to do to get this to work?

Code:
     1  #! /bin/sh
     2  
     3  echo "test"
     4  echo "the script is working"
     5  
     6  sudo -s 
     7  
     8  echo "reading script after first sudo"
     9  sudo -s
    10  echo " reading script after second sudo"
    11  
    12  echo "the script is still working" 
    13  echo " still working"

Produces the following results...

Code:
./btest
test
the script is working
#
!!!note script stops here. first sudo worked!!!!

# 2  
Old 09-03-2014
'sudo -s' does not mean "run the lines after this in a new shell". It means "run a completely new, independent shell with root permissions".

It works in terminal because the shell runs text from standard-input, so ends up reading the same thing even after the new shell is launched. Scripts are not fed into standard input, so the new subshell doesn't share the same input lines.

Try this:

Code:
/bin/bash < myscript.sh

That feeds it into stdin, causing the sharing you expected.

Generally though, if you want to run a script as sudo, it's sudo script, not a script containing sudo.

Last edited by Corona688; 09-03-2014 at 04:02 PM..
# 3  
Old 09-03-2014
Quote:
Originally Posted by Corona688
'sudo -s' does not mean "run the lines after this in a new shell". It means "run a completely new, independent shell with root permissions".

It works in terminal because the shell runs text from standard-input, so ends up reading the same thing even after the new shell is launched. Scripts are not fed into standard input, so the new subshell doesn't share the same input lines.

Try this:

Code:
/bin/bash < myscript.sh

That feeds it into stdin, causing the sharing you expected.



Generally though, if you want to run a script as sudo, it's sudo script, not a script containing sudo.




Hi, thanks for the response. I think I am barking up the wrong tree here. sudo does not seem to be the way to go. Maybe I would be better off changing the permissions of my script so that it runs as root.
# 4  
Old 09-03-2014
Quote:
Originally Posted by busi386
Hi, thanks for the response. I think I am barking up the wrong tree here. sudo does not seem to be the way to go. Maybe I would be better off changing the permissions of my script so that it runs as root.
Scripts don't work that way either. setuid doesn't work on scripts on many systems, or works in unexpected ways.

Maybe if you explained what you were actually trying to do, instead of showing us broken programs that don't do what you want to do, we'd understand what you wanted to do.
# 5  
Old 09-03-2014
How about this? A script that attempts to run itself with sudo unless it's root already. $0 is a special variable meaning "the file this script was run on", "$@" is a special variable meaning "all arguments". UID is a special variable which is set to the current user ID, which would be 0 if the script was running as root.

So if the UID isn't 0, it runs "sudo ./myscript.sh "given" "arguments". The exec causes it to replace itself so the 'exit 1' after it is redundant, just there in case the 'exec' fails for some reason.

Code:
#!/bin/sh

if [ "$UID" -ne 0 ]
then
        if [ ! -f "$0" ]
        then
                echo "Can't find '$0', can't elevate privileges" >&2
                exit 1
        fi

        echo "Attempting to run '$0' with sudo" >&2

        exec sudo "$0" "$@"
        exit 1
fi

echo "This part should run as root"
echo "UID is $UID"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Require help in creating a Sudo/Shell script

Hello Friends, I have a scenario to create a script, I know many of you feel this as simple script. I am not much familiar with unix scripting, please help me out. Situation:- 1. I have a list of config files like 40+ would be getting deployed in the /app/abcd/src/Config/ (This will... (2 Replies)
Discussion started by: ganjvin
2 Replies

2. Shell Programming and Scripting

Shell script question in special character

when I execute the unix commands its works normally in the 1st part. When I the tried the same in shell scripting the directory is not displayed in 2nd part example. please let me know what needs to be done. Unix : client=~zsvdbs echo $client /shome/zsvhome/zsvdbs Using... (3 Replies)
Discussion started by: keerthi2016
3 Replies

3. Shell Programming and Scripting

shell script is giving different resultControlled Special Account [csdwmast@ucdoud01.am.sony.com

Hi All, In this below script i am not able to find where is "form mail id" is taking when this script is executing i am getting "Controlled Special Account " as from ,i need to send "form mail id " from a table.:mad: kindly help me plzz. Regards, Krupa (0 Replies)
Discussion started by: krupasindhu18
0 Replies

4. Shell Programming and Scripting

Help with Shell Script on sudo

I want to execute a script(generateReport.sh) which resides on root home directory from shell script(localtrigger.sh) as root by using sudo. The thing is i cant edit /etc/sudoers (i can but m not allowed to do it , ethically) i cant change ownership of generateReport.sh script When i try... (7 Replies)
Discussion started by: saket
7 Replies

5. Shell Programming and Scripting

sudo su - user in a shell script

Normally i would google, but I did not know how to google the problem I am facing now also being a newbie in shell scripting. Okay, the requirement is user1 has sudo rule to su - user2(NO PASSWORD) and user2 has will be able to sudo certain commands so following works fine from command prompt... (2 Replies)
Discussion started by: beEnthu
2 Replies

6. Shell Programming and Scripting

Use arrow touch in a script shell without special character

Hello, I have a problem when i execute the script underneath. If i tape azerty 123 and i use the arrow touch, in the file /tmp/test i have the caracter #!/usr/bin/ksh clear echo "Taper l adresse IP de la partition a creer :" tput cup 1 48 read Adress echo $Adress echo "${Adress}" >>... (0 Replies)
Discussion started by: khalidou13
0 Replies

7. Shell Programming and Scripting

shell script problem , sudo mount command

cat test.sh sudo mount -t vfat /dev/sda7 /media/Ddrive If i double click the test.sh file and select run in terminal then the terminal prompts for password. How can i avoid typing password? Or if i double click test.sh file and select run then nothing happens. What i'm trying "Double... (3 Replies)
Discussion started by: cola
3 Replies

8. UNIX for Dummies Questions & Answers

Need help to escape special characters in Korn shell script

Hi, I would like to display the following message from my shell (Korn) script Copy "old_file.txt" to "new_file.txt" My code looks as follows print "Copy "old_file.txt" to "new_file.txt"" However, when I execute the script, I get the following output Copy old_file.txt to... (6 Replies)
Discussion started by: rogers42
6 Replies

9. Shell Programming and Scripting

Special Character Check in Shell script

Hi, I'm currently working on a project that requires parsing xml file. One of the field in the xml is shown below (don't remember exactly): <variable="ITEM">12345678</variable> I coded my script keeping in mind that the value denoted in bold will always be a number. After getting just the... (1 Reply)
Discussion started by: mradul_kaushik
1 Replies

10. UNIX for Dummies Questions & Answers

sudo in OS X shell script without password prompt??

I've written a shell script to alter a particular preference file on OS X (10.3.9), which works fine (tested by running the script from the terminal sat in front of the box). Problem is, I now have to run this script remotely across a number of machines via remote desktop, so where I've used the... (1 Reply)
Discussion started by: Brad_GNET
1 Replies
Login or Register to Ask a Question