Curated list of useful bash techniques
- yq, refer to install_yq.sh. Or other ways
- jq, refer to install_jq.sh
- Get local directory of script local_dir.sh
- Get OS platform get_os.sh
- Promote AWS ECR Image promote_ecr.sh
if [ "$(timeout 1 bash -c '</dev/tcp/localhost/9100'; echo $?)" == "0" ]; then
echo "hi"
fiOS_PLATFORM=$(cat /etc/os-release | awk -F= '/^ID=/{print $2}' | tr -d '"')
echo $OS_PLATFORM#!/bin/bash
set -e- Refer to retry.sh
- Refer to user_continue.sh
Loop through indices, values
for i in "${!foo[@]}"; do
echo "${i}: ${foo[$i]}"
doneLoop through values
for i in "${foo[@]}"; do
echo "${i}"
doneJoin array to string
foo_arr=['a','b','c']
bar=$(IFS=. ; echo "${foo_arr[*]}")
# a.b.cDeclare local variable
function foo() {
local bar=""
}
foo
# only work with functionDeclare global variable
bar=""
function foo() {
echo "${bar}"
}Default value
bar="${1:-default}"
echo "${bar}"
# defaultSplit string to array
IFS='.' read -ra arr <<< "a.b.c"
echo "${arr[@]}
# ['a', 'b', 'c']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
fifoo() (
bar() {
echo "hello"
}
bar
)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
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
Getting VPC CIDR from VPC_ID
vpc_id=${1}
aws ec2 describe-vpcs --vpc-ids $vpc_id | jq -r .Vpcs[0].CidrBlockGetting 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