Bashプログラミング学習ノート(二)


読み書きシェル変数
コマンド代替オペレータとその説明
$variableは変数の値を得、初期化されていない場合はNullが初期化されていないためNULLに戻る
${variable}は変数の値を得ます.初期化されていない場合はNullです.通常、他のものを変数値に追加するときに同上を使用します.
${variable:-string}は変数の値を得ます.この変数が定義されていない場合、決定された値variableが存在し、空の値ではない場合、変数の値を返します.nullを返します.
${variable:=string}変数が定義されていない場合は既知の値を割り当て、この値variableが空でない場合は値を返します.そうしないとstringはvariableに割り当てられ、stringはstringを返します.
${variable:?string}変数が定義されていない場合、メッセージvariableが存在し、空の値ではない場合、変数の値を返します.そうしないと、文字列variableが表示され、その後messageが表示されます.
${variable:+string}変数が存在するかどうかをテストします.variableが存在し、空の値ではない場合はstringを返します.そうしないとnullを返します.
yaoyuan-desktop$echo $name

yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$echo $place

yaoyuan-desktop$echo ${name:-John} ${place:-Portland}
yaoyuan Portland
yaoyuan-desktop$echo ${place?"Not defined"}
bash: place: Not defined
yaoyuan-desktop$echo ${name:+"Not defined"}
Not defined
yaoyuan-desktop$echo ${place:+"Not defined"}

yaoyuan-desktop$echo ${place:="San Francisro"}
San Francisro
yaoyuan-desktop$echo ${name:-John} ${place:-Portland}
yaoyuan San Francisro

',',*,の使用
yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yao yuan
bash: yuan: 
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yaoyuan*
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$name=yaoyuan
yaoyuan-desktop$echo $name
yaoyuan
yaoyuan-desktop$echo "$name"
yaoyuan*
yaoyuan-desktop$echo "Is Not Hello World! "
Is Not Hello World! 
yaoyuan-desktop$echo '$name'
$name
yaoyuan-desktop$echo '$name'
$name

Shellコマンド置換
$(command)
yaoyuan-desktop$command=pwd
yaoyuan-desktop$echo "The value of command is: $command."
The value of command is: pwd.
yaoyuan-desktop$command=$(pwd)
yaoyuan-desktop$echo "The value of command is: $command."
The value of command is: /home/yaoyuan.

コマンド置換は、任意のコマンドに適用されます.
yaoyuan-desktop$echo "The date and time is $(date)."
The date and time is 2008  10  12    01:41:28 CST.

読み取り専用のユーザー定義変数の作成
declare -r [name-list]
typeset -r [name-list]
readonly [name-list]
name-listにリストされている変数に新しい値を割り当てるのを阻止するには
yaoyuan@yaoyuan-desktop:~/ScriptFile$ declare -r name=Yao place=Yuan
yaoyuan@yaoyuan-desktop:~/ScriptFile$ echo $name $place
Yao Yuan

yaoyuan@yaoyuan-desktop:~/ScriptFile$ name=Yuan
bash: name:  
yaoyuan@yaoyuan-desktop:~/ScriptFile$ place="YaoYuan"
bash: place:  

読み取り専用変数の値はリセットできません

yaoyuan@yaoyuan-desktop:~/ScriptFile$ unset name
bash: unset: name: :  variable
yaoyuan@yaoyuan-desktop:~/ScriptFile$ unset place
bash: unset: place: :  variable

標準入力デバイスから読み込む
read [options][variable-list]
標準入力装置から1行を読み込み、読み込んだ語をvariable-listの変数に割り当てる
yaoyuan@yaoyuan-desktop:~/ScriptFile$ cat read_demo
#! /bin/bash
echo -n "Enter input: "
read line
echo "You entered: $line"
echo -n "Enter another line: "
read word1 word2 word3
echo "The first word is: $word1"
echo "The second word is : $word2"
echo "The rest of the line is: $word3"
exit 0
yaoyuan@yaoyuan-desktop:~/ScriptFile$ ./read_demo
Enter input: yaoyuan
You entered: yaoyuan
Enter another line: hello shell, I love you
The first word is: hello
The second word is : shell,
The rest of the line is: I love you