Getting started shell script in production from example
The link list to learn shell script in production practice
- github-install-go-release-cmd
- falco-install-script
curl https://meshery.io/install
- various os guideline
- The AWS SSM Document has many script to learn
- helm secrets script
- BASH Programming - Introduction HOW-TO
- Advanced Bash-Scripting Guide
Variable assign relative
1# sets SOMETHING to value if it isn't already set, and evlate the value to execute, to avoid use this
2${SOMETHING='value'}
3# If parameter not set, use default.
4${parameter-default}, ${parameter:-default}
shell
Colorize the output
1print_green() {
2 BOLD_GREEN=$(tput bold ; tput setaf 2)
3 NORMAL=$(tput sgr0)
4 echo "${BOLD_GREEN}$1${NORMAL}"
5}
6
7print_yellow() {
8 BOLD_YELLOW=$(tput bold ; tput setaf 3)
9 NORMAL=$(tput sgr0)
10 echo "${BOLD_YELLOW}$1${NORMAL}"
11}
12
13print_red() {
14 BOLD_YELLOW=$(tput bold ; tput setaf 1)
15 NORMAL=$(tput sgr0)
16 echo "${BOLD_YELLOW}$1${NORMAL}"
17}
18
19print_blue() {
20 BOLD_YELLOW=$(tput bold ; tput setaf 4)
21 NORMAL=$(tput sgr0)
22 echo "${BOLD_YELLOW}$1${NORMAL}"
23}
shell
Chooise the menu for aws login
1# Set a prompt for user input
2PS3='Please enter your choice:'
3
4# Define the available options
5options=("dev" "uat" "prod")
6default_select="dev"
7
8# Ask for user input
9select opt in "${options[@]}" "quit"; do
10 case "$REPLY" in
11 [1-3])
12 echo "You select $opt environment"
13 default_select=$opt; break;;
14 $((${#options[@]}+1)))
15 echo "Goodbye!"; exit 0;;
16 *)
17 echo "Anythins not choice."; continue;;
18 esac
19done
20
21# Set the default profile
22export AWS_PROFILE="${default_select}"
23
24# Login to AWS with the default profile
25echo "SSO login to aws use ${default_select} profile!"
26aws sso login --profile $default_select
27echo "You can exec: export AWS_PROFILE=\"${default_select}\""
shell
Execution the cleanup when shell script exit
1#!/bin/sh -e
2cleanup() {
3 rm -f '/var/folders/r2/dqq64b157nz7nz_xtxfy33s80000gn/T/xxxxx-open-terminal_A99E78E2.sh'
4}
5trap cleanup EXIT
6'/Applications/xxxxx.app/Contents/MacOS/bin/xxxxxx' '-m' 'ubuntu'
shell
- when exec
/var/folders/r2/dqq64b157nz7nz_xtxfy33s80000gn/T/xxxxx-open-terminal_A99E78E2.sh; exit
will delete script self
Dynamic add cron schedule in linux
1echo "0 0 * * * docker system prune -a -f" >> /var/spool/cron/crontabs/root
2sudo chmod +x /var/spool/cron/crontabs/root
3echo "*/30 * * * * aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin xxxxxxx.dkr.ecr.us-east-1.amazonaws.com" >> /var/spool/cron/crontabs/root
shell
Run the docker tutorial 101 in localhost
Extract the sub string use shell script
1# use the cut command
2imageName=$(echo ${{ matrix.images }} | cut -d ':' -f1)
3current_tag=$(echo ${{ matrix.images }} | cut -d ':' -f2)
4
5# use the awk command
6major=$(echo $current_tag | awk -F. '{print $1}')
7minor=$(echo $current_tag | awk -F. '{print $2}')
8patch=$(echo $current_tag | awk -F. '{print $3}')
9((patch++))
10
11# use parameter expand
12# replace all colon to dash
13JSON_FILE_NAME=${JSON_FILE_NAME//:/-}
14# split the IMAGE variable dlimit / and extract the last section
15JSON_FILE_NAME=${IMAGE##*/}
shell
Read a file
The condition statement
1#!/bin/bash
2
3echo "Please enter a number: "
4read num
5
6if [ $num -gt 0 ]; then
7 echo "$num is positive"
8elif [ $num -lt 0 ]; then
9 echo "$num is negative"
10else
11 echo "$num is zero"
12fi
shell
The loop statement
The cron script
Schedule | Description Example |
---|---|
0 0 | Run a script at midnight every day 0 0 /path/to/script.sh |
/5 | Run a script every 5 minutes /5 /path/to/script.sh |
0 6 1-5 | Run a script at 6 am from Monday to Friday 0 6 1-5 /path/to/script.sh |
0 0 1-7 | Run a script on the first 7 days of every month 0 0 1-7 /path/to/script.sh |
0 12 1 | Run a script on the first day of every month at noon 0 12 1 /path/to/script.sh |
Reference
- 1000+ DevOps Bash Scripts
- AWS EKS packer scripts
- consider starting bash scripts with these options
- gitlab runner bash scripts
- Advanced Shell Scripting Techniques: Automating Complex Tasks with Bash
- 6 Techniques I Use to Create a Great User Experience for Shell Scripts
- Linux from scratch
- Command line reference for all OS