首先我想说我不是程序员或脚本编写者。下面是当我擦除我的电脑并从头开始时我拥有的脚本。这是一个用于设置我的用户配置文件的基本脚本(更像是命令的集合)。在评论为“安装所需软件”的部分中,我想知道是否有一种方法可以创建软件名称列表(在脚本中),然后以某种方式调用该列表并放入 apt-get install 命令。我知道这是没有必要的,但如果可能的话,这是我想做的事情的方式。我不知道这是否可以通过数组或类似的东西来完成,但我想仅从一行安装所有内容。预先感谢您的帮助。
贾斯汀
#/bin/bash
###################################
# Script Name: deploy.sh
# Creation Date: 28OCT15
# Completion Date: 17FEB18
###################################
# Modify /etc/fstab to include user defined devices
sudo echo >> /etc/fstab
sudo echo >> /etc/fstab
sudo echo "# User defined Devices" >> /etc/fstab
sudo echo >> /etc/fstab
sudo echo "#250G for Downloads/etc" >> /etc/fstab
sudo echo "UUID=9ba4afc0-f565-4bf4-b935-102352dccd2c /mnt/250G ext4 errors=remount-ro 0 2" >> /etc/fstab
# Installs needed software
sudo add-apt-repository ppa:stebbins/handbrake-releases
sudo apt-get install autofs nfs-common handbrake-cli handbrake lame vim chromium-browser screen ffmpeg htop cdparanoia eyed3 vorbis-tools clementine
# Configures autofs
sudo echo >> /etc/auto.master
sudo echo "/- /etc/auto.direct" >> /etc/auto.master
sudo touch /etc/auto.direct
sudo echo "# /mnt/music -rw 192.168.0.20:/export/music" >> /etc/auto.direct
sudo echo "# /mnt/storage -rw 192.168.0.20:/export/storage" >> /etc/auto.direct
sudo echo "# /mnt/movies -rw 192.168.0.20:/export/movies" >> /etc/auto.direct
sudo echo "# /mnt/shows -rw 192.168.0.20:/export/shows" >> /etc/auto.direct
sudo echo "# /mnt/pictures -rw 192.168.0.20:/export/pictures" >> /etc/auto.direct
sudo echo >> /etc/auto.direct
sudo echo "/mnt/music -rw,hard,intr,nfsvers=3 192.168.0.20:/export/music" >> /etc/auto.direct
sudo echo "/mnt/storage -rw,hard,intr,nfsvers=3 192.168.0.20:/export/storage" >> /etc/auto.direct
sudo echo "/mnt/movies -rw,hard,intr,nfsvers=3 192.168.0.20:/export/movies" >> /etc/auto.direct
sudo echo "/mnt/shows -rw,hard,intr,nfsvers=3 192.168.0.20:/export/shows" >> /etc/auto.direct
sudo echo "/mnt/pictures -rw,hard,intr,nfsvers=3 192.168.0.20:/export/pictures" >> /etc/auto.direct
# Remove existing directories
rm -Rf $HOME/Music
rm -Rf $HOME/Pictures
# Create mount points for NFS shares
sudo mkdir /mnt/music
sudo mkdir /mnt/pictures
sudo mkdir /mnt/storage
sudo mkdir /mnt/movies
sudo mkdir /mnt/shows
#Create symbolic links in home folder
sudo ln -s /mnt/music $HOME/music
sudo ln -s /mnt/pictures $HOME/pictures
sudo ln -s /mnt/storage $HOME/storage
sudo ln -s /mnt/movies $HOME/Videos/movies
sudo ln -s /mnt/shows $HOME/Videos/shows
sudo ln -s /mnt/storage/Scripts/linux $HOME/scripts
sudo ln -s /mnt/250G/ $HOME/Downloads
# Restart needed services
sudo service autofs restart
# Generates .bash_aliases file with useful aliases
sudo echo >> $HOME/.bash_aliases
sudo echo "alias ls='ls --color=auto'" >> $HOME/.bash_aliases
sudo echo "alias vi='vim'" >> $HOME/.bash_aliases
sudo echo "alias grep='egrep --color=auto'" >> $HOME/.bash_aliases
sudo echo "alias fgrep='fgrep --color=auto'" >> $HOME/.bash_aliases
sudo echo "alias egrep='egrep --color=auto'" >> $HOME/.bash_aliases
# Create $HOME/bin directory for personal bin location
mkdir $HOME/.bin
# Add scripts to above directory
sudo cp $HOME/scripts/bash/audio/extract.sh $HOME/.bin/
sudo cp $HOME/scripts/bash/audio/m4a2mp3.sh $HOME/.bin/
sudo cp $HOME/scripts/bash/video/mkv2mp4.sh $HOME/.bin/
# Not yet up to par for video quality
# sudo cp $HOME/scripts/bash/video/avi2mp4.sh $HOME/bin/
答案1
您可以使用数组
pkgs=(autofs nfs-common handbrake-cli handbrake lame vim chromium-browser screen ffmpeg htop cdparanoia eyed3 vorbis-tools clementine)
sudo apt-get install "${pkgs[@]}"
答案2
简化脚本的一种方法是去掉所有 sudo 调用,用 sudo 调用整个脚本
#!/bin/bash
if [[ $(id -un) != root ]]; then
echo "invoke me with sudo: sudo $0"
exit
fi
echo >> /etc/fstab
# etc
另请注意,您可以将换行符放入数组定义中以使其更具可读性:
pkgs=(
autofs lame vim nfs-common handbrake-cli handbrake chromium-browser
screen eyed3 htop clementine vorbis-tools cdparanoia ffmpeg
)