LoginSignup
2
1

More than 3 years have passed since last update.

bash shell に関するtips 備忘録

Posted at

実行時にデバッグ出力する場合のヘッダ

#!/bin/bash -x

root以外が実行すると処理せずに終了させる

if [ $(whoami) != "root" ]; then
        echo "This script must be run as root"
        exit 1
fi

スペースの入ったファイル名を処理するために配列の分割子を改行コードに固定する

export IFS=$'\n'

ので、上記設定以降はshell内での配列指定は以下の例のように記述する必要がある

EXTLIST=(
"mov"
"mkv"
"..avi"
"avi.mp4"
"wmv.mp4"
"flv.mp4"
"mpeg"
"mpg"
"flv"
"rmvb"
"wmv"
"M4A"
"avi"
"ts")

rootから全てのユーザのjavaに対してgc実行リクエストする

for line in `ps aux | grep java | tr -s " " | grep -v grep | tr " " "," | cut -d ',' -f 1-2`
do
        arr=( `echo $line | tr -s ',' ' '`)
        user=${arr[0]}
        pid=${arr[1]}
        sudo -u $user jmap -histo:live $pid
done

lockファイルを作らずに、shellの複数起動をしないようにする

参考 : https://qiita.com/hit/items/e95298f689a1ee70ae4a

_pcnt=`pgrep -fo ${CMDNAME} | wc -l`               
if [ ${_pcnt} -gt 1 ]; then                        
        echo "This script has been running now. proc : ${_pcnt}"
        exit 1                                           
fi
2
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1