Bash root to user. Best in same or separate script?Using chown $USER:$USER inside bash scriptShell script...

Napkin Folding Problem / Rumpled Ruble Problem

Why is Kirchoff's loop rule true in a DC circuit?

Selecting 2 column in an Inner join

Fight a biblical flood apart from building barriers

Confirm the ending of a string

How can I locate a missing person abroad?

How to say "quirky" in German without sounding derogatory?

Eccentric keepers

Is there any way to land a rover on the Moon without using any thrusters?

What did Aquinas do for recreation?

Is it appropriate for a professor to require students to sign a non-disclosure agreement before being taught?

Job offer without any details but asking me to withdraw other applications - is it normal?

Is low emotional intelligence associated with right-wing and prejudiced attitudes?

Telling my mother that I have anorexia without panicking her

Does my opponent need to prove his creature has morph?

Why would "an mule" be used instead of "a mule"?

Where can I find vomiting people?

Type leftwards arrow on macOS

Might have gotten a coworker sick, should I address this?

Is a suit against a Univeristy Dorm for changing policies on a whim likely to succeed (USA)?

Linear Programming with additional "if-then"/"Default to zero" constraints?

Are Democrats more likely to believe Astrology is a science?

Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?

Why do sellers care about down payments?



Bash root to user. Best in same or separate script?


Using chown $USER:$USER inside bash scriptShell script printing blank $USER variable when executed by SmartdBash script to display directory listing gives errorRsync bash script error (sending: command not found)how to let sudo fork bash instead of sh?Cron shell ignores runuser command - why?Bash script copy file to user's (wildcard) home dirbash: /root/bin/hello_world: Permission deniedexecute command with sudo and execute Bash script with sudo






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















I'm working on a Linux Bash 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 have sudo set up, but want to keep the script as generic as possible.



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:




  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 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


9/19/2014 EDIT TO ADD REQUESTED INFO



Here's part of the script. Still no solution. This latest variation has made it through building the first package from the list before failing. It does however complete the next line, which is the last command in the script. Keep in mind this command works fine outside of using it with the proceeding su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"



#!/bin/bash     
## makeiso.sh must be run as root within /home/"$USER"/makeiso/releng
## makeiso dependancies: archiso pacaur
#############################################################################

# Accessing an empty variable will yield an error

set -u

# Check if root
# Print message to user

echo "Checking if we're running as root"

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

# echo for a space
echo ""

# If passed root test, send message print message to user

if [[ $EUID = 0 ]]; then
echo "Passed root test, proceeding"
fi

##################################################
##################################################
Line 180 of the script running as root.......
##################################################

# Print message to user

echo "Copying system configuration files"

set -x

# Create directory for following command

mkdir -p /tmp/makeiso/

# Create a list of modified config files

pacman -Qii | awk '/^MODIFIED/ {print $2}' >> /tmp/makeiso/rtmodconfig.list

# Create destination directory for the following command

mkdir -p "$L"/airootfs/makeiso/configs/rootconfigs

# Copy above list to releng for later use

cp /tmp/makeiso/rtmodconfig.list "$L"/airootfs/makeiso/configs/rootconfigs/rtmodconfig.list

# Read rtmodconfig.list and copy the modified config files to releng

xargs -a /tmp/makeiso/rtmodconfig.list cp -t "$L"/airootfs/makeiso/configs/rootconfigs/

# Set makepkg variable to define where to send built packages

su -c - "$USER" "export PKGDEST=/tmp/makeiso/AUR"

# Build AUR packages from list, /tmp/makepkg/aur
#############

su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"

#############

# Copy the prebuilt AUR packages to releng

cp -R /tmp/makeiso/AUR "$L"/airootfs/makeiso/packages/AUR


Here's the output with set -x. I'm using pacaur to build the packages, which uses cower and makepkg. It seems like either pacaur or makepkg is loosing track of the file with the list of packages to build, and doesn't recognize the package names as commands.



