Amazon Linux 2 - Bash Code Exercises

GitHub repo link - https://github.com/senaonp/code-demos-bash
Append paths to PATH
Allow a user to pass in paths and append them to $PATH

example usage: source update_path_variable.sh /example/path/one /example/path/two

#!/bin/bash
VAR=""
for path in "$@"
do
  VAR+="$path:"
done
export PATH="$VAR$PATH"
VAR=""
Download URL to File
curl an endpoint and output it to a file.
allow the user to specify the filename; if a filename is not specified, set it to a datestamp.

example usage: download_url_to_file.sh https://fbacarisas.xyz/ index.html

#!/bin/bash
FILENAME="$(date +'%d%m%y')_download"
if [ ! -z "$2" ]; then
  FILENAME="$2"
fi
if [ ! -f "$2" ]; then
  touch "$FILENAME"
  curl $1 > "$FILENAME"
else
  echo -n "file $2 currently exists; overwrite file? (y/n) "
  read VAR
  if [ $VAR == "y" ]; then
    curl $1 > $2
  fi
fi
Copy Source Folder to Destination Folder
copy a source folder to destination folder
allow the user to specify source and destination directories with -s and -d flags respectively

example usage: copy_source_to_destination_folder.sh -s example/path/source -d example/path/destination

#!/bin/bash
SRC=""
DEST=""
while getopts s:d: FLAG
do
  case $FLAG in
    s)
      SRC=${OPTARG};;
    d)
      DEST=${OPTARG};;
  esac
done

if [ -d "$SRC" ]; then
  if [ -d "$DEST" ]; then
    cp -R $SRC $DEST
  else
    echo "error: destination directory [$DEST] does not exist; please specify it with -d example/path"
  fi
else
  echo "error: source directory [$SRC] does not exist; please specify it with -s example/path"
fi
Select Host to SSH to
SSH to a host from an array of host options
specify the list of hosts in an array object in the script and allow the user to select the host to SSH to by its array index.

example usage:

./ssh_to_host.sh

(0) my-cloud-desktop.compute.amazonaws.com
(1) tofuni-dev.domain.com
(2) smtp-host.something.aws.com
(3) dhcp-server.example.server.com
(4) bastion-host.localserver.com

Select a host to ssh to: 2
ssh to smtp-host.something.aws.com


#!/bin/bash
OPT=""
HOSTS=("my-cloud-desktop.compute.amazonaws.com" "tofuni-dev.domain.com"
"smtp-host.something.aws.com" "dhcp-server.example.server.com" "bastion-host.localserver.com")
SSH_HOST=""
CTR=0

echo
for HOST in ${HOSTS[@]}; do
  echo \($CTR\) $HOST
  CTR=$(($CTR + 1))
done
echo

echo -n "Select a host to ssh to: "
read OPT
echo "ssh to ${HOSTS[$OPT]}"
echo
ssh ${HOSTS[$OPT]}
Datestamp Objects in Path
for all objects in a given path (files, directories, etc), add a datestamp prefix to the object name
(e.g. 19012021_myfile.txt, myfile.txt with the datestamp for 01/19/2021)

skip any files that already have the datestamp formatted prefix

if no path is specified, assume the current directory "."
note: this will also datestamp the script itself but a conditional that greps the script name (similar to how the solution greps the datestamp prefix) can skip renaming it if needed

example usage: datestamp_files.sh server-logs (datestamp all the files/directories in the path "server-logs")

#!/bin/bash
P="."
if [ ! -z $1 ]; then
  P="$1"
fi
ITEMS=$(ls "$P")

for ITEM in $ITEMS; do
  if [ ! -z $(echo "$ITEM" | egrep "^[0-9]{8,}_") ]; then
    continue
  fi
  mv "$P/$ITEM" "$P/$(date +'%d%m%Y')_$ITEM"
done
Add Full File Permissions
add full access to a file for any combination of entities (user, group, other)

allow the user to pass a flag to add access based on user, group, or other using the respective options -u, -g, and -o

example usage: add_full_access.sh -u -g example/path/myfile.txt (adds user and group access to example/path/myfile.txt)

