Transcript vol4-5

情報工学科 3年生対象 専門科目
システムプログラミング
第4, 5回
シェルプログラミング
担当:青木義満
[email protected]
1
シェルとは?
• ユーザーとUNIXシステムの間のインタフェー
スとして機能するプログラム
• カーネルで行われる処理の細かな部分を
ユーザーから隠す役割
csh
その他の
プログラム
表2.1
カーネル
bash
X Window
2
プログラミング言語としてのシェル
• シェルプログラム作成の2つの方法
– コマンドを入力してシェルによって対話的に実行
– コマンドをファイルに保存し,プログラムとして呼び出す
• 対話的プログラムの例
– grep –l POSIX * | more
– POSIXというパターンに一致する内容を持つファイルの
名前を出力
– いつもコマンドを打つのは面倒
– コマンドを保存したファイルを作成し,必要に応じて実行
• “シェルスクリプト“
3
シェルプログラミングの利点
• 豊富なUNIXコマンド群を有効活用
• 例えば….
– ディレクトリ内にある画像ファイル全てに対して,
プログラムによりある処理を施したい
• ディレクトリ内にある画像ファイルのみを抽出,繰り返
し演算
– 複数あるデータベースファイルから,ある特定の
文字列を含むファイルのみを抽出してプログラム
により処理したい
• 文字列検索による該当ファイル抽出,繰り返し演算
4
シェルスクリプトの作成
• コマンドを記述したファイル”first.sh”を作成
#!bin/sh
#first.sh
#This file looks through all the files in the current
#directory for the string “yamada”, and then prints those
#files to the standard output.
for file in *
do
if grep -q yamada $file
then
more $file
fi
done
exit 0
• シェルスクリプトの実行
– /bin/sh first.sh
5
シェルの構文
• シェルの持つ強力なプログラミング機能の学習
• 小さなプログラムの断片を対話的にテスト,大きなスクリプト
に組み込める
• 構造化されたプログラムの作成
• 学習項目
–
–
–
–
–
–
–
変数:文字列,数値,環境,パラメータ
条件:シェルのブール型
プログラム制御:if, elif, for, while, until, case
リスト
関数
ビルトインコマンド
コマンドの結果の取得
6
シェルの変数
• 変数を宣言せずに使用
– 変数を最初に使用した時点で変数が作成
• デフォルトでは,全ての変数が文字列型
• 大文字と小文字は区別(例:FOOとfooは別)
• 変数名の前に$文字をつけることで変数の内容を
取得
• echoコマンドで変数の内容を出力
• 変数に値を割り当てる場合以外には$をつける必
要あり
7
変数の使用例
bash-2.05$
bash-2.05$
bash-2.05$
bash-2.05$
bash-2.05$
bash-2.05$
salutation=Hello
echo $salutation
salutation=“Yes Dear”
echo $salutation
salutation=7+5
echo $salutation
空白を含む文字列は引用符で囲む必要がある
また,等号の両側に空白をいれてはいけない
8
#!bin/sh
myvar="Hi there"
echo $myvar
echo "$myvar"
echo '$myvar'
echo \$myvar
echo Enter some text
read myvar
echo '$myvar' now equals
$myvar
exit 0
read コマンド
ユーザーが入力した文字列を取得
File name : read.sh
/bin/sh read.sh
9
環境変数
• いくつかの変数は,あらかじめ環境に含まれている値
で初期化(環境設定)
• 大文字の変数
• ユーザー定義変数→小文字で区別
10
パラメータ変数
11
例題)パラメータと環境変数
!/bin/sh
salutation="Hello"
echo $salutation
echo "The program $0 is now running"
echo "The second parameter was $2"
echo "The first parameter was $1"
echo "The parameter list was $*"
echo "The user's home directory is $HOME"
echo "Please enter a new greeting"
read salutation
echo $salutation
echo "The script is now completed"
exit 0
File name : parameter.sh
/bin/sh parameter.sh ・・・・
12
条件
• 条件を調べ,その結果に応じて異なる処理をする機
能
◆test( [ ] )コマンド
– シェルで真偽を判定するための[ ]コマンド(testコマンド)
– testコマンドにより単純な条件を調べる
– ファイルが存在するかどうか?
if test -f fred.c
[ -f fred.c ]
then
・・・・・・・
fi
スペースが必要!
13
簡単なtestコマンドの使用例
ファイル名: test.sh
#!/bin/sh
if test -f fred.c
then
echo "File fred.c exists."
else
echo "File fred.c not found."
fi
exit 0
14
testコマンドで使用でき
る条件
15
制御構造
◆if ステートメント
使用方法
if condition
then
statements
else
statements
fi
16
if コマンドの使用例
• ファイル名:if1.sh
#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if [ $timeofday = "yes" ]; then
echo "Good morning"
else
echo "Good afternoon"
fi
exit 0
17
Ifコマンド2
• ファイル名:if2.sh
!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if [ $timeofday = "yes" ]; then
echo "Good morning"
elif [ $timeofday = "no" ]; then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes or no"
exit 1
fi
exit 0
18
Ifコマンド3
• ファイル名:if3.sh
!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
if [ “$timeofday” = "yes" ]; then
echo "Good morning"
elif [ “$timeofday” = "no" ]; then
echo "Good afternoon"
else
echo "Sorry, $timeofday not recognized. Enter yes or no"
exit 1
fi
exit 0
19
繰り返し制御
• for, while, case文(Cとは少し異なる)
◆forの構文
変数
任意の文字列集合
for variable in values
do
命令文
statements
done
20
for文の使用例1
• ファイル名:for1.sh
#!bin/sh/
for name in aoki tokunaga ohzeki 43
do
echo $name
done
exit 0
21
for文使用例2
(ワイルドカードの展開 重要)
• ファイル名:for2.sh
#!bin/sh/
ワイルドカード
for file in $(ls *.sh); do
echo $file
done
exit 0
22
While文
• for文:一連の文字列を対象として繰り返し処
理をするには便利.指定した回数だけコマン
ドを実行する場合は? また,繰り返す回数
が不明の時は?
• 例:10回の繰り返し
#!/bin/sh
for foo in 1 2 3 4 5 6 7 8 9 10
do
echo "here we go again"
done
exit 0
冗長な表現
23
While文の構文
◆while
while condition
do
statements
done
繰り返しの条件
コマンド
24
while文使用例1
(パスワードの入力を求めるスクリプト)
• ファイル名:while1.sh
#!bin/sh
echo "Enter your password"
read trythis
while [ "$trythis" != "secret" ]
do
echo "Sorry, try again"
read trythis
done
echo "Login succeeded."
exit 0
25
while文の使用例2
(繰り返しの実行)
• ファイル名:while2.sh
#!/bin/sh
i=1
while [ "$i" -le 10 ]
do
echo "Loop number : $i"
i=$(($i+1))
done
exit 0
26
case文
◆case
case variable in
pattern | pattern | …. ) statements1;;
pattern | pattern | …. ) statements2;;
…..
esac
27
case使用例1
(ユーザーからの入力)
• ファイル名:case1.sh
#!/bin/sh
echo "Is it morning? please answer yes or no"
read timeofday
case "$timeofday" in
"yes") echo "Good morning";;
"no") echo "Good afternoon";;
"y") echo "Good morning";;
"n")
echo "Good afternoon";;
*)
echo "Sorry, answer not recognized.";;
esac
exit 0;
28
case使用例2
(パターンをまとめる)
• ファイル名:case2.sh
#!/bin/sh
echo "Is it morning? please answer yes or no"
read timeofday
case "$timeofday" in
"yes" | "y" | "Yes" | "YES") echo "Good morning";;
"no" | "n" | "No" | "NO") echo "Good afternoon";;
*)
echo "Sorry, answer not recognized.";;
esac
exit 0;
29
case使用例3
(複数ステートメントの実行)
• ファイル名:case3.sh
#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday
case "$timeofday" in
"yes"|"y"|"Yes"|"YES")
echo "Good Morning"
echo "Up bright and early this morning"
;;
[nN]* )
echo "Good Afternoon"
;;
*)
echo "Sorry, answer not recognized"
echo "please answer yes or no"
exit 1
;;
esac
exit 0
30
シェルスクリプト演習問題
• ディレクトリ内のファイルの中から,拡張子が
*.txtのファイルのみを表示し,その個数を表
示するシェルスクリプトを作成せよ
31
リスト(And と Or)
• ANDリスト
statement1 && statement2 && statement3 …
左から順に実行(falseとなるまで)
• Orリスト
statement1 || statement2 || statement3 …
左から順に実行(trueとなるまで)
32
リストの例
And
Or
#!/bin/sh
#!/bin/sh
touch file_one
rm -f file_two
rm -f file_one
if [ -f file_one ] && echo "hello" && [ -f
file_two ] && echo "there"
then
echo "in if"
else
echo "in else"
fi
if [ -f file_one ] || echo "hello" || echo
"there"
then
echo "in if"
else
echo "in else"
fi
exit 0
exit 0
33
関数
• 関数の定義
function_name( ){
statements
}
#!bin/sh
簡単な関数の
使用例
(func1.sh)
foo(){
echo "Function foo is executing"
}
echo "script starting"
foo
echo "script ended"
exit 0
34
ローカル変数の使用
#!/bin/sh
sample_text="global variable"
foo(){
local sample_text="local variable"
echo "function foo is executing"
echo $sample_text
}
echo "script starting"
echo $sample_text
foo
echo "script ended"
echo $sample_text
35
関数の使用例
#!/bin/sh
yes_or_no(){
echo "Parameters are $*"
while true
do
echo "Enter yes or no"
read x
case "$x" in
y | yes ) return 0;;
n | no ) return 1;;
* )
echo "Answer yes or no"
esac
done
}
echo "Original parameters are $*"
if yes_or_no "Is your name $1"
then
echo "Hi $1"
else
echo "Never mind“
fi
exit 0
36
配列について
• 以下の説明を参照
• http://www.not-enough.org/abe/manual/unix2-ad99/array.html
37
シェルプログラミング 演習課題
1.ファイル名をシェルの引数[filename]で与え,
% /bin/sh judge.sh [filename]
・そのファイルが存在すれば,ファイルの中身を表示する
・存在しない場合には,
"WARNING! No such file [filename]"
と表示し,
"Enter Filename: "
として再度ファイル名の入力を促し,判定を繰り返すシェルプログラムを作成せよ。
2.1のプログラムに,引数の個数チェックを行う処理を追加せよ。
例)
% /bin/sh judge.sh → 引数は0
この場合の実行結果
Wrong number of arguments.
Usage:/bin/sh judge.sh filename
となるようにせよ。
38
シェルプログラミング 演習課題
3.テキストファイルからのキーワード抽出プログラム
テキストファイルから,指定した文字列を含む行を探し出して表示するシェル
プログラムを作成せよ。
テキストファイルは各自適当なものを作成
実行例)
Enter filename: datafile.txt ←キーボード入力
Enter keyword: Aoki
←キーボード入力
Yoshimitsu Aoki
←結果の出力
4.特定ファイルバックアッププログラム
ディレクトリ内のファイルの中から,Cソースファイル(*.c)のみファイルをコ
ピーしてバックアップファイル(*.c.bak)を作成するシェルプログラムを作
成せよ。
39
シェルプログラミング 演習課題
5.ディレクトリ内のファイル名全てを表示し,
それぞれのファイルについて,
*.c であれば ”C source file”
*.sh であれば ”Shell script file”
*.txt であれば”Text file”
上記以外であれば,”else”
と表示するプログラムを作成せよ。
(それぞれの種類のファイルが何個ずつ存在す
るかも表示せよ。)
40
システムプログラミング
シェルプログラミング演習
課題提出方法
・【課題1~5 】のシェルスクリプト(kadai1~
5.sh)を以下の要領にて提出すること
・提出先:[email protected]
・提出期限:11/6(火)授業開始前まで
・メールの題名を
system3 学籍番号 苗字 とすること!
例) system3 L01001 aoki
41