Skip to main content
  1. Refs/

Shell Scripts

·2 mins

Go to References #

My References #

Check version of operating system #

lsb_release -a

Check if a file exists #

file=/path/to/filename
if [[ -f "$file" ]]; then
    echo "$file exists."
fi

Check if a file does not exist #

file=/path/to/filename
if [[ ! -f "$file" ]]; then
    echo "$file does not exist."
fi

Note from StackOverflow about flags: -e True if file exists and -f True if file exists and is a regular file where, a regular file means it is not a directory, symlink, socket, device, etc. Other existence checks can be found on Linuxize

Check if a directory exists #

dir=/path/to/directoryname
if [ -d "$dir" ]; then
    echo "$dir exists and is a directory."
fi

Find lines in file which don’t start with a hash #

Useful for finding all enabled lines in a .conf file (from Unix Stack Exchange)

grep "^[^#;]" filename.conf

Check for arguments #

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Must provide args: abc, xyz"
    exit 1
fi

Change echo color #

COLOR='\033[0;36m'
NC='\033[0m' # No Color

echo -e ${COLOR}"this text should display in blue"${NC}

Comparisons #

String compare #

if [ $deploymentType == "k8s" ]; then 
    dfEnd="Dockerfile.K"
else
    dfEnd="Dockerfile.U"
fi

Loops #

Go through all files in a folder passed in #

folderName=$1
folderPath="/path/to/working_directory/$folderName"
files="$folderPath/*"

for file in $files
do
    # logic
done

Check performance of any endpoint #

curl -w "@curl-format.txt" -o /dev/null -s "https://rshmhrj.io/refs"

curl-format.txt

     time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                       ----------\n
          time_total:  %{time_total}s\n\n

Text manipulation #

Find and rename text in a file #

pageBundleName=`echo $file | sed "s/.md//" | sed "s#$folderPath/##"` # replaces drops folderpath and .md at end of file fileName
prevLine=`cat $file | sed -n '/preview:/p'` # sets variable to line of text which says 'preview:'
prevFile=`echo "$prevLine" | sed "s#preview: ##"` # removes 'preview: ' to only get the file
thumbName=`echo $prevFile | sed 's#.png#_thumb.png#' | sed 's#img/.*/##'` # replace .png > _thumb.png and remove everything after img to the last slash

Convert image to thumbnail size #

convert assets/img/$folderName/$fileName -resize 320x240 -transparent -gravity center -extent 320x240 assets/thumb/$folderName/$pageBundleName/$thumbName

Compress and extract #

tar -cvjf /path/to/outputfile.tar.bz2 /path/to/directory/to/compress # compress using bzip2 -- more compressed file
tar -cvzf /path/to/outputfile.tar.gz /path/to/directory/to/compress # compress using gzip -- faster to compress / extract

tar xvf /path/to/outputfile.tar.bz2 # extract bzip2 archive
tar xvf /path/to/outputfile.tar.gz # extra gzip archive