#!/bin/bash
FILE=${@:$#}

if [ ! -f $FILE ]; then
  echo "error: file \"$FILE\" does not exist"
  exit 1
fi

while getopts ugo FLAG; do
  case $FLAG in
    u) chmod u+rwx $FILE; echo "user permissions added to $FILE";;
    g) chmod g+rwx $FILE; echo "group permissions added to $FILE";;
    o) chmod o+rwx $FILE; echo "other permissions added to $FILE";;
  esac
done
Remove All File Permissions
remove all access to a file for any combination of entities (user, group, other)

allow the user to pass a flag to remove access based on user, group, or other using the respective options -u, -g, and -o

example usage: remove_all_access.sh -g -o example/path/myfile.txt (removes group and other access to example/path/myfile.txt)

#!/bin/bash
FILE=${@:$#}

if [ ! -f $FILE ]; then
  echo "error: file \"$FILE\" does not exist"
  exit 1
fi

while getopts ugo FLAG; do
  case $FLAG in
    u) chmod u-rwx $FILE; echo "user permissions removed for $FILE";;
    g) chmod g-rwx $FILE; echo "group permissions removed for $FILE";;
    o) chmod o-rwx $FILE; echo "other permissions removed for $FILE";;
  esac
done
Python3 TestFile
utility for testing python; setup a directory and testfile and allow the user to pass in the following options

  • -d => delete the contents of the testfile
  • -e => edit the testfile
  • -r => read the contents of the testfile
  • -t => test the testfile
python3 is assumed to be installed and set in $PATH (i.e. "sudo yum install python3 ; python3 --version")

example usage: python_dev.sh -e -t (edit the python testfile and then test it)

#!/bin/bash
DEVP="$HOME/python_dev"
DEVF="$HOME/python_dev/test.rb"

if [ ! -d $DEVP ]; then
  echo "creating directory: $DEVP"
  mkdir $DEVP
fi
if [ ! -f $DEVF ]; then
  echo "creating test file: $DEVF"
  touch $DEVF
fi

while getopts dert OPT; do
  case $OPT in
    d) echo > $DEVF;;
    e) vim $DEVF;;
    r) cat $DEVF;;
    t) python3 $DEVF;;
  esac
done
Ruby TestFile
utility for testing ruby; setup a directory and testfile and allow the user to pass in the following options

  • -d => delete the contents of the testfile
  • -e => edit the testfile
  • -r => read the contents of the testfile
  • -t => test the testfile


example usage: ruby_dev.sh -d -e (delete the ruby testfile contents and then edit it)

#!/bin/bash
DEVP="$HOME/ruby_dev"
DEVF="$HOME/ruby_dev/test.rb"

if [ ! -d $DEVP ]; then
  echo "creating directory: $DEVP"
  mkdir $DEVP
fi
if [ ! -f $DEVF ]; then
  echo "creating test file: $DEVF"
  touch $DEVF
fi

while getopts dert OPT; do
  case $OPT in
    d) echo > $DEVF;;
    e) vim $DEVF;;
    r) cat $DEVF;;
    t) ruby $DEVF;;
  esac
done
Check for Security Updates and Install
query the latest security updates and prompt the user on whether to install them

example usage: apply_security_updates.sh

#!/bin/bash
echo
yum updateinfo security
echo
echo -n "apply the security updates? (y/n): "
read VAR
if [ "$VAR" == "y" ]; then
  sudo yum update --security -y
else
  echo "exiting program"
fi
Get IPv4 and IPv6 Addresses of Host
return the ipv4 and ipv6 details of the host

example usage: get_ipv4_ipv6_of_host.sh

#!/bin/bash
echo --- ipv4 details ---
ip address | egrep "inet [0-9]+"
echo
echo --- ipv6 details ---
ip address | grep "inet6"
Get Files with Extension
Query the files with a specified file extension
allow the user to pass in the file extension and optionally pass in a path to search for the files; if no path is declared, assume the current directory

example usage: ./get_files_with_extension.sh sh (get files ending in ".sh" in the current directory)

./get_files_with_extension.sh txt ./test1 (get files ending in ".txt" in the "./test1" directory)

#!/bin/bash
FP=$2
if [ ! -d $2 ]; then
  FP="."
fi

ls $FP | egrep "\.$1"