我想要:
- 询问用户是否安装了snap。如果没有运行安装快照并退出的脚本,如果是则继续。
- 安装 gimp,然后显示一条消息,指示 gimp 安装已完成。
- 询问用户是否要运行 gimp。如果是,则运行 gimp,如果没有退出脚本。
答案1
这是一个sh
兼容的脚本。提供注释来解释脚本的不同部分的作用。
snap
我认为使用包管理器安装是可以的apt
,因为OP没有指定如何安装snap
.
#!/usr/bin/env sh
# Exit early if any command fails
set -e
installSnap() {
# Assumption: This script is run only on Debian-based Linux distributions
echo 'snap not installed, installing snap...'
sudo apt update
sudo apt install snapd
echo 'snap installed. Log out and back in again before using snap.'
}
# If snap is not installed, install snap and exit
command -v snap || { installSnap && echo 'Exiting install script.' && exit 0; }
# Install GIMP
echo 'Installing GIMP...'
snap install gimp
echo 'GIMP installed.'
# Prompt user for input, and store input in answer
printf 'Would you like to run GIMP (y/n)? '
read -r answer
# If answer begins with 'Y' or 'y', start gimp
# Run gimp in background with nohup so gimp will continue running after
# this script terminates
# We likely aren't interested in gimp's output, so redirect it to /dev/null
[ "$answer" != "${answer#[Yy]}" ] && nohup gimp > /dev/null 2>&1 &