https://github.com/supernova106/bash-firefighter
Curated list of useful bash techniques
https://github.com/supernova106/bash-firefighter
bash yaml yaml-parser
Last synced: about 2 months ago
JSON representation
Curated list of useful bash techniques
- Host: GitHub
- URL: https://github.com/supernova106/bash-firefighter
- Owner: supernova106
- License: apache-2.0
- Created: 2020-03-20T18:14:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-11T06:13:49.000Z (about 4 years ago)
- Last Synced: 2025-07-01T01:05:34.209Z (12 months ago)
- Topics: bash, yaml, yaml-parser
- Language: Shell
- Size: 62.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bash-firefighter
Curated list of useful bash techniques
## Table of Contents
- [Helper](#user-content-helper)
- [Generic](#user-content-generic)
- [Programming](#user-content-programming)
- [Array](#user-content-array)
- [Join](#user-content-join-method)
- [Variables](#user-content-variables)
- [Split](#user-content-split-method)
- [Regex](#user-content-regex)
- [Modules](#user-content-modules)
- [Function in Function](#user-content-function-in-function)
- [Config Files](#user-content-config-files)
- [Convert YAML to key=value](#user-content-yaml)
- [Cloud Provider](#user-content-cloud-provider)
- [AWS](#user-content-aws)
- yq, refer to [install_yq.sh](./scripts/install_yq.sh). Or [other ways](https://mikefarah.gitbook.io/yq/#on-ubuntu-16-04-or-higher-from-debian-package)
- jq, refer to [install_jq.sh](./scripts/install_jq.sh)
- Get local directory of script [local_dir.sh](./scripts/local_dir.sh)
- Get OS platform [get_os.sh](./scripts/get_os.sh)
- Promote AWS ECR Image [promote_ecr.sh](./scripts/promote_ecr.sh)
### Check if a port is open
```sh
if [ "$(timeout 1 bash -c 'Exit on error
```sh
#!/bin/bash
set -e
```
- Refer to [retry.sh](./scripts/retry.sh)
- Refer to [user_continue.sh](./scripts/user_continue.sh)
Loop through indices, values
```sh
for i in "${!foo[@]}"; do
echo "${i}: ${foo[$i]}"
done
```
Loop through values
```sh
for i in "${foo[@]}"; do
echo "${i}"
done
```
Join array to string
```sh
foo_arr=['a','b','c']
bar=$(IFS=. ; echo "${foo_arr[*]}")
# a.b.c
```
Declare local variable
```sh
function foo() {
local bar=""
}
foo
# only work with function
```
Declare global variable
```sh
bar=""
function foo() {
echo "${bar}"
}
```
Default value
```sh
bar="${1:-default}"
echo "${bar}"
# default
```
Split string to array
```sh
IFS='.' read -ra arr <<< "a.b.c"
echo "${arr[@]}
# ['a', 'b', 'c']
```
Validate IP address
```sh
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
```
```sh
foo() (
bar() {
echo "hello"
}
bar
)
```
inlude bash file as a module
```sh
.
├── 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](./scripts/convert_yaml_to_env.sh)
Getting VPC CIDR from VPC_ID
```sh
vpc_id=${1}
aws ec2 describe-vpcs --vpc-ids $vpc_id | jq -r .Vpcs[0].CidrBlock
```
Getting available ENIs ID
```sh
aws ec2 describe-network-interfaces --region us-west-2 --filters Name=status,Values=available,Name=group-name,Values= --max-items 2 | jq -r .NetworkInterfaces[].NetworkInterfaceId
```