ShellScript よく使うパターン

ShellScript ソースコピペ用

Get parameter

read -p $'\e[93mInstall(i) or Uninstall(u): ' action
if [ $action = 'i' ]; then
...
eactionlse
	echo 'Your iput is missig'
	read -p $'\e[93mInstall(i) or Uninstall(u): ' action
fi

Loop

files=(.bashrc .zshrc .bash_profile)
	for file in ${files[@]}; do
	  if [ -e $HOME/$file ]; then
			for f in *.alias; do
				...
		  done < <(ls *)
		  echo ${END} >> $HOME/$file
	  fi
	done

Replace between start and end

files=(.zshrc)
START="# import aliases start"
END="# import aliases end"
	 sed -i -e "/^${START}/,/^${END}/d" $HOME/$file

Function

START="# import aliases start"
END="# import aliases end"
func_remove_text(){
	sed -i -e "/^${START}/,/^${END}/d" $1
}
	func_remove_text $HOME/$file

引数の数チェック

if [ $# != 1 ]; then
    echo "Please specify param"
    echo "ls: dump list"
    echo "rm: remove dumpfiles"
    exit 1
fi

Specify enxtensin and loop

	for f in `find $DIR -maxdepth 1 -type f -name "*.alias"`; do
  		#echo "source $DIR/"${f%.*}"" >> $HOME/$file
			echo $f
  		echo "source $f" >> $HOME/$file
  done < <(ls *)

Exec file path

#Drive$B!J%"!<%+%$%V!K(B/This path
DIR=$(cd $(dirname $0); pwd)

OS 判定

if [ "$(uname)" == 'Darwin' ]; then
CONTAINER_PATH=~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw
elif [ "$(expr substr $(uname -s) 1 5)" == 'Linux' ]; then
CONTAINER_PATH=/var/lib/docker
elif [ "$(expr substr $(uname -s) 1 10)" == 'MINGW32_NT' ]; then
CONTAINER_PATH=
else
echo "Your platform ($(uname -a)) is not supported."
exit 1
fi

color

echo -e $'\e[96m'

表示㝮整形

docker ps --format 'table {{.Image}} {{.Names}} {{.Status}}' | column -t

指定間隔㝧実行

while true;do
  func_docker_disk_used;sleep 30;echo ""
done

実行絝果を変数へ保存

func_docker_disk_used(){
 somue
}
result=$(ls -klsh $CONTAINER_PATH)
used_total=`echo $result | awk '{ print $1 }'`
}

Progress

prog() {
    local w=80 p=$1;  shift
    clear
    # create a string of spaces, then change them to dots
    printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /.};
    # print those dots on a fixed-width space plus the percentage etc.
    printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*";
}
# test loop

func_after_exec(){
  for x in {1..100} ; do
      prog "$x"  working...
      sleep 0.04   # do some work here
  done ; echo
  clear
  globalIp
}

変換

# " を削除
ip=$(echo $result | jq '.ip' | sed 's/"//g')

Exists チェック

Dir

  if [ ! -d $SOME_PATH ]; then

  fi

File

  if [ ! -f $SOME_PATH ]; then

  fi

Path

# 現在のPATHを絶対PATHへ
cd $(cd $(dirname $0); pwd)
# 現在のPATHを絶対PATHへ から移動
cd $(cd $(dirname $0)/../../../; pwd)

env ファイル読み込み

source ./.env

ディレクトリ内のファイルをループ

for file in "$HTML_DIR"/*.html; do

ファイル名から拡張子を除去

filename=$(basename "$file" | cut -f 1 -d '.')

おっさんWEBエンジニア奮闘記©2007 WEBDIMENSION