• 検索結果がありません。

syspro-0405.ppt

N/A
N/A
Protected

Academic year: 2021

シェア "syspro-0405.ppt"

Copied!
40
0
0

読み込み中.... (全文を見る)

全文

(1)

システムプログラミング

担当:青木義満

情報工学科 

3年生対象 専門科目

4, 5回

(2)

シェルとは?

•  ユーザーとUNIXシステムの間のインタフェー

スとして機能するプログラム

•  カーネルで行われる処理の細かな部分を

ユーザーから隠す役割

カーネル csh その他の プログラム 表2.1

(3)

プログラミング言語としてのシェル

•  シェルプログラム作成の2つの方法

–  コマンドを入力してシェルによって対話的に実行

–  コマンドをファイルに保存し,プログラムとして呼び出す

•  対話的プログラムの例

–  grep –l POSIX * | more

–  POSIXというパターンに一致する内容を持つファイルの名

前を出力

–  いつもコマンドを打つのは面倒

(4)

シェルプログラミングの利点

•  豊富なUNIXコマンド群を有効活用

•  例えば….

– ディレクトリ内にある画像ファイル全てに対して,

プログラムによりある処理を施したい

•  ディレクトリ内にある画像ファイルのみを抽出,繰り返し

演算

– 複数あるデータベースファイルから,ある特定の

文字列を含むファイルのみを抽出してプログラム

により処理したい

(5)

シェルスクリプトの作成

•  コマンドを記述したファイル”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

(6)

シェルの構文

•  シェルの持つ強力なプログラミング機能の学習

•  小さなプログラムの断片を対話的にテスト,大きなスクリプトに

組み込める

•  構造化されたプログラムの作成

•  学習項目

–  変数:文字列,数値,環境,パラメータ

–  条件:シェルのブール型

–  プログラム制御:if, elif, for, while, until, case

–  リスト

(7)

シェルの変数

•  変数を宣言せずに使用

–  変数を最初に使用した時点で変数が作成

•  デフォルトでは,全ての変数が文字列型

•  大文字と小文字は区別(例:FOOとfooは別)

•  変数名の前に$文字をつけることで変数の内容を取

•  echoコマンドで変数の内容を出力

•  変数に値を割り当てる場合以外には$をつける必要

(8)

変数の使用例

bash-2.05$ salutation

=

Hello

bash-2.05$ echo

$

salutation

bash-2.05$ salutation=“Yes Dear”

bash-2.05$ echo

$

salutation

bash-2.05$ salutation=7+5

bash-2.05$ echo

$

salutation

空白を含む文字列は引用符で囲む必要がある

また,等号の両側に空白をいれてはいけない

(9)

#!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 コマンド

(10)

環境変数

•  いくつかの変数は,あらかじめ環境に含まれている値

で初期化(環境設定)

•  大文字の変数

(11)
(12)

例題)パラメータと環境変数

!/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

(13)

条件

•  条件を調べ,その結果に応じて異なる処理をする機

test( [ ] )コマンド

–  シェルで真偽を判定するための[ ]コマンド(testコマンド)

–  testコマンドにより単純な条件を調べる

–  ファイルが存在するかどうか? 

if test -f fred.c

then

・・・・・・・

fi

[ -f fred.c ]

スペースが必要!

(14)

簡単な

testコマンドの使用例

#!/bin/sh

if test -f fred.c

then

echo "File fred.c exists."

else

echo "File fred.c not found."

fi

(15)

testコマンドで使用でき

る条件

(16)

制御構造

if ステートメント

 使用方法

if

condition

then

statements

else

statements

(17)

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

(18)

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

(19)

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

(20)

繰り返し制御

•  for, while, case文(Cとは少し異なる)

forの構文

for

variable

in

values

do

statements

変数 任意の文字列集合

(21)

for文の使用例1

•  ファイル名:for1.sh

#!bin/sh/

for

name

in

aoki tokunaga ohzeki 43

do

echo $name

done

(22)

for文使用例2

(ワイルドカードの展開 重要)

•  ファイル名:for2.sh

#!bin/sh/

for

file

in

$(ls *.c)

; do

echo $file

done

ワイルドカード

(23)

While文

•  for文:一連の文字列を対象として繰り返し処

理をするには便利.指定した回数だけコマン

ドを実行する場合は? また,繰り返す回数

が不明の時は?

•  例:10回の繰り返し

#!/bin/sh

