not logged in | [Login]

Shell cookbook and tools

Commands

Netcat: Test for listening ports

nc -z -v <IP> <PORT>
nc -z -v 192.168.1.100 80

Bash conditionals

if [[ $VAR == "foo" ]]; then
  echo "foo!"
elif [[ $VAR == "bar" ]]; then
  echo "bar!"
else
  echo "Neither foo nor bar!"
fi

Bash Strings

# Check if a variable is not empty
NAME="ronnie"
if [[ -n "$NAME" ]]; then
    echo "NAME is not empty"
fi

# Check if a variable is empty
NAME=""
if [[ -z "$NAME" ]]; then
    echo "NAME is empty"
fi

# String contins
if [[ "$NAME" == *"ron"* ]]; then
  echo "Name contains ron"
fi

OS detection

if [ "$(uname)" == "Darwin" ]; then
    # Do something under Mac OS X platform        
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    # Do something under GNU/Linux platform
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
    # Do something under Windows NT platform
fi
Source Detect OS

Using find

# execute `process` once for each file
find . -name \*.txt -exec process {} \;

# execute `process` once with all the files as arguments*:
find . -name \*.txt -exec process {} +

# using xargs*
find . -name \*.txt -print0 | xargs -0 process

# using xargs with arguments after each filename (implies one run per filename)
find . -name \*.txt -print0 | xargs -0 -I{} process {} argument
Source using find

Tools

  • ./jq - jq is a lightweight and flexible command-line JSON processor.
  • ShellCheck - A static analysis tool for shell scripts.
  • bash-git-prompt - An informative and fancy bash prompt for Git users

Resources