通过&&操作员在命令之间执行操作,每个命令将按顺序运行,并且如果任何命令失败(即返回非零状态),则不会执行后续命令。
如果您想继续进行下去,请使用;(或换行符,它等效)代替&&。在这里,您需要执行一个命令,如果执行成功,则无论是否成功,还要执行更多命令。实现此目的的一种方法是将这些命令放在大括号组内(cd … && mount1; mount2因为它会执行mount2是否cd由于优先级而成功执行,因此将不起作用)。
cd /mnt/gentoo && {
mount -t proc none /mnt/gentoo/proc
mount --rbind /dev /mnt/gentoo/dev
mount --rbind /sys /mnt/gentoo/sys
…
}
或者,如果cd失败,则退出脚本或从函数返回。
cd /mnt/gentoo || exit $?
mount -t proc none /mnt/gentoo/proc
…
或者,在下运行set -e,然后|| true在可能失败的命令后放置(“或继续前进”)。
set -e
cd /mnt/gentoo
mount -t proc none /mnt/gentoo/proc || true
…
或者,编写必须成功的命令:测试是否/proc已安装等等。
mount_if_needed () {
eval "mount_point=${\$#}"
awk -v target="$mount_point" '$2 == target {exit(0)} END {exit(1)}' </proc/mounts ||
mount "$@"
}
set -e
cd /mnt/gentoo
mount_if_needed -t proc none /mnt/gentoo/proc
您在打电话时还有另一个问题chroot。您已经写道:“在chroot中运行bash。当bash退出,运行source和export。” 那可能不是你的意思。/etc/profile可以通过将bash设置为登录shell来进行读取。设置的一种可能方法PS1是在运行bash之前进行设置,但是如果/etc/profile覆盖它则不起作用,这很常见。更好的方法是PS1在~/.bashrc chroot中运行(.bashrc,而不是.profile)。
chroot . bash --login
Debian使用下面的代码来设置PS1的/etc/bash.bashrc基础上的内容/etc/debian_chroot:
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
或者,对于提示,请使用环境变量代替:运行
CHROOT_LOCATION=$PWD chroot bash --login
并将其放在~/.bashrc或中/etc/bash.bashrc:
if [ -n "$CHROOT_LOCATION" ]; then PS1="($CHROOT_LOCATION)$PS1"; fi
;。只需将每个命令放在自己的行上,但还必须摆脱set -e。如果您不关心命令失败,为什么还要使用set -e?(我知道这个问题只需要一行,但这在脚本中没有任何意义)。