我正在编写一个 Bash 脚本,它将在全新安装后加快我的 shell 的速度。
main()
{
#
# By default we assume the terminal doesn't support colors
#
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
#
# Check if we are connected to a terminal, and that terminal
# supports colors.
#
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
#
# Set the colors if we can
#
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
fi
#
# Only enable exit-on-error after the non-critical colorization stuff,
# which may fail on systems lacking tput or terminfo
#
set -e
################################################################################
printf "${YELLOW}"
echo ''
echo ' ____ __ ___ _____ __ '
echo ' / __ )____ ______/ /_ |__ \/__ / _____/ /_ '
echo ' / __ / __ `/ ___/ __ \__/ / / / / ___/ __ \'
echo ' / /_/ / /_/ (__ ) / / / __/ / /__(__ ) / / /'
echo '/_____/\__,_/____/_/ /_/____/ /____/____/_/ /_/ '
echo ''
echo ''
printf "${NORMAL}"
################################################################################
#
# Find out if Zsh is already installed
#
CHECK_ZSH_INSTALLED=$(grep /zsh$ /etc/shells | wc -l)
#
# Check to see if Zsh is already installed
#
if [ ! $CHECK_ZSH_INSTALLED -ge 1 ]; then
sudo apt-get -y install zsh
fi
#
# Clean the memory
#
unset CHECK_ZSH_INSTALLED
################################################################################
#
# Remove the previous config file, so we know we start from scratch
#
rm ~/.zshrc &&
#
# Removing unnecessary Bash files
#
rm ~/.bash_history 2> /dev/null &&
rm ~/.bash_logout 2> /dev/null &&
rm ~/.bashrc 2> /dev/null &&
rm ~/.bash_sessions 2> /dev/null &&
rm ~/.sh_history 2> /dev/null &&
################################################################################
#
# Download the configuration file
#
curl -fsSL "https://raw.githubusercontent.com/davidgatti/my-development-setup/master/08_Zsh_instead_of_Bash/zshrc" >> ~/.zshrc
#
# Get the name of the logged in user
#
USER_NAME=$(whoami)
#
# Get the home path for the logged in user
#
HOME_PATH=$(getent passwd $USER_NAME | cut -d: -f6)
#
# Add a dynamic entry
#
echo 'zstyle :compinstall filename '$HOME_PATH/.zshrc'' >> ~/.zshrc
}
main
我确实跑了Bash -x
并且一切正常。但curl
在结果跟踪中却看不到。我尝试过的:
- 在变量中添加curl
- 使用评估
"
以 3 种不同方式设置- ETC。
问题
我想下载一个curl
链接到的 bash 脚本内未签名的文件,其中 bash 脚本将按以下方式执行:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/davidgatti/my-development-setup/master/08_Zsh_instead_of_Bash/install.sh)"
sh -x 输出
如果我跑
sh -cx "$(curl -fsSL https://raw.githubusercontent.com/davidgatti/my-development-setup/master/08_Zsh_instead_of_Bash/install.sh)"
这是输出。
+ main
+ RED=
+ GREEN=
+ YELLOW=
+ BLUE=
+ BOLD=
+ NORMAL=
+ which tput
+ tput colors
+ ncolors=256
+ [ -t 1 ]
+ [ -n 256 ]
+ [ 256 -ge 8 ]
+ tput setaf 1
+ RED=
+ tput setaf 2
+ GREEN=
+ tput setaf 3
+ YELLOW=
+ tput setaf 4
+ BLUE=
+ tput bold
+ BOLD=
+ tput sgr0
+ NORMAL=
+ set -e
+ printf
+ echo
+ echo ____ __ ___ _____ __
____ __ ___ _____ __
+ echo / __ )____ ______/ /_ |__ \/__ / _____/ /_
/ __ )____ ______/ /_ |__ \/__ / _____/ /_
+ echo / __ / __ `/ ___/ __ \__/ / / / / ___/ __ \
/ __ / __ `/ ___/ __ \__/ / / / / ___/ __ \
+ echo / /_/ / /_/ (__ ) / / / __/ / /__(__ ) / / /
/ /_/ / /_/ (__ ) / / / __/ / /__(__ ) / / /
+ echo /_____/\__,_/____/_/ /_/____/ /____/____/_/ /_/
/_____/\__,_/____/_/ /_/____/ /____/____/_/ /_/
+ echo
+ echo
+ printf
+ wc -l
+ grep /zsh$ /etc/shells
+ CHECK_ZSH_INSTALLED=2
+ [ ! 2 -ge 1 ]
+ unset CHECK_ZSH_INSTALLED
+ rm /home/admin/.zshrc
我个人不认为curl
会被处决
答案1
您没有~/.zshrc
文件,因此rm ~/.zshrc
以非零值退出。由于rm ~/.zshrc
是与 链接的一长串命令中的第一个命令&&
,因此不会执行以下任何命令。curl
是该列表的最后一个命令。
解决方案#1:使用rm -f
代替rm
或不使用 来终止您的行&&
。
而且,你已经把set -e
你闪亮的旗帜放在前面了。这会使您的脚本在第一个意外失败的命令处退出。因此,仅删除&&
是不够的。
解决方案#2:使用rm -f
或终止你的rm
行|| true
|| :
结论:改变你的rm foo 2> /dev/null &&
一切rm -f foo
答案2
您的问题不清楚您遇到了什么问题,但要保存您有 URL 的文件:
curl -o /path/to/output-file http://www.example.com/path/to/the/file
生活非常危险并在bash
shell 中运行假定的脚本:
bash -c "$(curl -fsSL http://www.example.com/path/to/script-that-quietly-installs-a-rootkit-for-you)"