An open API service indexing awesome lists of open source software.

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

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)

## Helper

- 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)

## Generic

### Check if a port is open

```sh
if [ "$(timeout 1 bash -c 'Exit on error

```sh
#!/bin/bash
set -e
```

### Retry on error

- Refer to [retry.sh](./scripts/retry.sh)

### User Continue

- Refer to [user_continue.sh](./scripts/user_continue.sh)

## Programming

### Array

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 method

Join array to string

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

# a.b.c
```

### Variables

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 method

Split string to array

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

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

### Regex

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
```

### Function in Function

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

bar
)
```

### Modules

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

```

## 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](./scripts/convert_yaml_to_env.sh)

## Cloud Provider

### AWS

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
```