for foo in 1 2 3 4 5 6 7 8 9 10

do

echo "here we go again"

(24)

While文の構文

while

while

condition

do

statements

done

繰り返しの条件 コマンド

(25)

while文使用例1

(パスワードの入力を求めるスクリプト)

•  ファイル名:while1.sh

#!bin/sh

echo "Enter your password" read trythis

while [ "$trythis" != "secret" ] do

echo "Sorry, try again" read trythis

done

(26)

while文の使用例2

(繰り返しの実行)

•  ファイル名:while2.sh

#!/bin/sh

i=1

while [ "$i" -le 10 ]

do

echo "Loop number : $i"

i=$(($i+1))

(27)

case文

case

case

variable

in

pattern | pattern | …. )

statements1

;;

pattern | pattern | …. )

statements2

;;

…..

esac

(28)

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";;

(29)

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;

(30)

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" ;;

*)

(31)

シェルスクリプト演習問題

•  ディレクトリ内のファイルの中から,拡張子が

*.txtのファイルのみを表示し,その個数を表

示するシェルスクリプトを作成せよ

(32)

リスト(

And と Or)

•  ANDリスト

statement1 && statement2 && statement3 …

左から順に実行(

falseとなるまで)

•  Orリスト

statement1 || statement2 || statement3 …

左から順に実行(trueとなるまで)

(33)

リストの例

#!/bin/sh

touch file_one rm -f file_two

if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo "there"

then

echo "in if" else

echo "in else" fi

exit 0

#!/bin/sh

rm -f file_one

if [ -f file_one ] || echo "hello" || echo "there"

then

echo "in if" else

echo "in else" fi

exit 0

(34)

関数

•  関数の定義

function_name( ){

statements

}

#!bin/sh foo(){

echo "Function foo is executing" }

echo "script starting" 簡単な関数の

使用例

(35)

ローカル変数の使用

#!/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

(36)

関数の使用例

#!/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

(37)

配列について

•  以下の説明を参照

(38)

シェルプログラミング 演習課題

1.ファイル名をシェルの引数[filename]で与え, % /bin/sh judge.sh [filename]

・そのファイルが存在すれば,ファイルの中身を表示する ・存在しない場合には,

 "WARNING! No such file [filename]" と表示し, "Enter Filename: " として再度ファイル名の入力を促し,判定を繰り返すシェルプログラムを作成せよ。 2.1のプログラムに,引数の個数チェックを行う処理を追加せよ。 例) % /bin/sh judge.sh → 引数は0 この場合の実行結果

(39)

シェルプログラミング 演習課題

3.テキストファイルからのキーワード抽出プログラム

テキストファイルから,指定した文字列を含む行を探し出して表示するシェル

プログラムを作成せよ。

テキストファイルは各自適当なものを作成

実行例)

Enter filename: datafile.txt ←キーボード入力

Enter keyword: Aoki ←キーボード入力

Yoshimitsu Aoki ←結果の出力

4.特定ファイルバックアッププログラム

ディレクトリ内のファイルの中から,

Cソースファイル(*.c)のみファイルをコ

ピーしてバックアップファイル(

*.c.bak)を作成するシェルプログラムを作

成せよ。

(40)

5.ディレクトリ内のファイル名全てを表示し,

それぞれのファイルについて,

*.c であれば ”C source file”

*.sh であれば ”Shell script file”

*.txt であれば”Text file”

上記以外であれば,

”else”

と表示するプログラムを作成せよ。

参照

関連したドキュメント

特に、その応用として、 Donaldson不変量とSeiberg-Witten不変量が等しいというWittenの予想を代数

[r]

奥付の記載が西暦の場合にも、一貫性を考えて、 []付きで元号を付した。また、奥付等の数

奥付の記載が西暦の場合にも、一貫性を考えて、 []付きで元号を付した。また、奥付等の数

・逆解析は,GA(遺伝的アルゴリズム)を用い,パラメータは,個体数 20,世 代数 100,交叉確率 0.75,突然変異率は

、肩 かた 深 ふかさ を掛け合わせて、ある定数で 割り、積石数を算出する近似計算法が 使われるようになりました。この定数は船

LF/HF の変化である。本研究で はキャンプの日数が経過するほど 快眠度指数が上昇し、1日目と4 日目を比較すると 9.3 点の差があ った。

各テーマ領域ではすべての変数につきできるだけ連続変量に表現してある。そのため