bash Scripts

These were all written for Debian based systems. They may not work elsewhere as is. You are free to edit them as you wish.

Quicklinks: makesh.sh, admintools.sh, ripper.sh, xcf2pngAll.sh, Set1368x768x60.sh, DiskSpaceMonitor2.sh, colors.sh, backupDir.sh, BULocalFiles.sh

makesh.sh

Builds a bash script file with a header and opens it for editing.

#!/bin/bash
# Name: makesh.sh
# Description: Builds a Bash script file with header and opens it for editing.
# Path: ~/bin/makesh.sh
# Arguments: none
# Usage: ~/bin/makesh.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# Nov 17, 2019: Ver 0.10 Created - JAD
# Jan 08, 2020: Ver 1.0  Test for empty script name, test for existing script name, combine both files of version 0.10 into one. - JAD
##################################

# Start Script #

# DEBUG
# set -xv

# Test for file
read -p "Enter script name (ie. script.sh): " SCRIPT

if [ -z $SCRIPT ]; then
	echo "You must enter a name for the script."
	echo "Please try again."
	$(basename $0) && exit
fi

if test -f "$SCRIPT"; then
	echo "Filename $SCRIPT already exists."
	echo "Please try another filname."
	$(basename $0) && exit
fi

# Build the script
echo "#!/bin/bash" > $SCRIPT
echo "# Name:" $SCRIPT >> $SCRIPT
read -p "Description: " DESC
echo "# Description:" $DESC >> $SCRIPT 
read -p "Path : " PATH
echo "# Path:" $PATH >> $SCRIPT
read -p "Arguments : " ARGS
echo "# Arguments:" $ARGS >> $SCRIPT
read -p "Command string : " USAGE
echo "# Usage:" $USAGE >> $SCRIPT
read -p "Authors name : " AUTHOR
echo "# Author:" $AUTHOR >> $SCRIPT
read -p "Authors email address : " EMAIL
echo "# email:" $EMAIL >> $SCRIPT
echo "# Version: BETA" >> $SCRIPT
echo "##################################" >> $SCRIPT
echo "# History" >> $SCRIPT
read -p "Creation date (ie Jan 01, 2020) : " CREATED 
echo "#" $CREATED": Created" >> $SCRIPT
echo "##################################" >> $SCRIPT
echo "# Start Script #" >> $SCRIPT
echo "# End Script #" >> $SCRIPT
echo "# EOF #" >> $SCRIPT

# Change permissions
/bin/chmod 640 $SCRIPT

# Open new file for editing
/bin/nano $SCRIPT

# End Script #
# EOF #

admintools.sh

A menu of various helpful tools

#!/bin/bash
# Name: admintools.sh
# Description: Menu of various tools
# Path: ~/bin/
# Arguments: none
# Usage: ./admintools
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# Jan 09, 2020: Created - JAD
##################################
# Start Script #

WHAT="SYSADMIN MENU"
PROMPT="Select a menu item:"
OPTIONS=("System Info" "Kernel Info" "Performance" "Disk Space" "IP Address" "Open Ports" "List Installed Packages" "Directory Size" "Service Status" "uptime")

clear

echo $WHAT
PS3=$PROMPT

select CHOICE in "${OPTIONS[@]}" "Quit"; do 

    case "$REPLY" in

    1 ) clear;lshw -short | more;;
    2 ) clear;uname -srv;;
    3 ) clear;top;;
	4 ) clear;df -aH;;
	5 ) clear;ifconfig;;
	6 ) clear;netstat -plntu;;
	7 ) clear;dpkg --get-selections | grep -v deinstall | more;;
	8 ) clear;read -p "Please enter the directory path: " DIRP;du -sh $DIRP;;
	9 ) clear;read -p "Please enter the service name: " SRVNAME;systemctl status $SRVNAME;;
	10 ) clear;uptime;;

    $(( ${#OPTIONS[@]}+1 )) ) break;;
    *) echo "Invalid option. Try another one.";continue;;

    esac

done

# End Script #
# EOF #

ripper.sh

Wrapper for “abcde” to rip music CD’s to FLAC and MP3. Requires package “abcde”.

#!/bin/bash
# Name: ripper.sh
# Description: Wrapper for abcde to rip music CD's to FLAC and MP3
# Path: ~/bin/ripper.sh
# Arguments: None
# Usage: ~/bin.ripper.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 0.1
##################################
# History
# Nov 17, 2019: Created
# Dec 17, 2019: Added Select statement for exit
##################################

# Start Script #

# Clear screen
clear
# Prompt load
read -p "Please load CD into drive and press enter to continue"
# Allow time to init
echo "Loading disc..."
sleep 5

# Music Directory
cd ~/Music
# Command
/usr/local/bin/abcde -o 'flac:-8,mp3:-b 320' -G -x

#select
PS3=Enter:
echo ""
echo "Do you need to rip another disc?"
select aq in "Yes" "Quit";
do case $aq in
	Yes ) ripper.sh;;
	Quit ) exit;;
esac 
done
# End Script #
# EOF #

xcf2pngAll.sh

Converts all xcf files in directory to png. Requires package xcftools.

#!/bin/bash
# Name: xcf2pngAll.sh
# Description: Converts all xcf files in directory to png
# Path: ~/bin/xcf2pngAll.sh
# Arguments: none
# Usage: Execute from within directory to be affected. Requires xfc2png.
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# Dec 29, 2019: Created
##################################

# Start Script #

for i in *.xcf;
	do xcf2png -f $i -o $(cut -d'.' -f 1 <<< $i).png;
	done

# End Script #
# EOF #

Set1368x768x60.sh

Sets display resolution to 1368×768 60hz.

