lvjian700
3/14/2016 - 6:12 AM

Shell script tips

Shell script tips

# function and arguments
function find_and_replace() {
  find_text=$1
  replace_test=$2
  echo "Replace $find_text to $replace_test"
}
find_and_replace "find me" "replace to me"

# if file or dir
if [ -d "$1" ]; then
  echo "Dir $1 is existed"
fi

if [ -e "$1" ]; then
  echo "File $1 is existed"
fi

# For loop
ARRAY_DATA=(build.sh update-version.sh)
function clean() {
    length=${#ARRAY_DATA[@]}
    for ((i=0; i<$length; i++))
    do
        item=${ARRAY_DATA[$i]}
        echo item
    done
}


# bash colors
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
# Usage
echo "${green}Finished.${reset}"


# grep and if
# is a rails project?
cat Gemfile | grep -q rails
if [ $? -eq 0 ]; then
    echo "y"
else
    echo "n"
fi

# rails new
# Usage
rails new
# Implement
# Filename: rails
function new() {
}
$@

# Fina and replace via silver searcher, awk, sed and xargs
# https://gist.github.com/kates/5915285
ag "sometext" --nogroup | awk '{print substr($1,1,index($1,":")-1);}' | xargs -I {} sed -i .bak -e 's/sometext/anothertext/g' {}

# splite a string
# http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
IN="app-name"
arrIN=(${IN//-/ }) #app name
# upper case first letter
echo $arrIN | awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'

# read last part of string, read version from tags
# mercury-pseeker-etl-1.1.27 =>  1.1.27
echo `git describe --abbrev=0 --tags` | awk -F- '{print $NF}'

# shell script multiple lines document
function usage() {
cat <<EOF
usage: iam-update.sh <command>

iam-update is a tool for pricing team. It use to update iam role and relavent files.

stack                  update IAM role via cloudfomration stack
policy                 update IAM role inline policy directly
cloudfomration         update cloudformation template and params files
help                   print usage
EOF
}
usage

# switch case in shell script
case "$1" in
test)
  ENV=test
  ;;
prod)
  ENV=prod
  ;;
*)
  usage
  ;;
esac

# read a file line by line
# http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line
# Reference:
# * IFS: https://bash.cyberciti.biz/guide/$IFS
# * read: https://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard
# * control input: http://billie66.github.io/TLCL/book/zh/chap30.html
# Read a configuration file:
# sqs:aws-queue-name
# email:jlv@thoughworks.com
while IFS='' read -r line || [[ -n "$line" ]]; do
  IFS=':' read -r protocol endpoint <<< "$line"
  echo "Protocol: $protocol - Endpoint: $endpoint"
done < "$file"


# process csv file
# filter start with WA row
cat 20160501T000000_list_price_sets_export.csv | grep ^WA > wa.csv
# filter not start with WA row 
cat 20160501T000000_list_price_sets_export.csv | grep -v ^WA > non-wa.csv
# count lines
cat 20160501T000000_list_price_sets_export.csv | wc -l
# extract third column
cat 20160501T000000_list_price_sets_export.csv | awk -F, '{ print $3; }'
# sort
cat 20160501T000000_list_price_sets_export.csv | awk -F, '{ print $3; }' | sort
# extract first header row
cat 20160501T000000_list_price_sets_export.csv | head -1
# sort by column 3 and extract first 3 column
cat 20160501T000000_list_price_sets_export.csv | sort -k 3 -t ',' | awk -F, '{print $1 "," $2 "," $3}'

# process multiple lines once
ls deploy/rea-shipper-*.yml | xargs -n 1 sed -i '' 's/KMS_ENCODED_/KMS_ENCRYPTED_/g'
cat deploy/rea-shipper-test.yml | grep KMS | awk -F': ' '{ print $2; }' | xargs -n 1 bundle exec ssssh decrypt | xargs -n 1 bundle exec rea-shipper kms -c deploy/rea-shipper-test.yml encrypt

# Read current folder name
CURRENT_FOLD=${PWD##*/}