rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

$を前置して表現する特殊文字 bashシェル

2021 Feb. 27.
2021 Feb. 21.

bash上にて)
シェルにヌル文字・改行文字・タブ文字を与えるには、ドル記号を前置してシングルクォーテーションで括る。
$'\0' null文字
$'\n' 改行
$'\t' タブ文字

これは文字列にも使える
$'abc\nde'

findコマンドでヒットした、null文字で区切られたファイルリストをreadコマンドで読み込んで処理する

while read -d $'\0' File; do
    // DO SOMETHING
done < <(find DIR/ -print0)


/etc/passwdからawkで取り出し、改行で区切られたユーザー名リストをreadコマンドで読み込んで処理する

UserList=$(cat /etc/passwd | awk -F ":" '{print $1}')
while read -d $'\n' User; do
  echo $User
done < <(echo "$UserList")


echoコマンドに与える文字列のいろいろなパターン

echo abc$'\n'de
abc
de

echo "abc$'\n'de"
abc$'\n'de

echo 'abc$'\n'de'
abc$nde

echo -e abc$'\n'de
abc
de

echo -e "abc$'\n'de"
abc$'
'de

echo -e 'abc$'\n'de'
abc$nde

echo abc\nde
abcnde

echo "abc\nde"
abc\nde

echo 'abc\nde'
abc\nde

echo -e abc\nde
abcnde

echo -e "abc\nde"
abc
de

echo -e 'abc\nde'
abc
de

$ echo $abc\nde
nde

echo $"abc\nde"
abc\nde

echo $'abc\nde'
abc
de

echo -e $abc\nde
nde

echo -e $"abc\nde"
abc
de

echo -e $'abc\nde'
abc
de

システムからのメールの保管量を半減するbashスクリプト

2021 Apr. 25.
2021 Mar. 11.
2021 Feb. 21.

システムからのメールが保管される~/mboxの前半を削除する。
MaxMboxSizeに指定したファイルサイズを超えるmboxに適用する。
下記スクリプトをcronで定期起動する。

スクリプト保管場所
https://bitbucket.org/arsmus/shell-script-public/src/master/linux-utility/reduceMbox.sh

シェルスクリプトの多重起動を防ぐbashスクリプト

2021 Mar. 11.
2021 Feb. 20.

シェルスクリプトと同じプロセス名で、プロセスIDとグループプロセスIDのいずれもがシェルスクリプトのものと異なるプロセスがあれば多重起動と判断する

## Is another process of same name running?
##   If there is a process that Both PID and PGID are different from this script'
s, another process of same name as this process is running.
##   Abort if another process of same name as this process is running.

CmdPath="$0"
# get GID of this process
ProcList=$(ps ax -o pid,ppid,pgid)
PidLines=$(echo "${ProcList}" | grep $$)
Gid=$(echo "$PidLines" | awk -v Pid=$$ '{if ($1 == Pid) print $3}')

# get PID that might be another process whose name is same as this script
OtherPid=$(pgrep -fo "$CmdPath")

# If OtherPid and Gid are different from this script's, another process is running.
if [ $$ -ne $OtherPid ]; then
  OtherGid=$(echo "$ProcList" | awk -v Pid=$OtherPid '{if ($1 == Pid) print $3}')
  if [ $OtherGid -ne $Gid ]; then
    echo "Another ${CmdPath} is running." 1>&2
    echo "Aborted." 1>&2
    exit 1
  fi
fi

psコマンドですべてのプロセスのPID, PPID, コマンド名を出力する

2021 Feb. 20.

psコマンドですべてのプロセスのPID, PPID, PGID, コマンド名を出力する

ps ax -o pid,ppid,pgid,comm    ## 簡易コマンド名表示
ps ax -o pid,ppid,pgid,command    ## 詳細コマンド名表示

pgrepコマンドについて

2021 Feb. 25.
2021 Feb. 20.

参考元 Linux - プロセス名とは何か?(pgrep)|teratail

オプションを付けない"pgrep STRING"はパスの付かないプロセス名リストに対してSTRINGを検索する。
"pgrep -f STRING"はフルパス付きプロセス名リストに対してSTRINGを検索する


"pgrep STRING"は"ps -e"のプロセス名とのマッチを調べる。
"ps -e"のプロセス名はパスの付かないコマンド名である。
STRING全体がマッチする"ps -e"のプロセス名を調べる。
"ps -e"のプロセス名は16文字目以降は切り捨てられている。
STRING文字列が16文字以上だと"ps -e"にマッチしないので、pgrepでもマッチしない。

"pgrep -f STRING"は"ps -ef"のプロセス名とのマッチを調べる。
"ps -ef"のプロセス名はフルパス付きコマンド名である。
STRING全体がマッチする"ps -ef"のプロセス名を調べる。

同名のコマンドが次のように複数存在するとする。
/usr/local/bin/command.sh
/{HOME}/USER/bin/command.sh

両方のcommand.shを起動状態にする。

一方のcommand.shの中で pgrep -f "$0" を実行しても、$0にはコマンドのフルパスが入っているのでpgrepでは他方のcommand.shプロセスはマッチしない。

シェルスクリプトの$0には起動したコマンドのフルパス("which COMMAND"の返り値と同じ)が入る

2021 Feb. 20.

シェルスクリプトの$0には起動したコマンドのフルパス("which COMMAND"の返り値と同じ)が入る。

pidofコマンドについて

2021 Feb. 25.
2021 Feb. 19.

pidofコマンド

pidofコマンドは、pidofの引数に指定した文字列をpsコマンドで出力されるプロセスリストに対して検索してマッチしたプロセスIDを出力すると考えておけばよい。