2

I'm extremely lazy, and prefer to write scripts to automate compiling software. To me, as these scripts can get quite long, it's easier to run them as root but have the individual software compile as an unprivledged user (thus sudo -u). I've been able to do this in the past, but am hitting a problem now in that the conditionals aren't being evaluated. Thus, something like:

sudo -u name if [ ! -f $HOME/file ];then echo "file not found";fi

chokes on me ... right at the then. How can I fix this?

  • 2
    What's the point of using sudo for the conditionals? Use it only for the compilation step. Ideally, you should do it the other way around and run the script as a regular user and use sudo only for the make install. – terdon Oct 24 '15 at 14:42
  • Instead of writing multiple scripts in order to compile something, write one Makefile and use make to build the target. – ott-- Oct 24 '15 at 20:35
  • 1
    I agree with @terdon, not a good practice. Not good to be running around the system with your sword unsheathed. – RobertL Oct 24 '15 at 23:14
  • I'm not using multiple scripts, just one to compile multiple programs (and often cross compile the same programs). As I have many such programs to compile, the script saves an enormous amount of time and energy. I just don't want to compile as root ... sudo -u user takes care of that for me :) – Thomas Douglas Oct 25 '15 at 14:47
1

You can do:

sudo -H -u name bash -c '[ -f "$HOME"/file ] || echo "file not found"'

[ -f "$HOME"/file ] || echo "file not found" does exactly what you are trying to do with if..then.

From man sudo:

-H, --set-home

Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database entry. Depending on the policy, this may be the default behavior.

  • Sadly, none of the suggestions have worked. Finally found an example of the scripts I'd written long ago, and I'd simply not run sudo -u with the conditionals at all. Suppose it's not needed ... they're just being used for testing. Works fine for compiling tho: – Thomas Douglas Oct 24 '15 at 14:17
  • @ThomasDouglas: I believe that the above command works.  What happens when you try it? – G-Man Says 'Reinstate Monica' Oct 24 '15 at 14:39
  • I'm sure it works, but it's not what I'm doing with the script :( The idea is to compile, test and install software and write messages to a log file for debugging purposes. The code snippet I originally gave was strictly a simplistic example. The code provided above attempts to bypass the conditional via bash internals, which would get pointlessly confusing in a large and complex shell script :( Thanks for trying, but I'm going to have to just run the conditionals normally within the scripts and keep sudo -u for actual compile and testing. – Thomas Douglas Oct 25 '15 at 15:01
0

Your current shell sees the semicolon and ends the sudo command there; thus, sudo only sees if [ ! -f $HOME/file ]. Suggestions:

  1. Escape the special characters, or
  2. quote the entire sudo command (untested), or
  3. put the commands in a (temporary, if desired) shell script and point sudo at that.
  • Trying to use sudo within a shell script is a pain in the arse ... you either have to give the script a no password permission (which, to me, is a big no-no) or sit there putting in passwords all the time (which defeats the entire point). Running the script as root and using sudo -u solves the problem nicely, as the compilation is done as a non privledged user and no extra passwords need be input :) Don't know what the hell I was thinking trying to use sudo with routine conditionals, but it would be nice to solve the problem. Oh well, can't have everything I suppose! Thanks for trying tho – Thomas Douglas Oct 24 '15 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.