ã·ã§ã«ã¹ã¯ãªããã§æ¡ä»¶åå²ãæ¸ãã¨ãã[
ã使ã£ãã [[
ã使ã£ãããããã¨ã«ãªãã¨æãã¾ãã
POSIX ã® test ã«ãè¨è¼ããããããã[
㯠test
ã¨åãæ©è½ãæã¤ãã¨ã«ãªã£ã¦ãããåãå®è£
ã§ãããã¨ãå¤ãã§ã (ãã ãã[
ã¨ãã¦å®è¡ãããããtest
ã¨ãã¦å®è¡ããããã«ä¾ã£ã¦æåãå¤ããå¾ã)ã
ä¸æ¹ã§ã[[
㯠POSIX ã§å®ç¾©ã¯ããã¦ããªããBash çã®é«æ©è½ã·ã§ã«ã§ã®æ¡å¼µã«ãªãã¾ãã
[
㨠[[
ã¯ãå½ç¶ãªããåããããªæ©è½ãæã£ã¦ãã¾ããããããå¾è
ã¯åè
ã®æ¡å¼µã§ãããããä¸æ¹ã§ããæ¡å¼µãã§ãããæ
ã«ã[
㨠[[
ã§ç°ãªãæåãåå¨ãã¾ãã
æ¼ç®å
ãã¨ãã°æ¯è¼ã®æ¼ç®ã¨ãã¦ã¯ä»¥ä¸ã®ãããªæåã®ã¡ããããã£ãããã¾ãããªãã[
ã¸ã®æç¡ã«ã¤ãã¦ã¯ãPOSIX å®ç¾©ã¨ãã観ç¹ã§ãã£ã¦ãã·ã§ã«å®è£
ã«ãã£ã¦ã¯æ®éã«ä½¿ãããã®ãããã¾ãã
æ©è½ | [ |
[[ |
åè |
---|---|---|---|
æååã®å¤§å°æ¯è¼ | a \> b |
a > b |
[ ã®å ´åããªãã¤ã¬ã¯ãã¨èª¤è§£ãããªãããã«ã¨ã¹ã±ã¼ããå¿
è¦ã§ã |
AND æ¡ä»¶ | s1 -a s2 |
s1 && s2 |
å®éã®ã¨ããã-a 㯠POSIX 㧠æ代é
ãã§ãããPOSIX æ¨æºã«æºæ ããå ´åã¯ä½¿ããªããããè¯ãã ã¨ããã¦ã¾ã |
OR æ¡ä»¶ | s1 -o s2 |
s1 || s2 |
åä¸ |
ãã¿ã¼ã³ããã | ãªã | $a = a* |
|
æ£è¦è¡¨ç¾ããã | ãªã | $a =~ ^hoge |
|
ãã¡ã¤ã«ã®æ°æ§ãã§ã㯠| ãªã | $file1 -nt $file2 |
ãã®ãããã®å·®ç°ã®èæ¯ã«ã¯ãPOSIX ã«ãè¨è¼ãããã¾ãã
Some additional primaries newly invented or from the KornShell appeared in an early proposal as part of the conditional command ([[]]): s1 > s2, s1 < s2, str = pattern, str != pattern, f1 -nt f2, f1 -ot f2, and f1 -ef f2. They were not carried forward into the test utility when the conditional command was removed from the shell because they have not been included in the test utility built into historical implementations of the sh utility.
ãã®ä»ã®æå
[[
ã§ã¯å¤æ°ãã¯ãªãã¼ã·ã§ã³ã§å²ã¾ãªãã¦è¯ã
$ var='' $ [ $var = '' ] # [ = '' ] ã¨å±éããã¦ãã¾ãã®ã§ NGãã¯ãªãã¼ã·ã§ã³ãå¿ è¦ã sh: [: =: unary operator expected $ [[ $var = '' ]
$ file="file name" $ [ -f $file ] # ã¹ãã¼ã¹ãå«ãã§ããã®ã§ãã¯ãªãã¼ã·ã§ã³ã§å²ã¾ãªãã¨å¤±æãã sh: [: file: binary operator expected $ [[ -f $file ]] # æåãã
[[
ã®å ´åã(
çãã¨ã¹ã±ã¼ãããå¿ è¦ãããã¾ããã
$ [ -f "$file1" -a \( -d "$dir1" -o -d "$dir2" \) ] # ã°ã«ã¼ãã³ã°ç¨ã®æ¬å¼§ãã¨ã¹ã±ã¼ãã®å¿ è¦æ $ [[ -f $file1 && ( -d $dir1 || -d $dir2 ) ]]
â»å
è¿°ã®éãã-a
ã -o
㯠POSIX ã¨ãã¦ã¯ãobsolete ã¨ããã¦ãã¾ãã
[[
ã®å ´åãæååã®å¤§å°æ¯è¼ã§ã¨ã¹ã±ã¼ãããå¿ è¦ããªã
$ [ a < b ] # ã·ã§ã«ã«ãªãã¤ã¬ã¯ãã¨è§£éããã sh: b: No such file or directory $ [[ a < b ]] $ [ a > b ] # ãã¡ãã¯ã¨ã©ã¼ããåºãªãã§ããæ£å¸¸ã«ããã¡ã¤ã«ãä½æããã¦ãã¾ãã $ ls b b
ãã¼ã¿ããªãã£
ç¶æ³ã«ãä¾ãã¾ãããã¼ãèªèº«ã¯ããã¾ã§ãã¼ã¿ããªãã£ãéè¦ãã¦ã·ã§ã«ã¹ã¯ãªãããæ¸ããªãã¨ãããªãã¨ã¯æã£ã¦ãã¾ããã ç°å¢ãããã£ãããã·ã§ã«ã¹ã¯ãªããããå¼ã³ã ãããã³ãã³ãã®æåãã¡ããã§ããããããã®ã¾ã¾å¥ç°å¢ã¸ç§»æ¤ã¨ããã®ã¯ã¾ãç¡ãã ããã¨æã£ã¦ãã¾ãã ããã«ããã¼ã¿ããªãã£ãéè¦ããã«ãã¦ããã·ã§ã«ã®åå¥æ¡å¼µã使ç¨ããã« POSIX ã ãã§æ¸ããã¨ããã®ã¯ãããé£ããã§ããããã
ããããèãæ¹ãªã®ã§ã[[
ã®æ¹ã使ãããªã