Shell
★Shellの確認
$ echo $0
$ echo $SHELL
$ ps -p $$
/bin/sh --version
★Shellデバッグ
-n 実行せず文法チェック
-x それぞれのコマンドが実行した結果を表示
〇方法1
cat > trace.sh
#! /bin/bash
who | wc -l
Ctrl+D
chmod +x trace.sh
sh -x trace.sh 実行
出力:
+ who
+ wc -l
3
〇方法2
cat > trace.sh
#! /bin/bash
set -x
who | wc -l
set +x
who | wc -l
Ctrl+D
chmod +x trace.sh
./trace.sh 実行
出力:
+ who
+ wc -l
3
+ set +x
3
〇方法3
#!/bin/bash -x
★判断条件
test, [ ], [[ ]], (( ))
[ ]とtestでは正規表現が使えず
$?:0はtrue、1はfalse
test $name = andy
[ $name=Andy ]
[[ $name = [Aa]ndy ]]
[ $x -gt $y ] 或いは ((x > y))
[ $x -le $y ] 或いは ((x <= y))
[ string1 = string2 ] 或いは [ string1 == string2 ]
[ string1 != string2 ]
[ string ] 或いは [ -n string ] stringがNULLではない場合true
[ -z string ] stringがNULLの場合true
[ string1 -a string2 ] 或いは [[ cond1 && cond2 ]]
[ string1 -o string2 ] 或いは [[ cond1 || cond2 ]]
[ ! string ] 或いは [[ !cond ]]
[ int1 -eq int2 ] int1 = int2
[ int1 -ne int2 ] int1 != int2
[ int1 -gt int2 ] int1 > int2
[ int1 -ge int2 ] int1 >= int2
[ int1 -lt int2 ] int1 < int2
[ int1 -le int2 ] int1 <= int2
[ file1 -nt file2 ] file1はfile2よりnew
[ file1 -ot file2 ] file1はfile2よりold
[ -d $file ] ディレクトリが存在かどうか
[ -e $file ] ファイルが存在かどうか
[ -f $file ] ディレクトリではないファイルが存在かどうか
[ -r $file ] ファイルが存在且つ読み込め
[ -w $file ] ファイルが存在且つ書き込め
[ -x $file ] ファイルが存在且つ実行可能
論理演算子
-a -o !
test 1 -eq 1 -a 2 -eq 2; AND / OR
★I/O処理
#ユーザーからの入力1
while :
do
read key
echo "$key"
if [ $key = "end" ]; then
break
fi
done
#ユーザーからの入力2
select option in A B
do
echo "$option"
break
done
#ファイルからの入力
i=1
while read line
do
echo "$i: $line"
i=`expr $i + 1`
done <$1
★内部変数
$? コマンドの戻り値
$$ shellのpid
$0 script名
$1,$2,${10} 引数
$# 引数の数
$*,$@ 引数リスト
#! /bin/bash
for i in $*;do 出力1
for i in $@;do 出力1
for i in "$*";do 出力2
for i in "$@";do 出力3
echo $i;
done
サンプル
xxx.sh 'a b' c d
出力1
a
b
c
d
出力2
a b c d
出力3
a b
c
d
★文法
・if文
if [[ string expr ]]; then 正規表現
command
fi
if (( numeric expr )); then
command
fi
if <expr>; then
command1
elif <expr>; then
: 処理なし
elif <expr>; then
command2
else
command3
fi
if test $x -gt 50
then
...
fi
或いは
if [ $x -gt 50 ]; then
...
fi
或いは
[ $x -gt 50 ] && ...
・case文
case <変数> in
value1|value2) 正規表現、|、or
command1
;;
value3)
command2
;;
[vV]alue4)
command3
;;
*)
command4
;;
esac
・for文
for file in file[1-5]; do
if [[ -f $file ]]; then
echo "$file exist"
fi
done
for i in 1 2 3
do
...
done
a=(1 2 3)
for i in ${a[@]}
do
...
done
for i in `seq 1 5`
do
...
done
for (( i = 5; i > 0; i-- ))
do
echo $i
done
・while文
num=10
while (( num > 0 )); do
num=$((num-1))
echo $num
done
i=0
while [ $i -lt 10 ]
do
i=`expr $i + 1`
...
done
while :
do
i=`expr $i + 1`
if [ $i -eq 3 ]; then
continue
fi
done
・shift <n>
デフォルト:引数リストの左側から一つを削除
test.sh
while (( $# > 0 ))
do
echo $*
shift
done
./test.sh a b c d e
a b c d e
b c d e
c d e
d e
e
・関数
readonly FILE="xxx"
function method() {
local i = 10
echo $1
}
for i in abc def; do
method $i
done
persons=(aaa bbb ccc);
echo ${persons[1]}; bbb
echo ${persons[*]}; aaa bbb ccc
echo ${#persons[*]}; 3
persons=(aaa [3]=bbb [2]=ccc);
echo ${persons[1]}; 空
echo ${persons[2]}; ccc
echo ${persons[*]}; aaa ccc bbb
echo ${#persons[*]}; 3
a=(2 4 6)
echo $a
echo ${a[1]}
echo ${a[@]} すべて
echo ${#a[@]} 要素数
a[2]=10
a+=(20 30)
d=(`date`)
echo ${d[3]} 曜日
${<変数>:+word} 意味:NULL != <変数> ? word : <変数>
${<変数>:-word} 意味:NULL == <変数> ? word : <変数>
${<変数>:=word} 意味:NULL == <変数> ? <変数> = word : <変数>
${<変数>:offset}或いは${<変数>:offset:length}
var=abcdefgh
${var:0:4} abcd
${var:4:4} efgh
${var:2} cdefgh
${var:0} abcdefgh
${#var} 8
var="/usr/bin/local/bin"
${var%/bin*} /usr/bin/local 末尾から最小削除
${var%%/bin*} /usr 末尾から最大削除
${var#*/} usr/bin/local/bin 先頭から最小削除
${var##*/} bin 先頭から最大削除
シングルコーテーション(特殊符号・変数・コマンドがすべて無効)
echo 'What is time? $(date)' 出力:What is time? $(date)
ダブルコーテーション(特殊符号が無効だが、変数・コマンドが有効)
echo "What is time? $(date)" 出力:What is time? 日付XXX
★コマンド
variable=$(date) 或いは variable=`date`
variable=`basename \`pwd\`` 或いは variable=$(basename $(pwd))
★計算式
echo $[5+4-2] 或いは echo $((5+4-2))
`expr $x + 2`
`expr $x \* 2`
`expr \( $x + 2 \) \* 2`
declare -i num=5+5;
declare -i num="5 + 5";
echo $num; 10 ※declare -iがない場合:5+5
let i=5;
let i+=1;
echo $i; 6
let "i = i + 2";
echo $i; 8