-3

first I would like to say that I'm not a programmer or scripter. Below is a script I have if/when I wipe my PC and start from scratch. It's a basic script (more like just a collection of commands) to set up my user profile. In the section commented as "Installs needed software" I was wondering if there was a way to create a list of the software names (in the script) and then somehow have that list called and the dropped in for the apt-get install command. I know it's not needed but it is the way I would like to do things if possible. I don't know if this can be done with an array or something like that but I was wanting to install everything from just one line. Thank you in advanced for the help.

Justin

#/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/
  • Yeah, you're right. Its really not needed. As you can see from @steeldriver's solution, the solution doesn't make your code any prettier or shorter. Passing already-installed-programs to apt isn't a problem either because it will simply skip them. – BoomShadow Feb 18 '18 at 16:11
  • 1
    If you're wiping your PC often enough that you need a script to set up your environment again, then IMO it would be more useful to stop doing whatever it is that is breaking your system to the point that it needs re-installation. Re-install from scratch is not a common or recommended way of solving problems on Linux (i.e. Linux is not Windows). One reason this is important is that if you don't understand what the cause of the problem was, you'll probably just do it again in the future. It's better to find and fix the problem. – cas Feb 19 '18 at 3:12
  • 1
    Could I trouble you for a more descriptive title for this post? It’s not quick and it’s not specific to bash. Perhaps something about installing software from a list? – Jeff Schaller Feb 20 '18 at 3:24
  • I second Jeff's comment and I'd also suggest reading How to Ask. – Anthony G - justice for Monica Feb 20 '18 at 10:21
1

One way to simplify the script is to take out all the sudo calls and call the entire script with sudo

#!/bin/bash
if [[ $(id -un) != root ]]; then
    echo "invoke me with sudo: sudo $0"
    exit
fi

echo >> /etc/fstab
# etc

Also note that you can put newlines inside an array definition to make it more readable:

pkgs=(
  autofs lame  vim  nfs-common handbrake-cli handbrake  chromium-browser 
  screen eyed3 htop clementine vorbis-tools  cdparanoia ffmpeg
)
  • Thank you, this wasn't what I was looking for in my question but it is helpful and I will be using this. – Justin Keith Feb 18 '18 at 17:56
2

You may use an array

pkgs=(autofs nfs-common handbrake-cli handbrake lame vim chromium-browser screen ffmpeg htop cdparanoia eyed3 vorbis-tools clementine)

sudo apt-get install "${pkgs[@]}"
  • Thank you, this answers my question. I will be updating my script to reflect this. – Justin Keith Feb 18 '18 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.