+ su -c - dom 'export PKGDEST=/tmp/makeiso/AUR'
+ su -c - dom 'pacaur --noconfirm --noedit -cm bootinfoscript
cairo-ubuntu
cower
fontconfig-ubuntu
freetype2-ubuntu
gnome-colors-icon-theme
gnome-colors-icon-theme-extras
gnome-icon-theme-xfce
google-chrome
grub-legacy
libxfcegui4
pacaur
package-query
pkgbrowser
ttf-ms-fonts
virtualbox-ext-oracle
vivaldi
xfce4-quicklauncher-plugin'
:: resolving dependencies...
:: looking for inter-conflicts...

AUR Packages (1): bootinfoscript-0.61-1

:: Retrieving package(s)...
sudo: no tty present and no askpass program specified
:: Checking bootinfoscript integrity...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:37 PDT 2015)
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
:: Building bootinfoscript package(s)...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
==> Extracting sources...
-> Extracting bootinfoscript-061.tar.gz with bsdtar
==> Removing existing $pkgdir/ directory...
==> Entering fakeroot environment...
==> Starting package()...
==> Tidying install...
-> Purging unwanted files...
-> Removing libtool files...
-> Removing static library files...
-> Compressing man and info pages...
-> Stripping unneeded symbols from binaries and libraries...
==> Creating package "bootinfoscript"...
-> Generating .PKGINFO file...
-> Generating .MTREE file...
-> Compressing package...
==> Leaving fakeroot environment.
==> Finished making: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
:: bootinfoscript cleaning skipped
bash: line 1: cairo-ubuntu: command not found
error: no operation specified (use -h for help)
bash: line 3: fontconfig-ubuntu: command not found
bash: line 4: freetype2-ubuntu: command not found
bash: line 5: gnome-colors-icon-theme: command not found
bash: line 6: gnome-colors-icon-theme-extras: command not found
bash: line 7: gnome-icon-theme-xfce: command not found
bash: line 8: google-chrome: command not found
bash: line 9: grub-legacy: command not found
bash: line 10: libxfcegui4: command not found
error: no operation specified (use -h for help)
bash: line 14: ttf-ms-fonts: command not found
bash: line 15: virtualbox-ext-oracle: command not found
bash: line 16: vivaldi: command not found
bash: line 17: xfce4-quicklauncher-plugin: command not found
+ cp -R /tmp/makeiso/AUR /home/dom/makeiso/releng/airootfs/makeiso/packages/AUR









share|improve this question

















bumped to the homepage by Community 38 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















  • What OS are you using? Do you have sudo set up?

    – terdon
    Sep 17 '15 at 22:32











  • I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

    – Dom
    Sep 18 '15 at 2:30











  • Why does building a list of packages requires to be user not root? Are you on NFS?

    – Bichoy
    Sep 18 '15 at 6:00











  • This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

    – Dom
    Sep 18 '15 at 8:46











  • It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

    – terdon
    Sep 18 '15 at 11:53


















2















I'm working on a Linux Bash 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 have sudo set up, but want to keep the script as generic as possible.



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:




  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 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


9/19/2014 EDIT TO ADD REQUESTED INFO



Here's part of the script. Still no solution. This latest variation has made it through building the first package from the list before failing. It does however complete the next line, which is the last command in the script. Keep in mind this command works fine outside of using it with the proceeding su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"



#!/bin/bash     
## makeiso.sh must be run as root within /home/"$USER"/makeiso/releng
## makeiso dependancies: archiso pacaur
#############################################################################

# Accessing an empty variable will yield an error

set -u

# Check if root
# Print message to user

echo "Checking if we're running as root"

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

# echo for a space
echo ""

# If passed root test, send message print message to user

if [[ $EUID = 0 ]]; then
echo "Passed root test, proceeding"
fi

##################################################
##################################################
Line 180 of the script running as root.......
##################################################

# Print message to user

echo "Copying system configuration files"

set -x

# Create directory for following command

mkdir -p /tmp/makeiso/

# Create a list of modified config files

pacman -Qii | awk '/^MODIFIED/ {print $2}' >> /tmp/makeiso/rtmodconfig.list

# Create destination directory for the following command

mkdir -p "$L"/airootfs/makeiso/configs/rootconfigs

# Copy above list to releng for later use

cp /tmp/makeiso/rtmodconfig.list "$L"/airootfs/makeiso/configs/rootconfigs/rtmodconfig.list

# Read rtmodconfig.list and copy the modified config files to releng

xargs -a /tmp/makeiso/rtmodconfig.list cp -t "$L"/airootfs/makeiso/configs/rootconfigs/

# Set makepkg variable to define where to send built packages

su -c - "$USER" "export PKGDEST=/tmp/makeiso/AUR"

# Build AUR packages from list, /tmp/makepkg/aur
#############

su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"

#############

# Copy the prebuilt AUR packages to releng

cp -R /tmp/makeiso/AUR "$L"/airootfs/makeiso/packages/AUR


Here's the output with set -x. I'm using pacaur to build the packages, which uses cower and makepkg. It seems like either pacaur or makepkg is loosing track of the file with the list of packages to build, and doesn't recognize the package names as commands.



+ su -c - dom 'export PKGDEST=/tmp/makeiso/AUR'
+ su -c - dom 'pacaur --noconfirm --noedit -cm bootinfoscript
cairo-ubuntu
cower
fontconfig-ubuntu
freetype2-ubuntu
gnome-colors-icon-theme
gnome-colors-icon-theme-extras
gnome-icon-theme-xfce
google-chrome
grub-legacy
libxfcegui4
pacaur
package-query
pkgbrowser
ttf-ms-fonts
virtualbox-ext-oracle
vivaldi
xfce4-quicklauncher-plugin'
:: resolving dependencies...
:: looking for inter-conflicts...

AUR Packages (1): bootinfoscript-0.61-1

:: Retrieving package(s)...
sudo: no tty present and no askpass program specified
:: Checking bootinfoscript integrity...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:37 PDT 2015)
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
:: Building bootinfoscript package(s)...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
==> Extracting sources...
-> Extracting bootinfoscript-061.tar.gz with bsdtar
==> Removing existing $pkgdir/ directory...
==> Entering fakeroot environment...
==> Starting package()...
==> Tidying install...
-> Purging unwanted files...
-> Removing libtool files...
-> Removing static library files...
-> Compressing man and info pages...
-> Stripping unneeded symbols from binaries and libraries...
==> Creating package "bootinfoscript"...
-> Generating .PKGINFO file...
-> Generating .MTREE file...
-> Compressing package...
==> Leaving fakeroot environment.
==> Finished making: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
:: bootinfoscript cleaning skipped
bash: line 1: cairo-ubuntu: command not found
error: no operation specified (use -h for help)
bash: line 3: fontconfig-ubuntu: command not found
bash: line 4: freetype2-ubuntu: command not found
bash: line 5: gnome-colors-icon-theme: command not found
bash: line 6: gnome-colors-icon-theme-extras: command not found
bash: line 7: gnome-icon-theme-xfce: command not found
bash: line 8: google-chrome: command not found
bash: line 9: grub-legacy: command not found
bash: line 10: libxfcegui4: command not found
error: no operation specified (use -h for help)
bash: line 14: ttf-ms-fonts: command not found
bash: line 15: virtualbox-ext-oracle: command not found
bash: line 16: vivaldi: command not found
bash: line 17: xfce4-quicklauncher-plugin: command not found
+ cp -R /tmp/makeiso/AUR /home/dom/makeiso/releng/airootfs/makeiso/packages/AUR









share|improve this question

















bumped to the homepage by Community 38 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















  • What OS are you using? Do you have sudo set up?

    – terdon
    Sep 17 '15 at 22:32











  • I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

    – Dom
    Sep 18 '15 at 2:30











  • Why does building a list of packages requires to be user not root? Are you on NFS?

    – Bichoy
    Sep 18 '15 at 6:00











  • This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

    – Dom
    Sep 18 '15 at 8:46











  • It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

    – terdon
    Sep 18 '15 at 11:53














2












2








2








I'm working on a Linux Bash 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 have sudo set up, but want to keep the script as generic as possible.



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:




  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 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


9/19/2014 EDIT TO ADD REQUESTED INFO



Here's part of the script. Still no solution. This latest variation has made it through building the first package from the list before failing. It does however complete the next line, which is the last command in the script. Keep in mind this command works fine outside of using it with the proceeding su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"



#!/bin/bash     
## makeiso.sh must be run as root within /home/"$USER"/makeiso/releng
## makeiso dependancies: archiso pacaur
#############################################################################

# Accessing an empty variable will yield an error

set -u

# Check if root
# Print message to user

echo "Checking if we're running as root"

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

# echo for a space
echo ""

# If passed root test, send message print message to user

if [[ $EUID = 0 ]]; then
echo "Passed root test, proceeding"
fi

##################################################
##################################################
Line 180 of the script running as root.......
##################################################

# Print message to user

echo "Copying system configuration files"

set -x

# Create directory for following command

mkdir -p /tmp/makeiso/

# Create a list of modified config files

pacman -Qii | awk '/^MODIFIED/ {print $2}' >> /tmp/makeiso/rtmodconfig.list

# Create destination directory for the following command

mkdir -p "$L"/airootfs/makeiso/configs/rootconfigs

# Copy above list to releng for later use

cp /tmp/makeiso/rtmodconfig.list "$L"/airootfs/makeiso/configs/rootconfigs/rtmodconfig.list

# Read rtmodconfig.list and copy the modified config files to releng

xargs -a /tmp/makeiso/rtmodconfig.list cp -t "$L"/airootfs/makeiso/configs/rootconfigs/

# Set makepkg variable to define where to send built packages

su -c - "$USER" "export PKGDEST=/tmp/makeiso/AUR"

# Build AUR packages from list, /tmp/makepkg/aur
#############

su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"

#############

# Copy the prebuilt AUR packages to releng

cp -R /tmp/makeiso/AUR "$L"/airootfs/makeiso/packages/AUR


Here's the output with set -x. I'm using pacaur to build the packages, which uses cower and makepkg. It seems like either pacaur or makepkg is loosing track of the file with the list of packages to build, and doesn't recognize the package names as commands.



+ su -c - dom 'export PKGDEST=/tmp/makeiso/AUR'
+ su -c - dom 'pacaur --noconfirm --noedit -cm bootinfoscript
cairo-ubuntu
cower
fontconfig-ubuntu
freetype2-ubuntu
gnome-colors-icon-theme
gnome-colors-icon-theme-extras
gnome-icon-theme-xfce
google-chrome
grub-legacy
libxfcegui4
pacaur
package-query
pkgbrowser
ttf-ms-fonts
virtualbox-ext-oracle
vivaldi
xfce4-quicklauncher-plugin'
:: resolving dependencies...
:: looking for inter-conflicts...

AUR Packages (1): bootinfoscript-0.61-1

:: Retrieving package(s)...
sudo: no tty present and no askpass program specified
:: Checking bootinfoscript integrity...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:37 PDT 2015)
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
:: Building bootinfoscript package(s)...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
==> Extracting sources...
-> Extracting bootinfoscript-061.tar.gz with bsdtar
==> Removing existing $pkgdir/ directory...
==> Entering fakeroot environment...
==> Starting package()...
==> Tidying install...
-> Purging unwanted files...
-> Removing libtool files...
-> Removing static library files...
-> Compressing man and info pages...
-> Stripping unneeded symbols from binaries and libraries...
==> Creating package "bootinfoscript"...
-> Generating .PKGINFO file...
-> Generating .MTREE file...
-> Compressing package...
==> Leaving fakeroot environment.
==> Finished making: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
:: bootinfoscript cleaning skipped
bash: line 1: cairo-ubuntu: command not found
error: no operation specified (use -h for help)
bash: line 3: fontconfig-ubuntu: command not found
bash: line 4: freetype2-ubuntu: command not found
bash: line 5: gnome-colors-icon-theme: command not found
bash: line 6: gnome-colors-icon-theme-extras: command not found
bash: line 7: gnome-icon-theme-xfce: command not found
bash: line 8: google-chrome: command not found
bash: line 9: grub-legacy: command not found
bash: line 10: libxfcegui4: command not found
error: no operation specified (use -h for help)
bash: line 14: ttf-ms-fonts: command not found
bash: line 15: virtualbox-ext-oracle: command not found
bash: line 16: vivaldi: command not found
bash: line 17: xfce4-quicklauncher-plugin: command not found
+ cp -R /tmp/makeiso/AUR /home/dom/makeiso/releng/airootfs/makeiso/packages/AUR









