if condition and sudo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if condition and sudo
# 1  
Old 10-16-2012
if condition and sudo

Hello, I have a regular user account that needs to run a script as a cron job. This script is to check whether or not a file exists, and if so, perform an action on that file using sudo (the file is owned by root). The problem is that the if statement fails, because the user does not have read permissions on the folder.
Code:
if [ -f $SOMEFILE ]; then
     sudo cp $SOMEFILE /somewhere/else/
else
  echo $SOMEFILE" not found!"
fi

The "sudo cp" command, when run on its own, works just fine. But the "if -f" condition fails because the user cannot access the folder where $SOMEFILE lives, in order to check if it exists or not.

Is there some way to use sudo in conjunction with the "if -f" condition? Or is there some other command or method I use use with sudo to check for the existence of this file?

Thank you

Last edited by Scott; 10-17-2012 at 02:08 PM.. Reason: Code tags
# 2  
Old 10-16-2012
Remove sudo from the script, include the script on the allowed sudo commands. run the script as:

Code:
sudo script

# 3  
Old 10-16-2012
You could also do:

Code:
sudo sh -c "[ -f $SOMEFILE ] && cp $SOMEFILE /somewhere/else" || echo "$SOMEFILE" not found!

Problem is this may report "not found" error if the copy fails for some reason (out of diskspace or /somewhere/else is and invalid path).

A better solution is to make another script to copy/report missing $SOMEFILE and run this as root from your cron script.
# 4  
Old 10-17-2012
Thanks for the ideas, I ended up solving it this way:

Code:
if sudo test -f $SOMEFILE; then
  sudo cp $SOMEFILE /other/place/
else
  echo $SOMEFILE" not found!"
fi

So "sudo test" works for letting regular users check on files that they normally wouldn't have permission to.

Last edited by Scott; 10-17-2012 at 02:09 PM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi, Have a need to run the below command as a "karuser" from a java class which will is running as "root" user. When we are trying to run the below command from java code getting the below error. Command: sudo -u karuser -s /bin/bash /bank/karunix/bin/build_cycles.sh Error: sudo: sorry,... (8 Replies)
Discussion started by: Satyak
8 Replies

3. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi All, I running a unix command using sudo option inside shell script. Its working well. But in crontab the same command is not working and its throwing "sudo: sorry, you must have a tty to run sudo". I do not have root permission to add or change settings for my userid. I can not even ask... (9 Replies)
Discussion started by: Apple1221
9 Replies

4. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

5. Shell Programming and Scripting

ssh foo.com sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ foo.com:path/on/remote/machine/ ssh foo.com sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

6. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

7. AIX

sudo log and sudo auditing

Sudo In AIX, how to find out what commands have been run after a user sudo to another user? for example, user sam run 'sudo -u robert ksh' then run some commands, how can I (as root) find what commands have been run? sudo.log only contains sudo event, no activity logging. (3 Replies)
Discussion started by: jalite19
3 Replies

8. Cybersecurity

sudo /bin/sh or sudo su -

we are looking at changing the way we get root on our network. in our current system if an admin needs root access he just gets the root password and uses an su. some of our staff have decided that a sudo to "/bin/sh" will be easer. some of our staff think a sudo to "su -" will be better. I... (0 Replies)
Discussion started by: robsonde
0 Replies

9. UNIX for Dummies Questions & Answers

Unable to use the Sudo command. "0509-130 Symbol resolution failed for sudo because:"

Hi! I'm very new to unix, so please keep that in mind with the level of language used if you choose to help :D Thanks! When attempting to use sudo on and AIX machine with oslevel 5.1.0.0, I get the following error: exec(): 0509-036 Cannot load program sudo because of the following errors:... (1 Reply)
Discussion started by: Chloe123
1 Replies
Login or Register to Ask a Question