我正在研究需要以root身份启动的Linux Bash脚本。在脚本的末尾,它将生成软件包列表。这需要以用户身份运行。然后它将移动软件包,这需要再次以root用户身份运行。我已经设置了sudo,但是想要保持脚本尽可能通用。
我在第一个脚本中尝试了以下内容以及它的一些细微变化,以从根用户切换到用户。
su -c - "$USER" "export PKGDEST=/tmp/test/AUR/"
su -c - "$USER" "pacaur --noconfirm --noedit -cm" "$(< /tmp/test/aur)"
第二行无法正常运行,导致不同的错误,具体取决于报价的组合和位置。在读取文件/ tmp / test / aur ::未指定目标之后,如上所示在第一个脚本中运行这些行会产生以下错误(使用-h获得帮助)。我的喜好是使单个脚本方法正常工作。
Google等表示root,用户,bash中的root不好,因此尝试将其分为3部分:
- 第一个脚本以root身份运行。问题集中在这里,无法启动第二个问题。
- 第二个脚本以用户身份运行,并以root身份启动第三个脚本:sudo / bin / bash“ $ L” /copyaur.sh
- 第三个脚本copyaur.sh运行,可以正常移动软件包,但是由于调用了新的shell而没有显示为在终端中运行?
在测试过程中,以下用户终端中以su身份运行。最好在脚本中使用$ USER var。
在终端中以su身份显示,然后返回su - $USER
,然后 ls
在home中列出我的普通用户名[dom]的内容。
[dom@arch testing]$ echo $USER
dom
[root@arch testing]# echo $USER
dom
[root@arch testing]# pwd
/home/dom/scripts/testing
我在测试中获得以下结果,从终端中的su开始。想要让$ USER var代替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
2014年9月19日,编辑以添加所需的信息
这是脚本的一部分。仍然没有解决方案。最新的变体是通过在失败之前从列表中构建第一个程序包来实现的。但是,它确实完成了下一行,即脚本中的最后一条命令。请记住,此命令在将其与程序一起使用时可以正常工作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
这是设置-x的输出。我正在使用pacaur构建软件包,其中使用了Cower和makepkg。似乎pacaur或makepkg都在跟踪要构建的软件包列表的文件,并且无法将软件包名称识别为命令。
+ 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
root
?您正在使用NFS吗?
sudo -u user command
或即可su -c command user
。
sudo
设置好了吗?