简体   繁体   中英

Bash need root to user. In same script or separate script?

Scripting newb here, working on a bash script requiring root, user, root. I run it after su in user terminal. Initially tried:

su -c - "$USER" "export PKGDEST=/tmp/test/AUR/" 
su -c - "$USER" "pacaur --noconfirm --noedit -cm" "$(< /tmp/test/aur)"

Crashes second line regardless, different errors, depending quote combination, placement. Running the proceeding code, the second line produced the following error after reading the file /tmp/test/aur :: no targets specified (use -h for help).

pacaur --noconfirm --noedit -cm "$(< /tmp/test/aur)"

Runs fine when starting script as user. My first choice would be getting this method to work properly.

Google, etc.. said root, user, root in bash is bad, so tried break it into 3 parts:

1) The first script runs as root. The problem centers here, can't get it to start the second.

2) Second script runs as user, starts 3rd script as root with: sudo /bin/bash "$L"/copyaur.sh

3) Third script, copyaur.sh starts, runs, moving some packages but doesn't show in the terminal because calls new shell.

Below was ran as su in a user terminal during testing. Prefer using the $USER var as whoami reports root whereas echo $USER reports username. After switching su, then su - $USER, ls in ~ shows my home contents.

[dom@arch testing]$ echo $USER

dom
[root@arch testing]# echo $USER

dom
[root@arch testing]# pwd
/home/dom/scripts/testing


su -       $USER  home/$USER/scripts/testing/buildaur.sh  - No such file or directory
su --login $USER  home/$USER/scripts/testing/buildaur.sh  - No such file or directory
su -       $USER  home/dom/scripts/testing/buildaur.sh    - No such file or directory
su -      "$USER" home/dom/scripts/testing/buildaur.sh    - No such file or directory
su -      "$USER"                        ./buildaur.sh    - No such file or directory
su -       dom                           ./buildaur.sh    - No such file or directory

su -       dom  ~/scripts/testing/buildaur.sh              - Permission denied
su -      $USER ~/scripts/testing/buildaur.sh              - Permission denied
su --login dom  ~/scripts/testing/buildaur.sh              - Permission denied

su -       dom   /home/dom/scripts/testing/buildaur.sh     - Runs as user, expected
su --login dom   /home/dom/scripts/testing/buildaur.sh     - Runs as user, expected
su -       dom   /home/$USER/scripts/testing/buildaur.sh   - Runs as user, expected

/tmp/test/AUR and /tmp/test/aur are different files. Most file systems are case sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM