102-1. シェル、スクリプト、およびデータ管理
- シェル環境のカスタマイズと使用
- 簡単なスクリプトをカスタマイズまたは作成する
- 「source シェル名」でシェルを実行できる(「source」は「.」でも実行可能)
- testで条件式(「test 条件式」は、「[条件式]」と同じ)
- if,case,for,while,read等の構文がある
- SQLデータ管理
1. シェル環境のカスタマイズと使用
環境変数とシェル変数
- シェルは、ユーザー毎に異なる環境を用意できる
- 変数は、環境変数とシェル変数の2種類がある
シェルのオプション
- シェルには様々なオプション機能があり、「set」コマンドオプションをオン(+)・オフ(-)に切り替えが出来る
- 「set」コマンドのみで実行すると変数一覧が表示される
- 「set -o」で実行すると変数に対するオプションのオン・オフ一覧が表示される
set [-o][+o] オプション
オプション | 説明 |
---|---|
allexport | 作成・変更した変数を自動的にエクスポート |
emacs | emacs風キーバインド |
ignoreeof | Ctrl+Dでログアウトしない |
noclobber | 出力リダイレクトを禁止(「set -C」でも実行可) |
noglob | メタキャラを使ったファイル展開を無効 |
vi | vi風キーバインド |
メタキャラを禁止した例) [vagrant@localhost lpic]$ ls *.txt hoge.txt hoge2.txt hoge3.txt [vagrant@localhost lpic]$ set -o noglob [vagrant@localhost lpic]$ ls *.txt ls: cannot access *.txt: そのようなファイルやディレクトリはありません [vagrant@localhost lpic]$
エイリアス
- エイリアスとは、コマンドに別名つけたりコマンドとオプションをひとまとめにして新しいコマンドのようしたりできる
エイリアスの使用例) [vagrant@localhost lpic]$ alias ls='ls -l' [vagrant@localhost lpic]$ ls 合計 4 -rw-rw-r-- 1 vagrant vagrant 9 7月 22 10:20 2015 hoge.txt -rw-rw-r-- 1 vagrant vagrant 0 8月 5 03:49 2015 hoge2.txt -rw-rw-r-- 1 vagrant vagrant 0 8月 5 03:49 2015 hoge3.txt [vagrant@localhost lpic]$ \ls hoge.txt hoge2.txt hoge3.txt [vagrant@localhost lpic]$ unalias ls [vagrant@localhost lpic]$ ls hoge.txt hoge2.txt hoge3.txt [vagrant@localhost lpic]$
関数の定義
- 「function」コマンドを使うと、bashシェル上で利用できる独自の関数を定義できる
- 「{」の後と「}」の前には空白が必要
- 引数を利用する場合、「$1」で利用できる
- 「declare -f」コマンドで関数のみを表示できる
- 「unset」コマンドで関数を削除出来る
function 関数名() { コマンド; }
関数の利用例) [vagrant@localhost lpic]$ declare -f [vagrant@localhost lpic]$ function lslink() { ls -l $1 | grep '^l'; } [vagrant@localhost lpic]$ declare -f lslink () { ls -l -l $1 | grep '^l' } [vagrant@localhost lpic]$ ls -l lpic2 lrwxrwxrwx 1 vagrant vagrant 19 8月 5 04:25 2015 lpic2 -> /home/vagrant/lpic2 [vagrant@localhost lpic]$ lslink ~/lpic/ lrwxrwxrwx 1 vagrant vagrant 19 8月 5 04:25 2015 lpic2 -> /home/vagrant/lpic2 [vagrant@localhost lpic]$ unset lslink [vagrant@localhost lpic]$ declare -f [vagrant@localhost lpic]$
bashの設定ファイル
ファイル | 説明 |
---|---|
/etc/profile | ログイン時に実行(全ユーザーの環境変数やシェルスクリプトを設定) |
~/.bash_profile | ログイン時に実行(自分用の環境変数やシェルスクリプトを設定) |
~/.bash_login | ログイン時に実行(bash_profileがない場合) |
~/.profile | ログイン時に実行(bash_profile、bash_loginがない場合) |
~/.bashrc | bash起動時に実行(自分用のaliasの設定等) |
~/.bash_logout | ログアウト時に実行 |
2. 簡単なスクリプトをカスタマイズまたは作成する
シェルスクリプトの基礎
- 「source」コマンドでシェルを実行できる(「source」は「.」でも実行可能)
- ファイルに実行権限をつけると「source」コマンドを使わくてもコマンドのみで実行できる
- bash用のシェルファイルは文頭に「#!/bin/bash」と宣言する
source シェル名
シェル(lsld)の事項例 [vagrant@localhost lpic]$ ls -l 合計 12 -rw-rw-r-- 1 vagrant vagrant 9 7月 22 10:20 2015 hoge.txt drwxrwxr-x 2 vagrant vagrant 4096 8月 5 05:14 2015 hogeDir lrwxrwxrwx 1 vagrant vagrant 19 8月 5 04:25 2015 lpic2 -> /home/vagrant/lpic2 -rw-rw-r-- 1 vagrant vagrant 89 8月 5 05:12 2015 lsld [vagrant@localhost lpic]$ cat lsld ls -l $1 > lslink echo "Link Files" grep '^l' lslink echo "Directories" grep '^d' lslink [vagrant@localhost lpic]$ . lsld . // sourceコマンドを処略して実行 Link Files lrwxrwxrwx 1 vagrant vagrant 19 8月 5 04:25 2015 lpic2 -> /home/vagrant/lpic2 Directories drwxrwxr-x 2 vagrant vagrant 4096 8月 5 05:14 2015 hogeDir [vagrant@localhost lpic]$ chmod a+x lsld [vagrant@localhost lpic]$ ./lsld // コマンドのみで実行 Link Files lrwxrwxrwx 1 vagrant vagrant 19 8月 5 04:25 2015 lpic2 -> /home/vagrant/lpic2 Directories drwxrwxr-x 2 vagrant vagrant 4096 8月 5 05:14 2015 hogeDir [vagrant@localhost lpic]$ [vagrant@localhost lpic]$
引数
変数名 | 説明 |
---|---|
$0 | シェルスクリプト自身の名前※ |
$1...9 | 1〜9番目の引数 |
$# | 引数の数 |
$@ | すべての引数.(スペース区切り) |
$* | すべての引数(環境変数IFSで指定した区切り) |
※.コマンドとして実行した場合はシェルスクリプト名、 シェルで実行した場合シェル名(bashなど)
使用例) [vagrant@localhost lpic]$ echo 'echo $0; echo $1; echo $2; echo $#;' > testargs;chmod a+x testargs; [vagrant@localhost lpic]$./testargs arg1 // コマンドとして実行 ./testargs arg1 1 [vagrant@localhost lpic]$ [vagrant@localhost lpic]$ . testargs arg1 // シェルで実行 -bash 0 [vagrant@localhost lpic]$
test
条件式の書式) test 条件文 または [ 条件文 ]
分類 | 主な条件式 | 実行結果 | 備考 |
---|---|---|---|
ファイル形式 | -f ファイル名 | ファイルがあれば真 | 実行権有りは-x、書き込み権有りは-w |
-d ディレクトリ名 | ディレクトリがあれば真 | ||
ファイル1 -nt ファイル2 | ファイル1の更新日が、ファイル2より新しければ真 | -nt → is newer than | |
ファイル1 -ot ファイル2 | ファイル2の更新日が、ファイル1より新しければ真 | -ot → is older than | |
数値 | 数値1 -eq 数値2 | 数値1 == 数値2 | -eq → equal |
数値1 -ne 数値2 | 数値1 != 数値2 | -ne → not equal | |
数値1 -ge 数値2 | 数値1 >= 数値2 | -ge → greater than or equal | |
数値1 -gt 数値2 | 数値1 > 数値2 | -gt → greater than | |
数値1 -le 数値2 | 数値1 <= 数値2 | -le → less than or equal | |
数値1 -lt 数値2 | 数値1 < 数値2 | -lt → less than | |
文字列 | -n 文字列 | 文字列の長さが0より大きければ真 | |
-z 文字列 | 文字列の長さが0であれば真 | ||
文字列1 = 文字列2 | 等しければ真(==ではない) | ||
文字列1 != 文字列2 | 異なれば真 | ||
論理結合 | !条件 | 真→偽、偽→真 | |
条件1 -a 条件2 | and | ||
条件1 -o 条件2 | or |
if
書式) if 条件式 then 実行文 else 実行文 fi
実行例) [vagrant@localhost lpic]$ cat iftest1.sh if test -f testscript then source ./testscript else echo "testscript file not exist" fi [vagrant@localhost lpic]$ ls ./testscript ls: cannot access ./testscript: そのようなファイルやディレクトリはありません [vagrant@localhost lpic]$ ./iftest1.sh testscript file not exist [vagrant@localhost lpic]$
case
書式) case 式 in 値1) 実行文 値N) 実行文 esac
for
書式) for 変数名 in 変数に代入する値のリスト do 実行文 done
実行例) [vagrant@localhost lpic]$ cat fortest1.sh for var in Vine SUSE Gentoo do echo $var Linux done [vagrant@localhost lpic]$ ./fortest1.sh Vine Linux SUSE Linux Gentoo Linux [vagrant@localhost lpic]$
while
書式) while 条件式 do 実行文 done
read
- 標準入力から入力を受け付ける
実行例) [vagrant@localhost lpic]$ cat readtest.sh echo -n "Who are you? : " read username echo "Hello, $username!" [vagrant@localhost lpic]$ ./readtest.sh Who are you? : Kinopio Hello, Kinopio! [vagrant@localhost lpic]$
3. SQLデータ管理
DML
「SELECT」文でレコードを参照
SELECT カラム名 FROM テーブル名
「INSERT」文でレコードを追加
INSERT INTO テーブル名(カラム名,カラム名...) VALUES (値,値...)
「UPDATE」文でレコードを更新
UPDATE テーブル名 SET カラム名=値, カラム名=値...
- 「WHERE 条件」で更新するレコードを絞れる
「DELETE」文でレコードを削除
DELETE FROM テーブル名
- 「WHERE 条件」で削除するレコードを絞れる
おまけ
既存シェル変数を実行時に利用したくない場合
- 実行時だけ既に$VARが指定してあって環境変数を指定したくない時は「変数名=初期値 . 実行シェル」
$ VAR=HOGE! $ echo $VAR HOGE! $ cat hoge.sh #!/bash/bin VAR=hogehoge echo $VAR $ VAR='' . hoge.sh hogehoge $ echo $VAR HOGE!