shell位置参数默认只能用$1~$9表示,但并不表示我们只能传递9个位置参数
使用shift可以让我们拥有使用更多位置参数和复杂功能的处理能力
一个生产案例:
#!/bin/bash
# Added by royoy
# Date : 07/2019
# Desc : SQL query and so on
# Usage: ./db.sh '[dev|staging|product]' SQL
# Eg : ./db.sh staging "select ..."
env="$1"
shift
db(){
echo -e "The results of the execution is as follows:\n"
docker run --rm -i \
-v /tmp:/tmp \
-e PGHOST=uas.cfgxco79q1h9.ap-northeast-1.rds.amazonaws.com \
-e PGDATABASE=$env \
-e PGUSER=$env \
-e PGPASSWORD=$env \
-e PAGER=less \
postgres:alpine psql -c "`echo "$@" | sed 's/\*/\\*/g' |sed "s/'/\\\'/g"`"
}
[[ "dev staging product" =~ "$env" ]] && db "$@" || exit 1