Getting started shell script in production from example

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}

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}

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}\""

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'
  • 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

Run the docker tutorial 101 in localhost

1docker run  -dp 0.0.0.0:8000:80 docker/getting-started:pwd

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##*/}

Reference