Skip to content

supernova106/bash-firefighter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 

Repository files navigation

bash-firefighter

Curated list of useful bash techniques

Table of Contents

Helper

Generic

Check if a port is open

if [ "$(timeout 1 bash -c '</dev/tcp/localhost/9100'; echo $?)" == "0" ]; then
        echo "hi"
fi

get OS Platform

OS_PLATFORM=$(cat /etc/os-release | awk -F= '/^ID=/{print $2}' | tr -d '"')
echo $OS_PLATFORM

Exit on error

#!/bin/bash
set -e

Retry on error

User Continue

Programming

Array

Loop through indices, values

for i in "${!foo[@]}"; do
  echo "${i}: ${foo[$i]}"
done

Loop through values

for i in "${foo[@]}"; do
  echo "${i}"
done

Join method

Join array to string

foo_arr=['a','b','c']
bar=$(IFS=. ; echo "${foo_arr[*]}")

# a.b.c

Variables

Declare local variable

function foo() {
  local bar=""
}

foo
# only work with function

Declare global variable

bar=""

function foo() {
  echo "${bar}"
}

Default value

bar="${1:-default}"

echo "${bar}"
# default

Split method

Split string to array

IFS='.' read -ra arr <<< "a.b.c"

echo "${arr[@]}
# ['a', 'b', 'c']

Regex

Validate IP address

if [[ ! $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  echo -e "error! $ip is not valid"
  return 1
fi

Function in Function

foo() (
  bar() {
    echo "hello"
  }

  bar
)

Modules

inlude bash file as a module

.
├── foo.sh
└── modules
    └── utils.sh

$ cat modules/utils.sh
#!/bin/bash

function bar() {
  echo "hello"
}

$ cat foo.sh
#!/bin/bash

source modules/utils.sh

bar

Config Files

Yaml

Convert yaml file (work with up to level 2 of keys) to key=value environment variables file

  • Detect if the input file's content changes with md5() hash
  • allow bash to work with yaml for configuration automation

Requirements

  • yq
  • jq

Refer to convert_yaml_to_env.sh

Cloud Provider

AWS

Getting VPC CIDR from VPC_ID

vpc_id=${1}
aws ec2 describe-vpcs --vpc-ids $vpc_id | jq -r .Vpcs[0].CidrBlock

Getting available ENIs ID

aws ec2 describe-network-interfaces --region us-west-2 --filters Name=status,Values=available,Name=group-name,Values=<GROUP_NAME> --max-items 2 | jq -r .NetworkInterfaces[].NetworkInterfaceId

About

Curated list of useful bash techniques

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages