4

I'm trying to run the following:

if [! -f .owner ]; then
    creator=$(whoami)
    sudo -u appuser ksh
    echo "$creator" >> .owner
    mkdir test1 test2 test3
    exit
fi

However, the file/folders get made by the person that runs the script. Any way to make these get created by appuser?

Edit: sudo -l returns (appuser) NOPASSWD: ALL

3
  • you can just change the owner at the end of the script. – Rabin Jul 22 '14 at 20:17
  • you could run your script using sudo and then use su inside your script – Kiwy Jul 23 '14 at 8:20
  • Users running this script won't be super users, the appuser account itself is also not a super user. Chown isn't available to unprivileged users. The purpose for this script is that the appuser creates a whole bunch of files when it first gets ran (hence the need to track the person that called the job). The folder these files are located in have read-only access to their common group. – charles Jul 23 '14 at 13:08
7

Try:

    #!/bin/bash
    id
    touch script-run-user.file

    sudo -u appuser 'ksh' <<EOF
    # add list of cmds to execute
    id
    touch appuser.file 
    EOF

Edit: Just as an update, check out Here Documents. EOF = "End Of File", the name is arbitrary.

1
  • This is correct - but this is just a note that the reason this works and the other didn't is that the >> means the shell creates the file - and its process is owned by the user invoking the script so files it creates are owned by that user, but here you change users then invoke touch - which also creates files owned by the user invoking it. – mikeserv Jul 22 '14 at 21:06

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.