share|improve this question
















I'm working on a Linux Bash 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 have sudo set up, but want to keep the script as generic as possible.



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:




  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 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


9/19/2014 EDIT TO ADD REQUESTED INFO



Here's part of the script. Still no solution. This latest variation has made it through building the first package from the list before failing. It does however complete the next line, which is the last command in the script. Keep in mind this command works fine outside of using it with the proceeding su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"



#!/bin/bash     
## makeiso.sh must be run as root within /home/"$USER"/makeiso/releng
## makeiso dependancies: archiso pacaur
#############################################################################

# Accessing an empty variable will yield an error

set -u

# Check if root
# Print message to user

echo "Checking if we're running as root"

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

# echo for a space
echo ""

# If passed root test, send message print message to user

if [[ $EUID = 0 ]]; then
echo "Passed root test, proceeding"
fi

##################################################
##################################################
Line 180 of the script running as root.......
##################################################

# Print message to user

echo "Copying system configuration files"

set -x

# Create directory for following command

mkdir -p /tmp/makeiso/

# Create a list of modified config files

pacman -Qii | awk '/^MODIFIED/ {print $2}' >> /tmp/makeiso/rtmodconfig.list

# Create destination directory for the following command

mkdir -p "$L"/airootfs/makeiso/configs/rootconfigs

# Copy above list to releng for later use

cp /tmp/makeiso/rtmodconfig.list "$L"/airootfs/makeiso/configs/rootconfigs/rtmodconfig.list

# Read rtmodconfig.list and copy the modified config files to releng

xargs -a /tmp/makeiso/rtmodconfig.list cp -t "$L"/airootfs/makeiso/configs/rootconfigs/

# Set makepkg variable to define where to send built packages

su -c - "$USER" "export PKGDEST=/tmp/makeiso/AUR"

# Build AUR packages from list, /tmp/makepkg/aur
#############

su -c - "$USER" "pacaur --noconfirm --noedit -cm $(< /tmp/makeiso/aur)"

#############

# Copy the prebuilt AUR packages to releng

cp -R /tmp/makeiso/AUR "$L"/airootfs/makeiso/packages/AUR


Here's the output with set -x. I'm using pacaur to build the packages, which uses cower and makepkg. It seems like either pacaur or makepkg is loosing track of the file with the list of packages to build, and doesn't recognize the package names as commands.



+ su -c - dom 'export PKGDEST=/tmp/makeiso/AUR'
+ su -c - dom 'pacaur --noconfirm --noedit -cm bootinfoscript
cairo-ubuntu
cower
fontconfig-ubuntu
freetype2-ubuntu
gnome-colors-icon-theme
gnome-colors-icon-theme-extras
gnome-icon-theme-xfce
google-chrome
grub-legacy
libxfcegui4
pacaur
package-query
pkgbrowser
ttf-ms-fonts
virtualbox-ext-oracle
vivaldi
xfce4-quicklauncher-plugin'
:: resolving dependencies...
:: looking for inter-conflicts...

AUR Packages (1): bootinfoscript-0.61-1

