Skip to content

Instantly share code, notes, and snippets.

@AndreaGhizzoni
Last active January 11, 2018 13:08
Show Gist options
  • Save AndreaGhizzoni/3170739f78f7907ba48af95d9b4cbda9 to your computer and use it in GitHub Desktop.
Save AndreaGhizzoni/3170739f78f7907ba48af95d9b4cbda9 to your computer and use it in GitHub Desktop.
bash-scripting-references.md

BASH Scripting References

Sha-Bang

#!/usr/bin/env bash

Script Parametes

script.bash

#!/usr/bin/env bash

echo "$0 is the script name" 
echo "$1 first parameter"
echo "$2 second parameter"

echo "$# total number of parameters"
echo -n "all the parameters as list: "
echo "$@" 

Output:

$ ./script.bash Hello World
./script.bash is the script name
Hello first parameter
World second parameter
2 is the total number of parameters
all the parameters as list: Hello World 

Check if script run as root

script.bash

#!/usr/bin/env bash

if [ "$EUID" -ne 0 ]; then
    echo "Hello Root!"
else
    echo "Hello NON-Root!"
fi

Output:

$ sudo ./script.bash
Hello Root!
$ ./script.bash
Hello NON-Root!
#!/usr/bin/env bash

RED='\033[0;31m'
NC='\033[0m'
echo -e "${RED}this text is red.${NC} This is white"

ANSI escape codes

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

script.bash

#!/usr/bin/env bash

function foo () {
    echo "$0 = script name"
    echo "$1 = first function argument"
}

foo "Hello"

Output:

$ ./script.bash
./script.bash = script name
Hello = first function argument

Check if directory exists

script.bash

#!/usr/bin/env bash

DIR=/usr/bin
if [ -d "$DIR" ]; then
    echo "$DIR exists"
else
    echo "$DIR do not exists"
fi

Output:

$ ./script.bash
/usr/bin exists

Check if variable is empty

script.bash

#!/usr/bin/env bash

var=""
if [ -z "$var" ]; then
    echo "\$var is empty"
else
    echo "\$var is NOT empty"
fi

Output:

$ ./script.bash
$var is empty

Checking Dependencies

script.bash

#!/usr/bin/env bash

RED='\033[0;31m'
NC='\033[0m'     # No color

# check if required packages are installed.
# if no dependencies required for this script, just skip it without modify.
# insert the required packages, space separated.
dep_req=( )
dep_not_found=( ) # DO NOT EDIT THIS ARRAY
i=0
for dep in "${dep_req[@]}"
do
    q=$(dpkg-query -W -f='${Status}' "$dep" 2>/dev/null | grep -c "ok installed")
    if [ "$q" -eq 0 ]; then
        dep_not_found[$i]=${dep}
        ((i++))
    fi
done
if [ ${i} -ne 0 ]; then
    echo -e "${RED}Abort: Dependency package/s not found: ${NC}"
    echo -n "${dep_not_found[@]}"
    echo ""
    exit 1
fi

echo "Dependencies OK!"

Output:

$ ./script.bash
Abort: Dependency package/s not found: 
IDoNotExist

script.bash

#!/usr/bin/env bash

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

Output:

$ ./script.bash
Are you sure? y
do dangerous stuff
$ ./script.bash
Are you sure? n
$
$ ./script.bash
Are you sure? o
$

Conditional Evaluating

script.bash

#!/usr/bin/env bash

A=""
# if A is empty then set B to 'wlan0', otherwise set to A
B="${A:-wlan0}"
echo $B

Output:

$ ./script.bash
wlan0
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment