bash

My Note

  • Loop through files in dir
bash
for file in *.zip; do
    unzip $file
done
  • Check if file exists
bash
if [! -f ./some_file ]; then
    echo "file exists"
    # do something
else
    echo "file does not exist"
fi
  • Check if env var is set
bash
if [[ -z "${SOME_VAR}" ]]; then
    # do something
fi
  • Demo function -> using factorial
bash
#!/usr/bin/env bash
factorial() {
    if [ "$1" -gt "1" ]; then
        previous=`expr $1 - 1`
        parent=`factorial $previous`
        result=`expr $1 \* $parent`
        echo "$result"
    else
        echo 1
    fi
}

factorial "$1"