Shell Scripts

This cluster features a brief collection of bashing shell scripts. Additional line breaks are for better readability only.

Feh Show

Feh Show creates a basic slideshow using the feh command.

0000#!/bin/sh
0001
0002# creates a recursive slideshow using feh
0003# m = menu, q = quit -- man feh for more info
0004# sleep 10m adds 10 minutes delay
0005
0006# set image directory
0007DIR=/home/john/slide
0008
0009sleep 10m
0010xsetroot -solid black
0011xset -dpms
0012xset s noblank
0013xset s off
0014feh -x -F -Y -z -r -D5 $DIR

Fix permissions Debian

Fix Permissions Debian attempts to fix broken permissions on Debian or based on Debian systems.

0000#! /bin/bash
0001
0002# fix-permissions-debian
0003
0004# This script attempts to restore broken permissions on Debian or based
0005# on Debian systems, e.g. after something like a global chown root:root
0006
0007# You should almost always be able to still log into text mode of the
0008# broken system by picking the recovery option from the boot screen.
0009
0010
0011# - login as root and drop the script into /
0012
0013# - run chmod +x fix-permissions-debian     -- we want an executable
0014# - run chown /usr/local/man -R man:root    -- to avoid bonus trouble
0015# - establish a working internet connection -- e.g. nmcli
0016
0017# - grab a cuppa and feed some pizza to the microwave
0018# - run ./fix-permissions-debian
0019
0020# - wait... (enter cuppa and pizza)
0021
0022# - reboot
0023# - pick default option, close your eyes and pray...
0024# - enjoy the candy
0025
0026
0027# If you are still unable to login to X, well, then that would imply
0028# there's a lot more broken than just a few permissions and thus way
0029# beyond the scope of this little script.
0030
0031# On the contrary, if you can login to X again and although all looks
0032# fine on the surface you may yet have to do some minor extra patching,
0033# e.g. desktop files making a call to su may no longer work. Using gksu
0034# instead usually fixes the issue without much pain. YMMV
0035
0036
0037# There is nothing to be edited below this line.
0038
0039
0040perm_string_to_mode() {
0041  string="$1"
0042  let perms=0
0043  [[ "${string}" = ?r???????? ]] && perms=$(( perms +  400 ))
0044  [[ "${string}" = ??w??????? ]] && perms=$(( perms +  200 ))
0045  [[ "${string}" = ???x?????? ]] && perms=$(( perms +  100 ))
0046  [[ "${string}" = ???s?????? ]] && perms=$(( perms + 4100 ))
0047  [[ "${string}" = ???S?????? ]] && perms=$(( perms + 4000 ))
0048  [[ "${string}" = ????r????? ]] && perms=$(( perms +   40 ))
0049  [[ "${string}" = ?????w???? ]] && perms=$(( perms +   20 ))
0050  [[ "${string}" = ??????x??? ]] && perms=$(( perms +   10 ))
0051  [[ "${string}" = ??????s??? ]] && perms=$(( perms + 2010 ))
0052  [[ "${string}" = ??????S??? ]] && perms=$(( perms + 2000 ))
0053  [[ "${string}" = ???????r?? ]] && perms=$(( perms +    4 ))
0054  [[ "${string}" = ????????w? ]] && perms=$(( perms +    2 ))
0055  [[ "${string}" = ?????????x ]] && perms=$(( perms +    1 ))
0056  [[ "${string}" = ?????????t ]] && perms=$(( perms + 1001 ))
0057  [[ "${string}" = ?????????T ]] && perms=$(( perms + 1000 ))
0058  echo $perms
0059}
0060
0061# generate a list of installed packages that have files etc in /var
0062grep -l /var/ /var/lib/dpkg/info/*.list  | \
0063  sed -e 's:/var/lib/dpkg/info/::' -e 's/\.list$//' | \
0064  xargs dpkg -l | \
0065  awk '/^[hi]/ {print $2}' > /tmp/packages.list
0066
0067# clean out the apt cache so we only have one version of each package
0068apt-get clean
0069
0070# download the packages as if we were going to reinstall them
0071# NOTE: packages which are no longer available for download
0072# will not have their permissions fixed. apt-get will complain about
0073# those packages so you can get a list by redirecting or tee-ing the
0074# output of this script.
0075xargs apt-get -y -d -u --reinstall install < /tmp/packages.list
0076
0077for pkg in $(cat /tmp/packages.list) ; do
0078  PKGFILE="/var/cache/apt/archives/${pkg}_*.deb"
0079
0080  if [ -e $PKGFILE ] ; then
0081    dpkg-deb -c /var/cache/apt/archives/${pkg}_*.deb | \
0082    awk '/\.\/var\// {print $1, $2, $6}' | \
0083    sed -e 's/ /\t/' -e 's/ +/\t/g' | \
0084
0085    while IFS=$'\t' read permstring ownergroup filename ; do
0086
0087      # don't change owner/group/perms on symlinks
0088      if ! [[ "${permstring}" =~ ^l ]] ; then
0089        mode=$(perm_string_to_mode $permstring)
0090        # change "owner/group" to "owner:group" for chown
0091        ownergroup=${ownergroup//\//:}
0092        # remove leading '.' from filename
0093        filename=${filename#?}
0094cat <<__EOF__
0095            chown "$ownergroup" "$filename"
0096            chmod "$mode" "$filename"
0097__EOF__
0098      fi
0099    done
0100  echo
0101  fi
0102done

LF Sync

LF Sync syncs a local folder with a remote folder using the lftp command.

0000#!/bin/sh
0001
0002# sync local folder with remote folder using lftp
0003# configure SKIP to exclude items or leave empty
0004
0005# config
0006HOST="example.com"
0007USER="johndoe"
0008PASS="secret"
0009FROM="/local/path/to/sync"
0010SYNC="/remote/path/to/sync"
0011SKIP="FOO,bar"
0012
0013# print info
0014echo "lfsync is now performing the following task ..."
0015echo
0016echo "FROM: $FROM"
0017echo "  TO: $HOST$SYNC"
0018echo "SKIP: $SKIP"
0019echo
0020
0021# exec lftp
0022
0023# --delete removes remote files no longer present in the local source
0024# note that this will delete any new dynamically created remote files
0025# not present in the local source -- use with caution
0026# you'll probably want to import everything with your FTP client first
0027
0028lftp -f "
0029open $HOST
0030user $USER $PASS
0031lcd $FROM
0032mirror --reverse --verbose --delete --exclude-glob $SKIP $FROM $SYNC
0033bye
0034"
0035
0036# that's all folks
0037echo "All done, bye."

Mount ISO

Mount ISO mounts an ISO image with a minimum of typing.

0000#!/bin/sh
0001
0002# mount iso image at /tmp/isoname -- requires root
0003
0004# print info
0005echo "Mount ISO image at /tmp/isoname"
0006echo "Usage: miso isoname [without extension]"
0007echo
0008
0009# check root
0010if [ "$(id -u)" != "0" ]; then
0011  echo "Only root can run this script!" 2 > & 1
0012  exit 1
0013
0014else
0015  # missing iso
0016  if [ ! $1 ]; then
0017    echo "Missing ISO!"
0018
0019  # mount iso
0020  else
0021    mkdir -p /tmp/$1
0022    mount -t iso9660 -o loop /tmp/$1/$1.iso /tmp/$1
0023  fi
0024fi

Recursively Find Files By Extension

Recursively Find Files By Extension recursively finds and lists all files of given extension.

0000#!/bin/sh
0001
0002# Recursively find all files matching extension
0003# phclaus.com/shell-scripts/recursively-find-files-by-extension
0004
0005# set editor and logfile
0006EDT=nano
0007LOG=~/rfx.log
0008
0009# print header
0010echo "rfx - recursively find files by extension"
0011echo
0012echo "Syntax : rfx /path/dir extension"
0013echo "Example: rfx /foo/bar batz"
0014echo
0015
0016# remove old logfile
0017if [ -f $LOG ]; then
0018  rm -f $LOG
0019fi
0020
0021# check missing argument
0022if [ $# -eq 0 ]; then
0023  echo "No arguments provided!"
0024  exit 1
0025
0026# check valid directory
0027elif [ -d $1 ]; then
0028
0029  # check missing extension
0030  if [ $# -eq 1 ]; then
0031    echo "Missing extension!"
0032    exit 1
0033
0034  else
0035    # process query
0036    find $1 -name \*.$2 -type f > $LOG
0037
0038    # check non-empty logfile and open in editor
0039    if [ -s $LOG > 0 ]; then
0040      $EDT $LOG
0041
0042    else
0043      # print status and remove empty logfile
0044      echo "No files with extension *.$2 found!"
0045      rm -f $LOG
0046    fi
0047
0048  fi
0049
0050# check if directory argument is file
0051elif [ -f $1 ]; then
0052  echo "$1 is a file but it must be a directory!"
0053  exit 1
0054
0055else
0056  # check if directory exists
0057  echo "$1 does not exist!"
0058  exit 1
0059fi

Scrot Shot

Scrot Shot takes a screenshot using the scrot command.

0000#!/bin/sh
0001
0002# takes a screenshot using scrot
0003
0004DATE=`date +%Y-%m-%d\ %H:%M:%S`
0005import -window root "$HOME/Pictures/screen-$DATE.png"

Vanilla Crypt

Vanilla Crypt encrypts and decrypts given file using Open SSL.

0000#!/bin/sh
0001
0002# Encrypt / decrypt source using OpenSSL
0003# Script required by sender and receiver
0004
0005# print header
0006echo "Vanilla Crypt"
0007echo
0008echo "Encrypt: vac e filename extension"
0009echo "Decrypt: vac d filename extension suffix"
0010echo
0011echo "Existing file/s will be overwritten!"
0012echo
0013
0014# suffix to append to encrypted files
0015SUFFIX="vac"
0016
0017# check missing option
0018if [ $1 ]; then
0019
0020  # check valid option
0021  if [ $1 != e ]; then
0022
0023    if [ $1 != d ]; then
0024      echo "Invalid option: "$1
0025    fi
0026
0027  fi
0028
0029  # encrypt file -- change algorithm / cipher strength to your likes
0030  if [ $1 = e ]; then
0031
0032    if [ $2 ]; then
0033
0034      if [ $3 ]; then
0035        openssl aes-256-cbc -in $2"."$3 -out $2"."$3"."$SUFFIX
0036        echo
0037        echo "Successfully encrypted" $2"."$3" as "$2"."$3"."$SUFFIX
0038
0039      else
0040        echo "Missing extension!"
0041      fi
0042
0043      else
0044        echo "Missing filename / extension!"
0045
0046    fi
0047
0048  fi
0049
0050  # decrypt file -- algorithm / cipher strength must match encryption method
0051  if [ $1 = d ]; then
0052
0053    if [ $2 ]; then
0054
0055      if [ $3 ]; then
0056
0057        if [ $4 ]; then
0058          openssl aes-256-cbc -d -in $2"."$3"."$4 -out $2"."$3
0059          echo
0060          echo "Successfully decrypted" $2"."$3"."$SUFFIX" as "$2"."$3
0061
0062        else
0063          echo "Missing suffix!"
0064        fi
0065
0066        else
0067          echo "Missing extension / suffix!"
0068      fi
0069
0070      else
0071        echo "Missing filename / extension / suffix!"
0072    fi
0073
0074  fi
0075
0076fi

Word Weight

Word Weight weights the words of a given source.

0000#!/bin/sh
0001
0002# calculates word weight of given source
0003
0004# set logfile
0005LOG=$HOME/ww.txt
0006
0007# print header
0008echo "Calculates word weight of given source"
0009echo
0010echo "Example: ww http://example.com/foo/bar.baz"
0011echo "         ww /foo/bar/baz.txt"
0012echo
0013
0014# missing source, target
0015if [ ! "$1" ]; then
0016  echo "Missing source!"
0017
0018# process valid source
0019else
0020  curl $1 | tr -cs "A-Za-z" "\n" | tr "A-Z" "a-z" | 
0021    sort | uniq -c | sort -n -r > $LOG
0022
0023  echo "All done. Results have been saved in $LOG"
0024fi