ファイル読み込み

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/ksh

#入力ファイル名
INPUTFILE="./input.txt"

#入力ファイルの区切り文字
IFS=",";

#ファイル読み込み
while read line ; do
if [[ "$line" != "" ]]
then
set $line
echo "[$1][$2]"
fi
done < ${INPUTFILE}

case文

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
OSNAME=$(uname)
case $OSNAME in
    Linux)
      dmesg > $SYSLOG_OUTFILE
      ;;    # not supported iostat COMMAND
    SunOS)
      dmesg > $SYSLOG_OUTFILE
      IOOPT=-x
      ;;
    HP-UX)
      alias pscmd="ps -ef"
      alias dfcmd="bdf"
      IOOPT=-t
      ;;
    AIX)
      alias pscmd="ps -e -o user,pid,ppid"
      IOOPT=-d
      ;;
    *)
      echo "Not support $OSNAME.";exit 3
      ;;
esac

for文

1
2
3
4
5
RESULT=/tmp/res.out
for ARG in "$@"
do
   echo $ARG
done > RESULT 2>&1

while文

1
2
3
4
5
6
7
let n=0
while read line
do
   let n=$n+1
   echo "$n: $line"
done < $0
echo "Total lines are $n ."

while無限

1
2
3
while true; do
   sleep 60
done

OSコマンド実行

1
2
3
4
5
6
7
set -A DIR_NMS $(ls)     : DIR_NMS 配列に `ls` の結果 
MAX_DIR=${#DIR_NMS[*]}    : MAX_DIR に ls の個数
ただしファイル数が多い場合、配列がオーバーフローしエラー
配列の最大サイズは 1024 です。

alias datecmd='date +"%Y %m %d"'
TODAY=$(datecmd)

演算子

演算子 意味 例:let no=6 結果 
exp1 -eq exp2 exp1 と exp2 が等しいなら真 if [[ $no -eq 6 ]];then echo YES;fi; YES
exp1 -ne exp2 exp1 と exp2 が等しくないなら真 if [[ $no -ne 6 ]];then echo NO;fi;
exp1 -lt exp2 exp1 < exp2 なら真 if [[ $no -lt 6 ]];then echo NO;fi;
exp1 -gt exp2 exp1 > exp2 なら真 if [[ $no -gt 6 ]];then echo NO;fi;
exp1 -le exp2 exp1 <= exp2 なら真 if [[ $no -le 6 ]];then echo YES;fi; YES
exp1 -ge exp2 exp1 >= exp2 なら真 if [[ $no -ge 6 ]];then echo YES;fi; YES

文字列検査演算子

演算子 意      味 例:str=“oracle”;str1=“AZ”;str2=“BZ” 結果
-n string string の長さがゼロでなければ真   if [[ -n $str ]];then echo YES;fi; YES
-o option option が設定されていれば真 if [[ -o $str ]];then echo NO;fi;  
-z string string の長さがゼロならば真   if [[ -z $str ]];then echo NO;fi; YES
string = pattern string と pattern が一致すれば真 if [[ $str = “oracle” ]];then echo YES;fi; YES
string != pattern string と pattern が一致しなければ真 if [[ $str != “oracle” ]];then echo NO;fi;  
string1 < string2 string1 < string2 なら真 if [[ $str < “ORACLE” ]];then echo NO;fi;  
string1 > string2 string1 > string2 なら真 if [[ $str > “ORACLE” ]];then echo YES;fi; YES
(expression) expression が真なら真 if [[ ($str = [a-z]*) ]];then echo YES;fi; YES
! expression expression が偽なら真 if [[ ! ($str = [A-Z]*) ]];then echo YES;fi; YES
exp1 && exp2 exp1 と exp2 が真なら真 if [[ ($str1 = [A-Z]*) && ($str2 = [A-Z]*) ]];then echo YES;fi; YES
exp1 || exp2 exp1 か exp2 が真なら真 if [[ ($str1 = “AZ”) || ($str2 = “AZ”) ]];then echo YES;fi; YES

ファイル検査演算子

演算子 真となる条件 結果
-a file ファイルは存在する   if [[ -a $HOME/.cshrc ]];then echo YES;fi YES
-b file ファイルは存在し、ブロック型である if [[ -b /dev/null ]];then echo YES;fi  
-c file ファイルは存在し、文字型である if [[ -c /dev/stdin ]];then echo YES;fi YES
-d file ファイルはディレクトリ if [[ -d $HOME ]];then echo YES;fi YES
-f file ファイルは普通のファイル if [[ -f $HOME/.cshrc ]];then echo YES;fi YES
-g file ファイルは存在し、setgid が設定されている if [[ -g $HOME/.cshrc ]];then echo YES;fi  
-k file ファイルは存在し、sticky が設定されている if [[ -k /opt/SWAP ]];then echo YES;fi  
-p file ファイルは存在し、fifo である if [[ -p /tmp/.X11-pipe/X0 ]];then echo YES;fi  
-r file ファイルをユーザが読める if [[ -r $HOME/.cshrc ]];then echo YES;fi YES
-s file ファイルは存在し、0 より大きい if [[ -s $HOME/.cshrc ]];then echo YES;fi YES
-t N ファイルディスクリプタ N が端末である if [[ -t 1 ]];then echo YES;fi YES
-u file ファイルは存在し、setuid が設定されている if [[ -u $HOME/.cshrc ]];then echo YES;fi YES
-w file ファイルは存在し、ユーザが書ける if [[ -w $HOME/.cshrc ]];then echo YES;fi YES
-x file ファイルは存在し、ユーザが実行できる if [[ -x /bin/ksh ]];then echo YES;fi YES
-G file ファイルは存在し、グループ id はシェル と同じ if [[ -G $HOME/.cshrc ]];then echo YES;fi YES
-L file ファイルは存在し、シンボリックリンクである if [[ -L /usr/lib/libcrypt.so ]];then echo YES;fi YES
-O file ユーザはファイルの所有者 if [[ -O $HOME/.cshrc ]];then echo YES;fi YES
-S file ファイルは存在し、ソケットである if [[ -S /var/tmp/.oracle/sora817 ]];then echo YES;fi YES
F1 -nt F2 ファイルは存在し、F2 より新しい if [[ .cshrc -nt $HOME/.cshrc ]];then echo YES;fi YES
F1 -ot F2 ファイルは存在し、F2 より古い if [[test.c -ot test.o ]];then echo YES;fi YES
F1 -ef F2 ファイルは存在し、ファイルが同じファイルを参照 if [[ /usr/lib/libcrypt.so -ef /lib/libcrypt.so.1 ]];then echo YES;fi YES

メタ文字

メタ文字 意      味
~ ホームディレクトリ cd ~taka
$ 変数識別子    $HOME
$! 直前に呼び出した子プロセスの番号  kill $!
$- シェルに与えられたフラグ  
<,>,&,! 入出力の切換え    echo $HOME > /tmp/out
!,^ 履歴    !vi
$$ 起動中のシェルのプロセス番号 set out=/tmp/$$.out
; コマンド区切り    cd;pwd
() コマンドグループ    (cd; make)
コマンド コマンド置き換え    set file = ls
& バックグラウンドジョブ    tst &
,’," 文字をエスケープする    echo ‘$HOME’
\ 次文字をエスケープする     
echo \$HOME 継続行であることを示す。前に空白が必要 XXXXXXX \
*,?,[,],,{,},|,~,- ファイル名展開 ls t?.*
文字として扱いたい場合はエスケープ ()
# コメント     
| パイプ    ls | more