:: Retrieving package(s)...
sudo: no tty present and no askpass program specified
:: Checking bootinfoscript integrity...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:37 PDT 2015)
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
:: Building bootinfoscript package(s)...
==> Making package: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Found bootinfoscript-061.tar.gz
==> Validating source files with md5sums...
bootinfoscript-061.tar.gz ... Passed
==> Extracting sources...
-> Extracting bootinfoscript-061.tar.gz with bsdtar
==> Removing existing $pkgdir/ directory...
==> Entering fakeroot environment...
==> Starting package()...
==> Tidying install...
-> Purging unwanted files...
-> Removing libtool files...
-> Removing static library files...
-> Compressing man and info pages...
-> Stripping unneeded symbols from binaries and libraries...
==> Creating package "bootinfoscript"...
-> Generating .PKGINFO file...
-> Generating .MTREE file...
-> Compressing package...
==> Leaving fakeroot environment.
==> Finished making: bootinfoscript 0.61-1 (Sat Sep 19 22:27:38 PDT 2015)
:: bootinfoscript cleaning skipped
bash: line 1: cairo-ubuntu: command not found
error: no operation specified (use -h for help)
bash: line 3: fontconfig-ubuntu: command not found
bash: line 4: freetype2-ubuntu: command not found
bash: line 5: gnome-colors-icon-theme: command not found
bash: line 6: gnome-colors-icon-theme-extras: command not found
bash: line 7: gnome-icon-theme-xfce: command not found
bash: line 8: google-chrome: command not found
bash: line 9: grub-legacy: command not found
bash: line 10: libxfcegui4: command not found
error: no operation specified (use -h for help)
bash: line 14: ttf-ms-fonts: command not found
bash: line 15: virtualbox-ext-oracle: command not found
bash: line 16: vivaldi: command not found
bash: line 17: xfce4-quicklauncher-plugin: command not found
+ cp -R /tmp/makeiso/AUR /home/dom/makeiso/releng/airootfs/makeiso/packages/AUR






bash shell-script root not-root-user






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 20 '15 at 6:23







Dom

















asked Sep 17 '15 at 21:44









DomDom

291 silver badge6 bronze badges




291 silver badge6 bronze badges






bumped to the homepage by Community 38 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.









bumped to the homepage by Community 38 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 38 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • What OS are you using? Do you have sudo set up?

    – terdon
    Sep 17 '15 at 22:32











  • I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

    – Dom
    Sep 18 '15 at 2:30











  • Why does building a list of packages requires to be user not root? Are you on NFS?

    – Bichoy
    Sep 18 '15 at 6:00











  • This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

    – Dom
    Sep 18 '15 at 8:46











  • It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

    – terdon
    Sep 18 '15 at 11:53



















  • What OS are you using? Do you have sudo set up?

    – terdon
    Sep 17 '15 at 22:32











  • I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

    – Dom
    Sep 18 '15 at 2:30











  • Why does building a list of packages requires to be user not root? Are you on NFS?

    – Bichoy
    Sep 18 '15 at 6:00











  • This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

    – Dom
    Sep 18 '15 at 8:46











  • It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

    – terdon
    Sep 18 '15 at 11:53

















What OS are you using? Do you have sudo set up?

– terdon
Sep 17 '15 at 22:32





What OS are you using? Do you have sudo set up?

– terdon
Sep 17 '15 at 22:32













I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

– Dom
Sep 18 '15 at 2:30





I'm running this on Arch Linux and sudo is set up. I'd like to possibly share this script down the road, so I'd like to keep as generic as possible. I edited the op to reflect this.

– Dom
Sep 18 '15 at 2:30













Why does building a list of packages requires to be user not root? Are you on NFS?

– Bichoy
Sep 18 '15 at 6:00





Why does building a list of packages requires to be user not root? Are you on NFS?

– Bichoy
Sep 18 '15 at 6:00













This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

– Dom
Sep 18 '15 at 8:46





This is an Arch Linux relate standard, to not build packages as root. In fact makepkg will not run as root. Arch has the AUR, Arch user repository. This repo contains build scripts rather than binary pre built packages. Anyone can contribute, mostly unchecked, so the potential is there for things to go wrong, either in error or malicious. More info: allanmcrae.com/2015/01/replacing-makepkg-asroot

