I need advice on how to make every command I run in a Bash script first display itself and then execute. I created a simple function and a subsequent sed command to uncomment lines in the pacman.conf file, but it isn't working. Specifically, I want to uncomment these two lines:
YC="\033[1;33m"
RC="\033[0m"
RCH() {
echo -e "${YC} $*${RC}"
arch-chroot /mnt /bin/bash -c "$*" | sed 's/^/ /'
}
RCH sed '/^#\[multilib\]/s/^#//n;/^#Include/s/^#//' /etc/pacman.conf
#[multilib]
#Include ...
Here is what I need:
- Search for a line starting with #[multilib]. If found, uncomment it.
- Move to the next line and search for a line starting with #Include. If found, uncomment it. Then stop, no further searching or modifications.
Update: I thank you all very much for guiding me in the right direction. I modified the script like this, and so far, everything works except space key to continue.
WAIT() {
echo -e "Press ${GC}SPACE${RC} to continue or ${GC}ESC${RC} to exit .."
while true; do
read -n 1 -s key
if [[ $key == " " ]]; then
break
elif [[ $key == $'\e' ]]; then
exit 0
fi
done
}
RUN() {
echo -e "${YC} $*${RC}"
WAIT
/bin/bash -c "$@" | sed 's/^/ /'
}
RUN "sed -i '/^#\[multilib\]/{s/^#//;n;/^#Include/s/^#//}' /etc/pacman.conf"
RUN "echo LANG=${LOCALE} > /etc/locale.conf"
RUN "cat <<EOL > /etc/hosts
127.0.0.1 localhost
::1 localhost
EOL"
set -xsounds like what you want. It prints all commands the shell executes, with the variables etc. expanded. Put theset -xat the top of the script (orset -o xtraceif you like the more verbose variant)