bashã«ããã·ã§ã«ã¹ã¯ãªããã®å°æ(2)
åå(bashによるシェルスクリプトの小技(1))ã«å¼ãç¶ããã·ã§ã«ã«ãã£ã¦èªåçã«å¤ãè¨å®ãããç¹æ®ãªå¤æ°ã«ã¤ãã¦ç´¹ä»ãããç¹æ®ãªå¤æ°ãåç §ãããã¨ã«ãããæ§ã ãªæ å ±ãåå¾ãããã¨ãã§ãã(ãã ãããããã®å¤æ°ã«ã¯èªåã§ä»»æã®å¤ãè¨å®ãããã¨ã¯ã§ããªã)ã
ãã¦ãã¾ãã¯ç¹æ®å¤æ°ãä¸è¦§ã§ã¾ã¨ãã¦ã¿ããã馴æã¿ã®ãã®ãå¤ãããæå¾ã®2ã¤(ç¹ã«æå¾ã®PIPESTATUS)ã«ã¤ãã¦ã¯ãã£ã¨ä»ã¾ã§ç¥ããªãã£ã人ãããããããªãã ããããã·ã§ã«ã®ä¸ã§ãã¤ãããã¨éä¸ã®ã³ãã³ãã®ãªã¿ã¼ã³ã³ã¼ããæ¾ããªãã¨ãæã£ã¦ãã¾ãããï¼ä»åã®ãã¤ã³ãã¨ãã¦ã¯ãã1. PIPESTATUSå¤æ°ã«ã¤ãã¦ãã¨ã2. ç¹æ®å¤æ° $@ã¨$*ã®éãã«ã¤ãã¦ãã®2ç¹ã主ã«èª¬æããã
ç¹æ®å¤æ°ä¸è¦§è¡¨
å¤æ°å | èªåçã«è¨å®ãããå¤ |
$? | ç´åã«å®è¡ãããã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ãè¨å®ãããå¤æ°ãæ£å¸¸çµäºã®å ´åã¯ã0ããç°å¸¸çµäºã®å ´åã¯ã0 以å¤ããã»ããããããã·ã§ã«ã¹ã¯ãªããã§ã¯ exit ã³ãã³ãã«ä¸ããå¼æ°ããã®ã·ã§ã«ã¹ã¯ãªããã®çµäºã¹ãã¼ã¿ã¹ã¨ãªãã ä¾ãã°ãexit 2ãã¨æ¸ãããªãã°ããã®ã·ã§ã«ã¹ã¯ãªããã®çµäºã¹ãã¼ã¿ã¹ã¯ã2ããâ» é¢æ°ã® return ãåæ§ã«ãæå®ããå¼æ°ããã®é¢æ°ã®çµäºã¹ãã¼ã¿ã¹ã¨ãªãã |
$! | ããã¯ã°ã©ã¦ã³ãã§å®è¡ãããã³ãã³ãã®ããã»ã¹ID ãè¨å®ãããå¤æ°ãã sleep 100 & ãã®ããã« & ãä»ãã¦å®è¡ããã¨ããã®ã³ãã³ãã¯ããã¯ã°ã©ã¦ã³ãã§å®è¡ãããã ä¸è¨ã®å ´åãã $! ãã«ã¯ sleep ã³ãã³ãã® PID (ããã»ã¹ID)ãã»ããããã¦ããã |
$- | set ã³ãã³ãã§è¨å®ããããã©ã°ããããã¯ã·ã§ã«ã®èµ·åæã«æå®ããããã©ã°ã®ä¸è¦§ãè¨å®ãããå¤æ°ãå¤æ° $- ã®å¤ããabcããªãã°ãã©ã°ã¯ã-abcãã¨ãããã¨ã«ãªãã |
$$ | ã³ãã³ãèªèº«ã® PID (ããã»ã¹ID)ãè¨å®ãããå¤æ°ãã·ã§ã«ã¹ã¯ãªããå ã§ã echo $$ ãã®ããã«è¨è¿°ããã¨ããã®ã·ã§ã«ã¹ã¯ãªãããå®è¡ãããã¨ãã® PID åå¾ã§ãã ( PID 㯠OS ä¸ã§ä¸æã«ãªãããå²ãå½ã¦ããã)ããã®å¤æ°ã使ç¨ãããã¯ããã¯ã§ãã·ã§ã«ã¹ã¯ãªããå ã§ä½æãããã¡ã¤ã«åã®æ¡å¼µåã«å¤æ° $$ ãæå®ããã¨ãããã®ããã ( ex. tempfile.$$ )ãå¤æ° $$ ãæ¡å¼µåã«æå®ãããã¨ã§ãã·ã§ã«ã¹ã¯ãªããå®è¡æ¯ã«ä½æãããã¡ã¤ã«åãå¤ãããã¨ãã§ããã®ã§ã åä¸ã·ã§ã«ã¹ã¯ãªãããåæå®è¡ããå ´åã«ãåæå®è¡ããããåã¹ã¯ãªãããåæã«åä¸ãã¡ã¤ã«ã«åºåãã¦ãã¾ããã¨ãé²æ¢ã§ããã |
$# | å®è¡æã«æå®ãããå¼æ°ã®æ°ãè¨å®ãããå¤æ°ãã $ ./command.sh AAA BBB CCC ãã®ããã«å®è¡ãããå ´åãã·ã§ã«ã¹ã¯ãªãã command.sh å ã§å¤æ° $# ãåç §ããã¨ãã®å¤ã¯ã 3 ãã¨ãªãã |
$@ | ã·ã§ã«ã¹ã¯ãªããå®è¡æãããã㯠set ã³ãã³ãå®è¡æã«æå®ãããå ¨ãã©ã¡ã¼ã¿ãè¨å®ãããå¤æ°ãå¤æ° $* ã¨åºæ¬çã«åãã ãã""ãã§å²ãã ã¨ãã®åä½ãç°ãªãã |
$* | ã·ã§ã«ã¹ã¯ãªããå®è¡æãããã㯠set ã³ãã³ãå®è¡æã«æå®ãããå ¨ãã©ã¡ã¼ã¿ãè¨å®ãããå¤æ°ãå¤æ° $@ ã¨åºæ¬çã«åãã ãã""ãã§å²ãã ã¨ãã®åä½ãç°ãªãã |
$LINENO | ãã®å¤æ°ã使ç¨ãã¦ããè¡ã®è¡çªå·ãè¨å®ãããå¤æ°ããããã°ã«ä¾¿å©ãªè¡çªå·ãè¨å®ããã¦ããå¤æ°ãä¾ãã°ãã·ã§ã«ã¹ã¯ãªããå ã®5è¡ç®ã§ãã®å¤æ°ã使ç¨ããã¨ããã¨ãããã§ã®å¤æ° LINENO ã®å¤ã¯"5"ã¨ãªãã |
${PIPESTATUS[@]} | ãã¤ãã§é£çµããåã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ãè¨å®ãããå¤æ°(é å)ããã¤ã(ã|ã)ã«ããé£çµãããåã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ãè¨å®è¨å®ããã¦ããå¤æ°(é å)ã å¤æ° $? ã§ã¯ãã¤ãã®ä¸çªæå¾ã«ããã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ããåç §ãããã¨ãã§ããªããããã®å¤æ°ã使ç¨ãããã¨ã§ãã¤ãã®å é ã ãããã¯éä¸ã«ããã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ãåç §ãããã¨ãã§ãã (æ®å¿µãªãã bash ã®ã¿ã§ ksh ã§ã¯ä½¿ç¨ã§ããªã)ã |
1.PIPESTATUSå¤æ°ã«ã¤ãã¦
- true/false/exitã³ãã³ãã対話å®è¡å¾ããã¤ãã§é£çµãããåã³ãã³ãã®çµäºã¹ãã¼ã¿ã¹ãåå¾ãã¦ã¿ãã
$ true | false | (exit 3) | (exit 8)
$ echo ${PIPESTATUS[@]}
0 1 3 8
- ã·ã§ã«å
ã§ä½¿ã£ã¦ã¿ã
- å è¡ã§å®è¡ãããå¦çã®ãªã¿ã¼ã³ã³ã¼ããæ¾ã£ã¦ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããä¾(以ä¸ã®ä¾ã§ã¯wcã³ãã³ãã«åå¨ããªããªãã·ã§ã³ã-kããã¤ãã¦ã¨ã©ã¼ã«ãã¦ãã)
- ãããªãµã³ãã«ã¹ã¯ãªããã§æ¤è¨¼
$ cat test02.sh
#!/bin/bash
COMMAND1='wc -k'
COMMAND2='wc -l'
$COMMAND1 | $COMMAND2
test ${PIPESTATUS[0]} -ne 0 && echo "[$COMMAND1] error!"
-
- å®è¡çµæã¯ä»¥ä¸ã®éããã¡ããã¨æ¾ãã¦ãããã¨ãããã
$ bash test02.sh
wc: ãªãã·ã§ã³ãéãã¾ã -- k
詳ãã㯠`wc --help' ãå®è¡ãã¦ä¸ãã.
0
[wc -k] error!
2.ç¹æ®å¤æ° $@ã¨$* ã®éãã«ã¤ãã¦
- å¤æ° $@
- ã""ãã§å²ãã ã¨ãã¯ãæ ¼ç´ããã¦ããåã ã®å¤ãããããã""ãã§å²ãã ç¶æ ã§å±éããããä¾ãã°ãæ ¼ç´ããã¦ããå¤ãããaaaããbbbããcccãã(â» $ set aaa bbb ccc)ãªãã°ãã"$@"ãã¯ãã"aaa" "bbb" "ccc"ãã®ããã«å±éãããã
- å¤æ° $*
- å¤æ° $@ ã¨ã¯ç°ãªããã""ãã§å²ãã ã¨ãã¯ãæ ¼ç´ããã¦ããå ¨ã¦ã®å¤ãä¸ã¤ã®ã""ãã§å²ãã ç¶æ ã§å±éããããä¾ãã°ãæ ¼ç´ããã¦ããå¤ãããaaaããbbbããcccãã(â» $ set aaa bbb ccc)ãªãã°ãã"$*"ãã¯ãã"aaa bbb ccc"ãã®ããã«å±éãããã
- å®éã«çµæãæ¯ã¹ã¦ã¿ã
- é åã使ã£ã¦VARå¤æ°ã«3ã¤ã®å¤ããããtest03é¢æ°ã§èªèããå¼æ°ãæ¾ã£ã¦ã¿ãã
- ãããªãµã³ãã«ã¹ã¯ãªããã§æ¤è¨¼
$ cat test03.sh
#!/bin/bash
VAR=(aaa bbb ccc)
test03(){
n=1; while test $n -le $#
do
v=`eval echo '$'$n`
echo "ARG${n}: $v"
n=`expr $n + 1`
done
}
echo '===== [$@] ====='; test03 "${VAR[@]}"
echo '===== [$*] ====='; test03 "${VAR[*]}"
-
- å®è¡çµæã¯ä»¥ä¸ã®éããã$*ãã使ã£ãå ´åã«ã¯ãã"aaa bbb ccc"ãã¨ãã1ã¤ã®å¼æ°ã¨ãã¦è§£éããã¦ãããã¨ããããã
$ bash test03.sh
===== [$@] =====
ARG1: aaa
ARG2: bbb
ARG3: ccc
===== [$*] =====
ARG1: aaa bbb ccc