– Dom
Sep 18 '15 at 8:46













It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

– terdon
Sep 18 '15 at 11:53





It's very hard to understand what you're doing since you don't show your scripts. Could you simplify this to an example script that reproduces the problem? If you need to run a single command as non-root, just use sudo -u user command or su -c command user.

– terdon
Sep 18 '15 at 11:53










1 Answer
1






active

oldest

votes


















0
















The following worked as needed in my script to switch from root to $USER, then back to root.



sudo -u "$USER" bash << HereTag

command
command
command

# End Here Tag
HereTag


To review, I'm starting the script like this:



[user@linuxbox ~]$ su
Password:
[root@linuxbox user]# ./script.sh


After $ su, echo $USER shows my username. Even though I'm starting the script as root, I am prompted for a password @ sudo -u "$USER" bash.



I'm in the wheel group needing to provide password in suoders file.



I'd like to eventually figure out how to accomplish this without the need for sudo, but this will work for now.






share|improve this answer






























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });















    draft saved

    draft discarded
















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f230402%2fbash-root-to-user-best-in-same-or-separate-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    The following worked as needed in my script to switch from root to $USER, then back to root.



    sudo -u "$USER" bash << HereTag

    command
    command
    command

    # End Here Tag
    HereTag


    To review, I'm starting the script like this:



    [user@linuxbox ~]$ su
    Password:
    [root@linuxbox user]# ./script.sh


    After $ su, echo $USER shows my username. Even though I'm starting the script as root, I am prompted for a password @ sudo -u "$USER" bash.



    I'm in the wheel group needing to provide password in suoders file.



    I'd like to eventually figure out how to accomplish this without the need for sudo, but this will work for now.






    share|improve this answer
































      0
















      The following worked as needed in my script to switch from root to $USER, then back to root.



      sudo -u "$USER" bash << HereTag

      command
      command
      command

      # End Here Tag
      HereTag


      To review, I'm starting the script like this:



      [user@linuxbox ~]$ su
      Password:
      [root@linuxbox user]# ./script.sh


      After $ su, echo $USER shows my username. Even though I'm starting the script as root, I am prompted for a password @ sudo -u "$USER" bash.



      I'm in the wheel group needing to provide password in suoders file.



      I'd like to eventually figure out how to accomplish this without the need for sudo, but this will work for now.






      share|improve this answer






























        0














        0










        0









        The following worked as needed in my script to switch from root to $USER, then back to root.



        sudo -u "$USER" bash << HereTag

        command
        command
        command

        # End Here Tag
        HereTag


        To review, I'm starting the script like this:



        [user@linuxbox ~]$ su
        Password:
        [root@linuxbox user]# ./script.sh


        After $ su, echo $USER shows my username. Even though I'm starting the script as root, I am prompted for a password @ sudo -u "$USER" bash.



        I'm in the wheel group needing to provide password in suoders file.



        I'd like to eventually figure out how to accomplish this without the need for sudo, but this will work for now.






        share|improve this answer















        The following worked as needed in my script to switch from root to $USER, then back to root.



        sudo -u "$USER" bash << HereTag

        command
        command
        command

        # End Here Tag
        HereTag


        To review, I'm starting the script like this:



        [user@linuxbox ~]$ su
        Password:
        [root@linuxbox user]# ./script.sh


        After $ su, echo $USER shows my username. Even though I'm starting the script as root, I am prompted for a password @ sudo -u "$USER" bash.



        I'm in the wheel group needing to provide password in suoders file.



        I'd like to eventually figure out how to accomplish this without the need for sudo, but this will work for now.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 25 '15 at 9:00

























        answered Sep 25 '15 at 8:44









        DomDom

        291 silver badge6 bronze badges




        291 silver badge6 bronze badges


































            draft saved

            draft discarded



















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f230402%2fbash-root-to-user-best-in-same-or-separate-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...