- 2012-01-14 (土)
書き方
とりあえず
#!/bin/bash
あとはコマンドを連ねる。
いつも良くやる作業があればスクリプトにしてしまおう。
実行のさせ仕方
vim hoge.sh
などでスクリプト作成したとしまして
hoge.sh というスクリプトが現在ディレクトリにあるとします
sh ./hoge.sh で実行可能
chmod u+x hoge.sh して実行属性をつけておくと
./hoge.sh で実行可能
hoge.sh へ PATH が通っていれば
hoge.sh で実行可能
引数
実行時にファイルに引数を与えると
$1 $2 で拾える
if
PHPで
if ( cond ) {
command ;
}elseif( cond2 ){
command2 ;
else {
command3 ;
}
ならbashで
if cond ; then
command
elif cond2 ; then
command2
else
command3
fi
switch
PHPで
swtich ($cond){
case ‘a’ :
command_a ;
case ‘b’ :
command_b ;
case ‘c’ :
command_c ;
}
ならbashで
case $cond in
“a”)
command_a
“b”)
command_b
“c”)
command_c
esac
for do done
for word in a b c d ; do
echo $word
done
このデリミタがいまいち把握しきれない。
1)
上記はスペースごと
2)
*.jpg
存在するファイルごと
3)
$*
で引数ごと
PHPのfunc_get_args()みたい
今知っているのはとりあえず上記3パターン。
while do done
while [$i -lt 10 ]; do
echo $i
i=’expr $i + 1′
done
上記は単純だが
path=$PATH:
while [ $path ]
do
echo ${path%%:*}
path=${path#*:}
done
で実行例が
$ echo $PATH
/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:
/usr/X11R6/bin:/home/tom-a/bin:/usr/local/pgsql/bin:/usr/interbase/bin
$ ./split.sh
/usr/local/bin
/bin
/sbin
/usr/bin
/usr/sbin
/usr/local/sbin
/usr/X11R6/bin
/home/tom-a/bin
/usr/local/pgsql/bin
/usr/interbase/bin
だという。
これは変数のパターンマッチで実現。
http://www.atmarkit.co.jp/flinux/rensai/theory08/theory08a.html
参考サイト
http://www.atmarkit.co.jp/flinux/rensai/theory08/theory08a.html
http://www.atmarkit.co.jp/flinux/rensai/theory08/theory08b.html
ここ2つを熟読したら
http://www.atmarkit.co.jp/flinux/index/indexfiles/shellsindex.html
ここを通読。
上記で一人立ちできると思います。
- タグ: bash
Comments:0
Trackback+Pingback:0
- TrackBack URL for this entry
- http://reoto.com/archives/355/trackback/
- Listed below are links to weblogs that reference
- シェルスクリプト bash 基礎 覚え方 from ぼんずーず