Technotes

Technotes for future me

Snippets

Rename files

for file in *.yaml; do mv “$file” “$(basename “$file” .yaml).yaml.j2” done

Scrape site

wget --no-check-certificate --mirror -E --ignore-length -x -k -p -np -N https://$URL_PRD
wget --no-check-certificate --mirror -E --ignore-length -x -k -p -np -N https://$URL_PRD/sitemap.xml

Rsync

rsync -avp --delete -e "ssh -v -p22 -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no" /app/content/prd blaataap@blaataap@rsync.<accountnr>.cdn.net:/

Logging

NOW=$(date +"%d-%m-%Y-%H-%M")
logger "At $NOW Production was synchronised via rsync to CDN"

Script options

case $1 in
        deploy_acc)
        $1
        ;;
        deploy_prd)
        $1
        ;;
        *)
       echo $"Usage : $0 {deploy_acc|deploy_prd}"
       exit 1
       ;;
esac

Script arguments

#!/usr/bin/env sh

# Default values for blank parameters
DEBUG=0
IN_FILE=/etc/some-input-file.conf
OUT_FILE=/var/log/some-output-file.log

# Option parser, the order doesn't matter
while [ $# -gt 0 ]; do
    case "$1" in
        -i|--input)
            IN_FILE="$2"
            shift 2
            ;;
        -o|--output)
            OUT_FILE="$2"
            shift 2
            ;;
        --debug) # Argument acting as a simple flag
            DEBUG=1
            shift 1
            ;;
        *)
            break
            ;;
    esac
done

# Some simple argument checks
wrong_arg() {
    echo "Error: invalid value for $1" >&2
    exit 2
}

[ -f $IN_FILE ] || wrong_arg "input file"
[ -f $OUT_FILE ] || wrong_arg "output file"

# The actual script can start below
# ...

Check/Create directory

if [ -d $LOGDIR ]; then
  :
else
  mkdir -p $LOGDIR
fi

Check if file exist

FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else 
    echo "$FILE does not exist."
fi

Check if file does not exist

FILE=/etc/docker
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
fi

#
[ ! -f /etc/docker ] && echo "$FILE does not exist."

Check if Multiple Files Exist

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
    echo "Both files exist."
fi

#
[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist."
#

if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then
    echo "Both files exist."
fi
#
[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist."

File test operators

The test command includes the following FILE operators that allow you to test for particular types of files:

-b FILE - True if the FILE exists and is a special block file.
-c FILE - True if the FILE exists and is a special character file.
-d FILE - True if the FILE exists and is a directory.
-e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
-f FILE - True if the FILE exists and is a regular file (not a directory or device).
-G FILE - True if the FILE exists and has the same group as the user running the command.
-h FILE - True if the FILE exists and is a symbolic link.
-g FILE - True if the FILE exists and has set-group-id (sgid) flag set.
-k FILE - True if the FILE exists and has a sticky bit flag set.
-L FILE - True if the FILE exists and is a symbolic link.
-O FILE - True if the FILE exists and is owned by the user running the command.
-p FILE - True if the FILE exists and is a pipe.
-r FILE - True if the FILE exists and is readable.
-S FILE - True if the FILE exists and is a socket.
-s FILE - True if the FILE exists and has nonzero size.
-u FILE - True if the FILE exists, and set-user-id (suid) flag is set.
-w FILE - True if the FILE exists and is writable.
-x FILE - True if the FILE exists and is executable.

Press any key

read -n 1 -s -r -p "Press any key to continue"

Prompt To Continue

read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

Yes/No Dialog

while true; do
    read -p "Do you wish to install this program?" yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

Select From Yes/No

echo "Do you wish to install this program?"
select yn in "Yes" "No"
case $yn in
    Yes ) make install;;
    No ) exit;;
esac

Source

functions.sh

check_root () {
  if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root"
    exit 1
  fi
}
#!/bin/bash

source functions.sh
check_root

echo "I am root"

Append to a File

echo "this is a new line" >> file.txt

echo -e "this is a new line \nthis is another new line" >> file.txt

printf "Hello, I'm %s.\n" $USER >> file.txt

tee is a command-line utility in Linux that reads from the standard input and writes to both standard output and one or more files at the same time.

echo "this is a new line"  | tee -a file.txt

# no std out output
echo "this is a new line"  | tee -a file.txt >/dev/null

echo "this is a new line" | sudo tee -a file.txt

echo "this is a new line"  | tee -a file1.txt file2.txt file3.txt

Compare variable

if [ $FILES = "blaataap" ]; then
  while true; do
      read -p "Do you wish to delete this file?" yn
      case $yn in
          [Yy]* ) echo ${FILES}; rm ${FILES}; exit;;
          [Nn]* ) echo "ok, no delete for you!"; exit;;
          * ) echo "Please answer yes or no.";;
      esac
  done
fi

Conditional execution

gits commit && git push
git commit || echo "Commit failed"

Curl timeout test

# Check blaataap.com certificate expiry
for url in blaataap.com
do
  echo "${url}"
  curl --insecure -vm 2 https://"${url}" 2>&1 | awk 'BEGIN { cert=0 } /^\* SSL connection/ { cert=1 } /^\*/ { if (cert) print }' | grep -e subject -e date

  res=$?
  if test "$res" != "0"; then
    echo "the curl command failed with timeout"
    read -n 1 -s -r -p "Press any key to continue"
    echo ""
  fi

done
Last updated on 19 Oct 2022
Published on 16 Dec 2019
Edit on GitHub