#!/bin/bash
# Name: Set1368x768x60.sh
# Description: Set display resolution
# Path: ~/bin/Set1368x768x60.sh
# Arguments: none
# Usage: ./Set1368x768x60.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: BETA
##################################
# History
# Jan 09, 2020: Created
##################################
# Start Script #

xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync
xrandr --addmode HDMI-0 1368x768_60.00
xrandr --output HDMI-0 --mode "1368x768_60.00"

# End Script #
# EOF #

DiskSpaceMonitor2.sh

To monitor disk space and report usage over the Alert value via mailx. This is meant to be run as a cronjob. Requires mailx.

#!/bin/bash
# Name: DiskSpaceMonitor2.sh
# Description: To monitor disk space and report usage over the Alert value via mailx. This is meant to be run as a cronjob. Requires mailx.
# Path: ~/bin/
# Arguments: none
# Usage: ./DiskSpaceMonitor2.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.1
##################################
# History
# Dec 08, 2016: Created - JAD
# Dec 20, 2016: Changed alert value to a variable - JAD
# Jan 09, 2020: Cleaned up standardized - JAD
##################################
# Start Script #

# DEBUG
# set -xv

# vars
MSERVER=	#Outgoing SMTP Server
MAILFROM=	#Can be a fake address
MAILTO=		#Recipient
ALERT=80	#Threshhold percentage

# find used percentage on mount points and feed to "do"
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read OUTPUT;
do
  echo $OUTPUT
  PERCENTU=$(echo $OUTPUT | awk '{ print $1}' | cut -d'%' -f1  )
  MNTPNT=$(echo $OUTPUT | awk '{ print $2 }' )
  if [ $PERCENTU -ge $ALERT ]; then
    echo "Mount point \"$MNTPNT\" on $(hostname) is getting low on space. Usage of \"($PERCENTU%)\" reported on $(date)." |
     mailx -S smtp=$MSERVER -r $MAILFROM -s "Alert: Almost out of disk space $PERCENTU%" $MAILTO
  fi
done

# End Script #
# EOF #

colors.sh

Displays a grid of ANSI color codes

#!/bin/bash
# Name: colors.sh
# Description: Displays a grid of ANSI color codes
# Path: ~/bin/
# Arguments: none
# Usage: ./colors.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# Sep 28, 2019: Created - JAD
##################################
# Start Script #

for((i=16; i<256; i++)); do
    printf "\e[48;5;${i}m%03d" $i;
    printf '\e[0m';
    [ ! $((($i - 15) % 6)) -eq 0 ] && printf ' ' || printf '\n'
done

# End Script #
# EOF #

backupDir.sh

This script creates a tar file of a directory

#!/bin/bash
# Name: nackupDir.sh
# Description: Archives listed directory. Is meant to be ran as a cronjob.
# Path: ~/bin/
# Arguments: filenames
# Usage: ./backupDir.sh Source Destination
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# Jan 15, 2020: Created
##################################
# set -xv

# Start Script #

# Path to backup. 
PATHtoBU=${1%/}
# Directory only
BUDIR=${PATHtoBU##*/}

# Path to backup to.
destDir=${2%/}

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archiveFile="$hostname-$BUDIR-$day.tgz"

# Print start status message.
echo -e "\nBacking up $PATHtoBU to $destDir/$archiveFile\n"
date

# Backup the files using tar.
tar -cvzf $destDir/$archiveFile $PATHtoBU

# Print end status message.
echo -e "\nBackup finished"
date

# End Script #
# EOF #

BULocalFiles.sh

TAR files for local users

#!/bin/bash
# Name: BULocalFiles.sh
# Description: Backup Local Files
# Path: ~/bin/
# Arguments: none
# Usage: ./BULocalFiles.sh
# Author: James A. Deck
# email: jim@jimdeck.com
# Version: 1.0
##################################
# History
# unknown: Created
# Jan 22, 2020: Rewritten to add checks - JAD
##################################
# Start Script #

# DEBUG
# set -xv

# What to backup. Edit as you see fit.
backup_files="/home /var/spool/mail /var/www /etc /root /boot /opt /usr/local/bin"

# Where to backup to.
dest=

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
log_file="$hostname-$day.log"

# Check to see if file already exists
if [ -e $archive_file ]; then
	echo -e "PROGRAM HALTED: TAR file already exists" 2>&1 | tee -a $dest/$log_file
	date 2>&1 | tee -a $dest/$log_file
	exit 2>&1 | tee -a $dest/$log_file
else
	# Backup the files using tar.
	if [ -d "$dest" ]; then
		if [ -d "$backup_files" ]; then
			echo -e "+++++++++++++++++++++" 2>&1 | tee -a $dest/$log_file
			echo -e "Backing up $backup_files to $dest/$archive_file: " 2>&1 | tee -a $dest/$log_file
			date 2>&1 | tee -a $dest/$log_file
			tar czvf $dest/$archive_file $backup_files 2>&1 | tee -a $dest/$log_file
			echo -e "Backup finished: " 2>&1 | tee -a $dest/$log_file
			date 2>&1 | tee -a $dest/$log_file
			echo -e "+++++++++++++++++++++" 2>&1 | tee -a $dest/$log_file
		else
			echo -e "PROGRAM HALTED: No files to backup" 2>&1 | tee -a $dest/$log_file
			date 2>&1 | tee -a $dest/$log_file
			exit 2>&1 | tee -a $dest/$log_file
		fi
	else
		echo -e "PROGRAM HALTED: Invalid destination directory" 2>&1 | tee -a $dest/$log_file
		date 2>&1 | tee -a $dest/$log_file
		exit 2>&1 | tee -a $dest/$log_file
	fi
fi

# End Script #

# EOF #