I'm working on a script that needs to start as root. At the end of the script, it builds a list of packages. This requires running as user. Then it will move the packages, which needs to run as root again.
I've tried the following and slight variations of it in the first script to switch from root to user.
su -c - "$USER" "export PKGDEST=/tmp/test/AUR/" su -c - "$USER" "pacaur --noconfirm --noedit -cm" "$(< /tmp/test/aur)" The second line fails to run properly resulting in different errors, depending quote combination and placement. Running the lines within the first script as shown above produced the following error after reading the file /tmp/test/aur :: no targets specified (use -h for help). My preference would be getting this single script method to work properly.
Google, etc. said root, user, root in bash is bad, so tried break it into 3 parts:
The first script runs as root. The problem centers here, can't get it to start the second.
Second script runs as user, starts 3rd script as root with: sudo /bin/bash "$L"/copyaur.sh
Third script, copyaur.sh runs, moving the packages properly but doesn't show as running in the terminal because calls new shell?
Below was ran as su in a user terminal during testing. Prefer using the $USER var in the script.
As su in the terminal, then back to su - $USER, then ls in home lists my normal user name [dom's] contents.
[dom@arch testing]$ echo $USER dom [root@arch testing]# echo $USER dom [root@arch testing]# pwd /home/dom/scripts/testing I get the following results in testing, starting as su in the terminal. Want to get $USER var to work in place of dom.
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