ããã«ã¡ã¯ãã¨ã³ã¸ãã¢ãªã³ã°Gã®ä¸æã§ãã
以åã«ãã®ããã°ã«ã¦ã¨ã ã¹ãªã¼ã§ã®ç¤¾å ç ä¿®ã«ã¤ãã¦ç´¹ä»ãã¾ãããä»åã¯ããã®ä¸ã§ã®bashã¹ã¯ãªããã£ã³ã°è¬åº§ã®è³æãå ¬éãã¾ãã
å¼ç¤¾ã®ä¸ã§ããããããªç¨éã§bashã使ããã¦ãã¾ãããbashã¯ç°¡åã«å©ç¨ã§ãããã®ã®ããã°ã©ãã³ã°è¨èªã¨ãã¦ã¯ãã°ãçã¿ããããè¾ãè¨èªã ã¨æãã¾ãã ããã§ç´¹ä»ãã¦ããã®ã¯ããããã³ã¼ãã£ã³ã°è¦åã¨ãããããããã°é²æ¢ã¨å¯èªæ§åä¸ã®ããã®ã«ã¼ã«ãTipséçã«ã¾ã¨ãããã®ã§ãã bashã«ããã¦ã¾ã ã¾ã 注æããã¨ããã¯ããããã§ãããå¤å°ãªãã¨ããããã«ããã¹ã¯ãªããã®åæ¸ã«ãªãã°ã¨æå¾ ãã¦ãã¾ãã
[追è¨: 2018-08-22]
ã¯ã¦ãã«ã¦ä»¥ä¸ã®ã³ã¡ã³ããããã ãã¾ããã
bashã¹ã¯ãªããã£ã³ã°ç ä¿®ã®è³æãå ¬éãã¾ã - ã¨ã ã¹ãªã¼ããã¯ããã°b.hatena.ne.jpbashã§50è¡ä»¥ä¸ã«ãªã£ããã¾ãã¯éãè¨èªä½¿ãã§ããã¨æãã®ã ãããã
2018/08/21 20:00
ããã¯å ¨ããã®ã¨ããã ã¨æãã¾ããä¸çªå¤§äºãªä¸»å¼µããç ä¿®ã®ä¸ã§å£é ã§ã¯ä¼ãã¦ããã®ã§ããè³æã«æ¸ãå¿ãã¦ããã®ã«æ°ã¥ãã¾ããã
è¤éãªå¦çã¯bashã§ã¯ãªãä»ã®è¨èªï¼pythonãªã©ï¼ã使ã£ãæ¹ãããã§ãã ãã®è³æã¯ãbashã¨ããè¨èªãããããã¨æ³¨æç¹ãå¤ããæ¬æ°ã§å¯¾å¦ããã«ã¯è¾ãè¨èªã§ããã¨ãããã¨ãä¼ãããã®ãã®ã§ãããã¾ãã bashã§ç¡çã«ãã¯ããã¯ãé§ä½¿ãã¦ãããã¨ããã®ã§ã¯ãªãã使ããããè¨èªãé¸ã¶ãã¨ãæ¤è¨ããæ¹ãããã¨æãã¾ãã
- bashã¹ã¯ãªããã£ã³ã°è¬åº§
- ã¯ããã«
- ã¹ã¯ãªããã®å®åãã³ãã¬ã¼ã
- bashã®æ¸ãæ¹ã§ããããåé¡
- bashã¹ã¯ãªããã®ããæãã®æ¸ãæ¹
- [å¿ é ] functionã®è¨è¿°æ¹æ³
- [å¿ é ] functionãªã©ã®ãããã¯å ã¯ã¤ã³ãã³ããã
- [å¿ é ] ã¤ã³ãã³ãã¯ã¹ãã¼ã¹ã®ã¿
- [å¿ é ] ï¼ãã¡ã¤ã«ä¸ã®è¨è¿°ã®é ç½®
- [å¿ é ] mainé¢æ°ãä½æãã¦ã¨ã³ããªã¼ãã¤ã³ãã¨ãã
- [å¿ é ] å¤æ°åã®å½å
- [å¿ é ] å¤æ°åã¯é·ããªã£ã¦ãããããããä»ãã
- [å¿ é ] ã³ãã³ããfunctionãä½æããã¨ãã¯ãã£ããã¨ã³ã¡ã³ããè¨è¿°ãã
- [å¿ é ] ã³ãã³ãå®è¡çµæã®æ¨æºåºåãå¤æ°ã§åå¾ããã¨ãã¯ã$(cmd)ã表è¨ã使ã
- [å¿ é ] localãreadonlyãä»ãã¦å¤æ°ãå®ç¾©ãã
- [å¿ é ] å¤ãå¤æ´ããªãå®æ°ãå®ç¾©ããã¨ãã¯readonlyãä»ãã¦å®ç¾©ãã
- [æ¨å¥¨] ã°ãã¼ãã«å¤æ°ãã§ããããã使ããªã
- [æ¨å¥¨] è¤éãªå¦çãå¿ è¦ã«ãªã£ããbash以å¤ã使ããã¨ãèãã
- [å¿ é ] bashã¹ã¯ãªããã«ã¯set -euCãä»ãã
- [æ¨å¥¨] ã¹ã¯ãªããçµäºæã«å¿ ãå¦çãããããã¨ãã¯trapãå©ç¨ãã
- bashã¹ã¯ãªããã®å®è£ ãã¯ããã¯
- bashã®ãããã°ææ³
- åèè³æ
- ã¨ã³ã¸ãã¢åé
bashã¹ã¯ãªããã£ã³ã°è¬åº§
ã¯ããã«
bashã¹ã¯ãªããã¯èª°ã§ãã¨ããããã¯ä½æã§ããã¨ããåé¢ãè¨è¿°ãéã«ãªããããå¾åãããããã«æãã¾ãã ã§ãããä¸åº¦ä½æããããã®ã¯ãã®å¾ã使ãç¶ããããå¾ã ã«ãªã£ã¦æãã¬ãã°ããããããã¡ã³ããã³ã¹ããã¥ããã¦è¦å´ãããã¨ãããã¨ã«ãªãã¾ãã
ãã®ããï¼bashã¨ããã©ãä»ã®è¨èªã§ã®éçºã¨åæ§ã«èªã¿ãããã»ããããããæ¸ãããã«å¿ããã¦ãã ããã è¨èªé¢ã§ã®å¶ç´ãå°ãªãåããããã«ããã³ã¼ãã«ãªããããï¼ããããã®ãæ¸ãã¦ãã¾ãï¼ã®ã§ãä»ãããä¸å±¤è¯ãã³ã¼ãã£ã³ã°ãæèããå¿ è¦ãããã¾ãã
以ä¸ã§ã¯ãbashã§ãã°ã«ãªããããç¹ããèªã¿ããããªããããªæ¸ãæ¹ã®æéãbashã®ãã¯ããã¯çãªãã¨ãä¸ãã¦ãã¾ãã
ãããã
ã¾ããããããbashã使ãã¹ããªã®ã©ãããèãã¦ãã ããã ãã®è³æãè¦ã¦ãå®ã¯bashãããããã¨è¾ãã»å¤§å¤ãªè¨èªã§ãããã¨ãæãã¦ã»ããã¨æã£ã¦ãã¾ãã ããç¨åº¦ä»¥ä¸ã®è¦æ¨¡ãè¤éãªãã¨ãããã®ã§ããã°ãbashã§ã¯ãªãä»ã®è¨èªã使ã£ã¦ãã ããã ããbashã使ãã®ã§ããã°ããããã示ã注æç¹ãè¨æ³ãå®ãã¤ã¤ä½æãã¦ãã ããã
ã¹ã¯ãªããã®å®åãã³ãã¬ã¼ã
æåã«ãã·ã§ã«ã¹ã¯ãªããã®ããããã®æ¸ãæ¹ã®ãã³ãã¬ã¼ãã以ä¸ã«è¨è¼ãã¾ãã
main.sh
#!/bin/bash # # ã¹ã¯ãªããã®å 容ã®èª¬æ # # 説æã®è©³ç´°æãè¨è¿°ãã¾ãã # # bashã®ã¹ã¤ãã set -euC # å¤é¨ã¹ã¯ãªããã®source #source ./setting.inc # ã°ãã¼ãã«å®æ° readonly DEBUG=false readonly WORKDIR=/tmp/mydir # ã°ãã¼ãã«å¤æ° INPUT_FILE= OUTPUT_FILE= FLAG=0 # # å¼æ°parseå¦ç # function usage() { # ãã©ã¡ã¼ã¿ã®è©³ç´°ãå¿ ãæè¨ããã㨠cat <<EOS >&2 Usage: $0 [-o OUTPUT_FILE] [-a] INPUT_FILE INPUT_FILE å ¥åãã¡ã¤ã« -o OUTPUT_FILE åºåãã¡ã¤ã« -a ãªãã·ã§ã³ããããããã¨ãã¦åä½ãã¾ã EOS exit 1 } # å¼æ°ã®ãã¼ã¹ function parse_args() { while getopts "o:a" OPT; do case $OPT in o) OUTPUT_FILE=$OPTARG ;; a) FLAG=1 ;; ?) usage;; esac done shift $((OPTIND - 1)) INPUT_FILE=${1:-} if [[ "$INPUT_FILE" == "" ]]; then usage fi } # # é¢æ°å®ç¾© # function hoge() { local input_file="$1" cat <<EOS cat "$input_file" EOS } function fuga() { local output_file="$1" cat <<EOS echo "Hello" > "$output_file" EOS } function main() { local input_file="$1" local output_file="$2" local flab="$3" hoge "$input_file" fuga "$output_file" } # ã¨ã³ããªã¼å¦ç if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then parse_args "$@" main "$OUTPUT_FILE" "$INPUT_FILE" "$FLAG" fi
test.sh
#!/bin/bash # # åä½ç¢ºèªç¨ã¹ã¯ãªãã # # åºæ¥ãã°UnitTestã¨ãã¦è¨è¿°ãã # ããã¾ã§ããªãã¦ãããã®ä¸ã§åä½ç¢ºèªã®ããã®è©¦ãæ¸ãããã¦å®è¡ãã # source ./main.sh function test_fuga { fuga log_file.txt cat log_file.txt } function test_args { parse_args -o "output" input echo $INPUT_FILE echo $OUTPUT_FILE } function test_main { main item1 item2 0 } #test_fuga test_args #test_main
bashã®æ¸ãæ¹ã§ããããåé¡
bashã¯ä¾¿å©ãªåé¢ãå¤ã®è§£éãªã©ã§åããã«ããåããå¤ãããã¾ãã ä¾ãã°ã以ä¸ã®è¨äºã«ã¦ãããããªè½ã¨ãç©´ãç´¹ä»ããã¦ãã¾ãã
- Bashã®ããããééã | Yakst
æ¬è¨äºã¯ä¸è¨ã®è¨äºãåèã«ããã¦ããã ãã¤ã¤ã社å ã§ç¹ã«ãããã¡ãªã¨ãããè£è¶³ãããã®ã§ãã
[å¿
é ] æ確ãªæå³ãç¡ããªãã·ãã³(#!ãã) ã«ã¯ /bin/sh
ã§ã¯ãªã/bin/bash
ãæ示ãã
ã¹ã¯ãªããã®ï¼è¡ç®ã®ã·ãã³(shbang)ã«ã¯ãsh
ã使ããã¨ãæ確ã«ãã¦ããªããªãbash
ãæ示ãã¦ãã ããã
sh
ã¨bash
ã¯æ£ç¢ºã«ã¯å¥ç©ã§ããsh
ã¯POSIXä»æ§ã«ãã£ãæ©è½ã®ã¿ã使ãããã®ã§ãããbash
ã¯ãããããæ©è½ãè±å¯ã§ãã
OSã«ãã£ã¦ã¯/bin/sh
ãå¥ã®ã·ã§ã«ã®ã·ã³ããªãã¯ãªã³ã¯ã§ããå ´åãããã¾ã(dash
ã ã£ãã)ã
ã¾ãã/bin/bash
ã®ã·ã³ããªãã¯ãªã³ã¯ã®å ´åããã£ãããã¾ãããå®ä½ã¯bashã§ãsh
å®è¡ã¢ã¼ãã§åãã¦ãã¦å®è³ªPOSIXãã使ããªãã¨ããããã¾ãã
åãã¹ã¯ãªãããããã¤ãã®Unix,Linuxç³»OSãã¾ããã§æµç¨ããã®ã§ããã°sh
ãæèãã¦å®è£
ããå¿
è¦ãããã¾ãã
ã§ããã社å
ã®ã¹ã¯ãªããï¼ç¹ã«åæç¨ã»èª¿æ»ç¨ã®ã¹ã¯ãªããï¼ã¯ãç¹å®ã®ç°å¢ã§ã®ã¿åãã°ãããããªãã¨ãã»ã¨ãã©ãªã®ã§bashã使ã£ã¦ããã¦ãã ããã
[å¿ é ] å¤æ°ãå±éããã¨ãã¯å¿ ãã"ãã§å²ã
bashã§ã®å¤æ°ã¯æ®éã®ããã°ã©ãã³ã°è¨èªã¨ã¯ããªãéããããè²ã ã¨æ³¨æããå¿ è¦ãããã¾ãã ãã®ä¸ã§å¤§ããªãã¤ã³ãã¨ãã¦ã¯ä»¥ä¸ã®ï¼ç¹ã§ãã
- 空ç½æåãå¤æ°ã«å ¥ãã¦æ±ãæ
- åè§ã¹ãã¼ã¹ãã¿ããå«ãå¤ãæ±ãæ
ãããã®ç´æçã«ã¯ãããã«ããåé¡ãåé¿ããããã«ããå¤æ°ãå©ç¨ããã¨ãã¯å¿ ãã""ãã§å²ãããã«ãã¦ãã ããã
å¤æ°ã空ç½æåãä¿æãã¦ããã¨ãã«èµ·ããåé¡: ãã®ï¼
VAL1= # 以ä¸ï¼ã¤ã¯åãæå³ some_func $VAL1 foo bar some_func foo bar
ä¸è¨ã®ï¼ã¤ã®ã³ãã³ãå®è¡ã¯åãæå³ã«ãªãã¾ããã¤ã¾ããsome_funcã®ç¬¬ï¼å¼æ°ã¯$VAL1
ã§ã¯ãªãfoo
ã¨ãªãã¾ãã
fooã第ï¼å¼æ°ã¨ãã¦æå®ãããã®ã§ããã°ã$VAL1
ãããã«ã¯ãªã¼ãã§å²ãã§ç¬¬ï¼å¼æ°ã空æåã¨ãªãããã«ããå¿
è¦ãããã¾ãã
VAL1= # 以ä¸ï¼ã¤ã¯åãæå³ some_func "$VAL1" foo bar some_func "" foo bar
å¤æ°ã空ç½æåãä¿æãã¦ããã¨ãã«èµ·ããåé¡: ãã®ï¼
以ä¸ã¯ãifæã®æ§æã¨ã©ã¼ã«ãªã£ã¦ãã¾ãã¾ãã
VAL1= # 以ä¸ï¼ã¤ã¯åãæå³ if [ $VAL1 -eq 1 ]; then echo "OK"; fi if [ -eq 1 ]; then echo "OK"; fi â å®è¡ã¨ã©ã¼çºçã[ ] å ã®æ§æãä¸æ£ã¨ãªãï¼ç¬¬ï¼å¼æ°ã ã-eqãã«ãªããã左辺ããªã)
å¤æ°ãããã«ã¯ãªã¼ãã§å²ãã¨åé¿ã§ãã¾ãã
VAL1= # 以ä¸ï¼ã¤ã¯åãæå³ if [ "$VAL1" -eq 1 ]; then echo "OK"; fi if [ "" -eq 1 ]; then echo "OK"; fi â [ ] å ã®ç¬¬ä¸å¼æ°ã ã""ãã¨ãªãã®ã§æç«ãã
å¤ã«åè§ã¹ãã¼ã¹ãå«ãå ´åã«èµ·ããåé¡
å¤æ°ã®å¤ã«åè§ã¹ãã¼ã¹ãããã¨ãã³ãã³ãå¼æ°ãã«ã¼ããªã©ã®ã¨ãã«ã¹ãã¼ã¹åºåãã§å¤ãå解ããã¦ãã¾ãã¾ãã
åé¡ãã¿ã¼ã³ï¼
# ãã¡ã¤ã«åã«ã¹ãã¼ã¹ãå«ã FILE_NAME="some text file.txt" # 以ä¸ã¯åãæå³ cp $FILE_NAME ./mydir/ cp some text file.txt ./mydir/ â ï¼ãã¡ã¤ã«ãã³ãã¼ãããããªåä½ã«ãªãããããªãã¡ã¤ã«ã¯ãªãã¦ã¨ã©ã¼
åé¡ãã¿ã¼ã³ï¼
# ãã¡ã¤ã«åã«ã¹ãã¼ã¹ãå«ã touch "some text file.txt" # å®è³ªçã«ä»¥ä¸ã¯åã for file in $(ls *.txt); do echo $file; done echo some; echo text; echo file.txt â ls ã®çµæã®æååãsome text file.txtãã空ç½ã§å解ãã¦ã«ã¼ããã¦ãã¾ã # æ£ããã¯ä»¥ä¸ã®ããã«æ¸ã for file in *.txt; do # `*.txt`ã«ããããããã¡ã¤ã«ãç¡ãå ´åã«ãfile='*.txt'ã¨ãã¦ã«ã¼ããï¼åå¦çãããã®ã§ãã§ã㯠if [ ! -e "$file" ]; then continue; fi echo $file done
[å¿
é ] ifæã¯[
ã]
ã§ã¯ãªã[[
ã]]
ã使ã
bashã§ã¯ãæ¡ä»¶å¤å®é¨åã[
ã¨ã¯å¥ã«[[
ã使ãã¾ãã
[[
ã®æ¹ãå®å
¨ã§æ©è½ãè±å¯ãªã®ã§ãã¡ãã使ããã¨ãæ¨å¥¨ãã¾ãã
[[
ã§ã¯åè¿°ã®å¤æ°ã空ã®å ´åã®ã¨ã©ã¼ãçºçãã¾ããï¼ã¡ããã¨æ¡ä»¶å¤å®ã¨ãã¦å®è¡ããã)
VAL1= # VAL1ã "" ã§å²ãå¿ãã¦ãã if [[ $VAL1 == '' ]]; then echo "OK"; fi => ã§ãã¨ã©ã¼ã«ãªããã"OK"ã¨è¡¨ç¤ºããã
詳ããã¯ä»¥ä¸åç §ãã¦ãã ããã
- [Bash] testã¨[ã¨[[ - Qiita
- bashã®ä¼¼ã¦ã¦ç´ãããããã® [[ / [ / test  ã¯ã©ããéãã®ï¼ - ãããã°ã§ï¼
- Google Shell Style Guide ... is preferred over [, test and /usr/bin/[.
- https://google.github.io/styleguide/shell.xml#Test,%5B_and%5B%5B
[å¿ é ] é åå¤æ°ãå±éããã¨ãã¯ã"ãã§å¿ ãå²ã
ã³ãã³ããfunctionã«æ¸¡ãããå¼æ°ã®å ¨ã¦ãä¿æããã$@ãã¨ããç¹æ®å¤æ°ã使ãæã¯ãå¿ ãã"ãã§å²ã£ã¦ãã ããã å¤ã«åè§ã¹ãã¼ã¹ãå«ãå ´åã«å²ããã©ããã§æåãå¤ãã£ã¦ãã¾ãã¾ãã
# 以ä¸ã®ããã«ã³ãã³ãå®è¡ããå ´å # myfunc 100 200 "foo bar" 300 # 以ä¸ï¼ã¤ã®foræã®çµæãç°ãªã for i in $@; do echo $i; done #=> 100 #=> 200 #=> foo #=> bar #=> 300 for i in "$@"; do echo $i; done #=> 100 #=> 200 #=> foo bar #=> 300
ã"ãã§å²ããªãå ´åã¯ãå¤ã«åè§ã¹ãã¼ã¹ãå«ãå ´åã«ããã§å解ããã¦ãã¾ãã¾ãã ä¾ãã°ãã¹ãã¼ã¹ãå«ããã¡ã¤ã«åãåãåããããªå ´åã«æ£ãããã¡ã¤ã«åãå©ç¨ã§ããªãã¨ãã£ããã¨ãèµ·ããã¾ãã
ãã®ã"$@"ãã¯ãã®ï¼æåã§åºæã®åä½ããã表è¨ã«ãªã£ã¦ãã¾ããfunc "hoge $@"
ã¨ãã£ã表è¨ã§ã¯æç«ãã¾ããã
ã¾ããå¼æ°ã®ä¸è¦§ãåå¾ããå¥ã®æ¹æ³ã«ã$*ãã¨ãããã®ãããã¾ããããã¡ãã§ã¯ã"$*"ãã¨æ¸ãã¦ãå¿ ãåè§ã¹ãã¼ã¹ãåå²ããã¦ãã¾ãã¾ãã ãªã®ã§ãåºæ¬çã«ã$*ãã¯ä½¿ããã"$@"ãã使ãããã«ãã¦ãã ããã
bashã¹ã¯ãªããã®ããæãã®æ¸ãæ¹
ã¹ã¯ãªãããæ¸ãã¨ãã®å ¨ä½çãªè¨è¿°ã®æ¹éã以ä¸ã«ã¾ã¨ãã¾ãã
[å¿ é ] functionã®è¨è¿°æ¹æ³
functionãå®ç¾©ããã¨ãã¯ä»¥ä¸ã®ãã©ã¼ãããã§æ¸ãã¦ãã ããã
- "function"ãæè¨
- ååã®ãã¨ã®ã()ãã¯ä»»æ
- functionå ã®æåã§å¼æ°ããã¼ã«ã«ã«ä¿æ
ä¾ã¨ãã¦ã¯ä»¥ä¸ã®ãããªæ¸ãæ¹ã«ãªãã¾ãã
# helloã¨åºåããé¢æ° # # 1: 追å 表示ããã¡ãã»ã¼ã¸ function func1() { local arg1="$1" echo hello "$arg1" }
bashã§ã¯"function"ãæè¨ããªãã¦ãå®ç¾©ã§ãã¾ããã grepãªã©ã§functionã®å®ç¾©ãæ¢ããããããããã«æè¨ãããã¨ããé¡ããã¾ãã
[å¿ é ] functionãªã©ã®ãããã¯å ã¯ã¤ã³ãã³ããã
bashãæ®éã®ããã°ã©ãã³ã°è¨èªã¨åæ§ã«ã³ã¼ããããã¯ã®ä¸ã§ã¯ã¤ã³ãã³ããã¦è¨è¿°ãã¦ãã ããã
ä¾å¤ã¨ãã¦ãã¢ããã¥ã¡ã³ãã®å ´åãªã©ã§è¡é ã«ç©ºç½ãå
¥ããããªãå ´åã¯é¤ãã¾ãã
function func1() { # ã¤ã³ãã³ã echo "hoge" if [[ -e "file.txt" ]]; then echo "file" fi while true; do echo "loop" break done } function func2() { # ãã¢ããã¥ã¡ã³ãå é¨ãåé¡ãªããã°ã¤ã³ãã³ã cat <<EOS | somework select * from doctor EOS # ãã¢ããã¥ã¡ã³ãã®ãã¼ã«ã¯ä¾å¤ # ãã¢ããã¥ã¡ã³ãå é¨ãè¡é ã«ç©ºç½ãå ¥ããããªããã°ä¾å¤ cat <<EOS > some.yml setting: text: Hello world! EOS # ã¤ã³ãã³ããã¦æ¸ããããã«ããæ¹æ³ cat <<EOS | sed -E 's/^ +\|//' > some.yml |setting: | text: Hello world! EOS } # ãµãããã»ã¹å®è¡ãªã©ãåæ§ ( echo "in subprocess" do_something )
[å¿ é ] ã¤ã³ãã³ãã¯ã¹ãã¼ã¹ã®ã¿
ãã¡ã¤ã«ä¸ã®ã¤ã³ãã³ãã¯ã¹ãã¼ã¹ã§2æåããããã¯4æåã§è¨è¿°ãã¦ãã ããã
ã¿ãæåã¯ä¸å¯ãã¹ãã¼ã¹ã»ã¿ãæ··å¨ã¯ä¸å¯ã§ãã
ã¿ãæåã¯gitlabãªã©ã§ã®è¡¨ç¤ºã®æåå¹
ãªã©ãæå®ã§ããªããããè¦ãç®ãçµ±ä¸ããããã«ã¹ãã¼ã¹ã¨ãã¦ãã ããã
[å¿ é ] ï¼ãã¡ã¤ã«ä¸ã®è¨è¿°ã®é ç½®
ã·ã§ã«ã¹ã¯ãªããã«è¨è¿°ãããã¨ã«ãªãè¦ç´ ã¯ä»¥ä¸ã®ãããªãã®ãããã¾ãã
- å¤é¨ã¹ã¯ãªããã®source
- ã°ãã¼ãã«å®æ°
- ã°ãã¼ãã«å¤æ°
- å¼æ°parseå¦ç
- é¢æ°å®ç¾©
- ã¹ã¯ãªããã®ã¡ã¤ã³å¦çã®ã¨ã³ããªã¼é¨å
ãããã®è¦ç´ ã¯ãè¦ç´ ãã¨ã«ä¸å¡ã«ãªãããã«è¨è¿°ãã¦ãã ããã å®æ°ãé¢æ°ã®å®ç¾©ããã¡ã¤ã«ä¸ã«æ£å¨ããã¨ãè¦è½ã¨ãã®åå ã«ãªã£ã¦ã¹ã¯ãªããã®å ¨ä½ãèªã¿ã¥ãããªãã¾ãã
åè¦ç´ ã®è¨è¿°é åºã¯åºæ¬çã«åè¿°ã®ãã³ãã¬ã¼ãã®ãããªé åºã§è¨è¿°ããããã«ãã¦ãã ããã
[å¿ é ] mainé¢æ°ãä½æãã¦ã¨ã³ããªã¼ãã¤ã³ãã¨ãã
å¼æ°ã®ãã¼ã¹å¦çãã°ãã¼ãã«ã®å®æ°ã»å¤æ°ã®å®ç¾©ãé¤ãã¦ä½ããã®å¦çãè¨è¿°ããã¨ãã¯ãå¿ ãé¢æ°ã¨ãã¦å®ç¾©ãã¦ãã ããã ã¹ã¯ãªããã®ã¨ã³ããªã¼ãã¤ã³ãã¨ãªãã¡ã¤ã³å¦çããmainé¢æ°ãå®ç¾©ãã¦ãããã¹ã¯ãªããã®æå¾ã§å¼ã³åºãããã«è¨è¿°ãã¦ãã ããã
ã¾ããmainé¢æ°ã®å®è¡ãif [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
ã§ãããã¯ãããã¨ã§ãã¹ã¯ãªãããç´æ¥å®è¡ããã¨ãã®ã¿å¦çãè¡ããsourceã§èªã¿è¾¼ã¾ããã¨ãã¯å¦çãè¡ããªãããã«ã§ãã¾ãã
ããã«ã¯ä»¥ä¸ã®å©ç¹ãããã¾ãã
- å ¨ã¦ãé¢æ°åãããã¨ã§ã³ã¼ãã®ã¤ã³ãã³ããæã£ã¦èªã¿ããããªã
- äºåæºåå¦çã¨ãã¡ã¤ã³å¦çã¨ãé¢æ°ã¨ãã¦åãã¦å®ç¾©ãããã¨ã§å¦çã®æå³ãèªã¿åãããããªã
- ãããã°ä½æ¥ããã¹ãã«ã¦ã¡ã¤ã³å¦çãå®è¡ãããåã ã®é¢æ°ã®ã¿ãåä½ããããã¨ãã§ããããã«ãªã
[å¿ é ] å¤æ°åã®å½å
é¢æ°ãå¤æ°ã®å½åã«ã¤ãã¦ã¯ä»¥ä¸ã®è¡¨è¨ã¨ãã¦ãã ããã
- snake caseã§å½åãã (ä¾: use_this_formatãOUTPUT_DIR)
- ã°ãã¼ãã«ãªå¤æ°ã»å®æ°ã¯å ¨ã¦å¤§æå(ä¾: OUTPUT_DIR)
- ãã¼ã«ã«å¤æ°ã¯å°æå (ä¾: use_this_format)
- æ¥ä»ã¯å¤ã®æ¸å¼ãåããæ§ã« foo_bar_date ãã㯠foo_bar_yyyymmdd ã®ããã«ãã¦ããã
- æéã表ãæ°å¤ãªã©ã¯ãå¤æ°åã§åä½ããããããã«ãããfoo_sec (ç§åä½)ãbar_min(ååä½)ãªã©
- é¢æ°ã¯å¦çã®å
容ã«æ²¿ã£ãã¹ã¯ãªããå
¨ä½ã§æå³ãéãååã«ãã
- NG: create_sqlãOK: create_message_list_sql
[å¿ é ] å¤æ°åã¯é·ããªã£ã¦ãããããããä»ãã
bashã¯ã°ãã¼ãã«å¤æ°ã®å©ç¨ãå¤ããªããã¡ã§ããã®æ§è³ªããå®ç¾©å ´æã¨å©ç¨å ´æãé¢ãããªã©ãã¦èªè§£ãé£ãããªããã¨ãããã¾ãã ãã®ãããã°ãã¼ãã«å¤æ°ã«ã¤ãã¦ã¯å¤ã«çããããããé·ãã»ããããã¨èãã¾ãã
å¤æ°åã®ä¾
- ããç¨åº¦çç¥ãããããã®ã¯OKã¨ãã¾ãããåºæ¬çã«ã¯çç¥ã®å¿
è¦ã¯ãªãã¨æãã¾ã
- OK: TBL (= table), LBL (= label )
- NG: L (ä¸æåã ãã®å¤æ°), ãã_L (æ¥å°¾èªãªã©ã§ãï¼æåã¯ã ãï¼
[å¿ é ] ã³ãã³ããfunctionãä½æããã¨ãã¯ãã£ããã¨ã³ã¡ã³ããè¨è¿°ãã
ã³ãã³ããfunctionã®ä½¿ãæ¹ãä½æãããæå³ãªã©ã«ã¤ãã¦ã³ã¡ã³ããæ®ãããã«ãã¦ãã ããã sourceãã¦ä½¿ãåæã®ãã¡ã¤ã«ã§ãããã®ãã¡ã¤ã«èªä½ã«ãç¨éããã®ãã¡ã¤ã«ã®æå³ãè¨è¼ãã¦ãã ããã
以ä¸ã®ç¹ãæ確ã«ãªãããã«è¨è¼ãããã¨ãå¿ããã¦ãã ããã
- ãã®ã¹ã¯ãªããã»ã³ãã³ãã®ç¨éã»ä½¿ãæ¹ã»å©ç¨ä¸ã®æ³¨æç¹
- åæã¨ãã¦å®ç¾©ãã¦ããç°å¢å¤æ°ãã³ãã³ãããã¡ã¤ã«ãªã©ã®ä¾åé¢ä¿
- å¼æ°ããªãã·ã§ã³ã®å®ç¾©
- ã¢ã¦ããããã¨ãªããã®ã®å®ç¾©ï¼ãã¡ã¤ã«ãDBã®ãã¼ãã«ãªã©ï¼
functionã®ã³ã¡ã³ãä¾
# CSVåºåç¨ã«å¤ãã¨ã¹ã±ã¼ããã # # $1: ã¨ã¹ã±ã¼ã対象ã®æåå # åºå: ã"ãã§å²ã£ãã¨ã¹ã±ã¼ããããæåå function csv_escape() { local value="$1" echo "\""$(echo "$value" | sed 's/"/""/g')"\"" }
[å¿ é ] ã³ãã³ãå®è¡çµæã®æ¨æºåºåãå¤æ°ã§åå¾ããã¨ãã¯ã$(cmd)ã表è¨ã使ã
ã³ãã³ãå®è¡ã®æ¨æºåºåãåå¾ããã«ã¯ãã`command`ãã¨ã$(command)ãã®ï¼éã表è¨ãããã¾ãããå¿ ãã$(command)ãã§è¨è¿°ãã¦ãã ããã
ã`ãã®è¡¨è¨ã¯è¨å·ãå°ããè¦èªãã¥ããã®ã¨ãã$()ãã§ããã°å ¥ãåã§è¨è¿°ãããã¨ãã§ããããã§ãã
VAL=$(echo "hoge")
[å¿ é ] localãreadonlyãä»ãã¦å¤æ°ãå®ç¾©ãã
bashã§ã¯functionå
ã§å®ç¾©ããå¤æ°ãã°ãã¼ãã«å¤æ°ã«ãªãã¾ãã
ãããé¿ããããã«functionå
ã§å¤æ°ãå®ç¾©ããã¨ãã¯local
ãä»ãã¦ãã ããã
ããããããä»ããã«æå³çã«ã°ãã¼ãã«å¤æ°ãå®ç¾©ãããã¨ãã¦ãã¨ãã¯ãä½ãç¡è¶ããããã¨ãã¦ããã¨æã£ã¦åèãã¦ãã ããã
ãã ããlocal
ã®å¤æ°ã®ã¹ã³ã¼ãã¯æ®éã®ããã°ã©ãã³ã°è¨èªã¨ã¯ç°ãªãã®ã§éä¿¡ããããªãããã«æ³¨æãã¦ãã ããã
localã¯ããã宣è¨ããããããã¯å
ã®å¼ã³åºãå
¨ã¦ã«å½±é¿ãã¾ãã
function hoge() { local val1=100 # ããã¯functionå é¨ããå¼ã³åºãããã³ãã³ãã®å ã¾ã§å½±é¿ãã fuga } function fuga() { echo $val1 } hoge #=> 100 fugaå é¨ã®val1ã®åºå㯠100ã表示ããã echo "val1=[${val1}]" #=> val1=[] ããã§ã¯val1ã¯åç §ã§ããªã
[å¿ é ] å¤ãå¤æ´ããªãå®æ°ãå®ç¾©ããã¨ãã¯readonlyãä»ãã¦å®ç¾©ãã
å¤ãå¤æ´ããªãå®æ°ãå®ç¾©ããã¨ãã¯ãreadonly
ãä»ãã¦ãã ããã
ãããã¤ããå®æ°ã¯ä¸æ¸ãã§ä»£å
¥ãããã¨ããã¨ã¨ã©ã¼ã«ãªãã¾ãã
readonly HOGE=10 HOGE=100 # ããã§ã¨ã©ã¼
[æ¨å¥¨] ã°ãã¼ãã«å¤æ°ãã§ããããã使ããªã
bashã¯å¤æ°ã®ã¹ã³ã¼ããå¶éãã¥ããã°ãã¼ãã«å¤æ°ãå¤ç¨ããã¡ã§ãã
ã§ãããã°ãã¼ãã«å¤æ°ã使ãã¨å®è£
ãèªã¿è§£ãã¥ãããªãããåºæ¬çã«ã¯ã°ãã¼ãã«å¤æ°ã使ããªãããã«å®è£
ãã¦ãã ããã
functionãèªã¿è§£ãã¨ãã«ããã®functionã ããè¦ãã°å¦çãç解ã§ããããã«ãªããã¨ãå¿ããã¦ãã ããã ã°ãã¼ãã«å¤æ°ãç´æ¥åç §ã§ããã¨ãã¦ããã®å¤æ°ãç´æ¥ã¤ããã®ã§ã¯ãªããå¼æ°ãã渡ãããã«ãã¦ãã ããã
ãã ããä½ãããã®å¤æ°ã渡ãã¦åãã¨éã«èªã¿ã«ãããªãå ´åãããã¾ãã®ã§ãã°ãã¼ãã«ãªå®æ°ãã»ã¼å®æ°çã«ä½¿ããããªãã®ã¯ç´æ¥ä½¿ã£ã¦ãããã§ãã
NG
VAL1=$1 function func1() { echo "$VAL1" } func1
OK
readonly WORKDIR=/tmp/workspace readonly LOGFILE=$WORKDIR/app.log INPUT_MESSAGE=$1 function func1() { local message=$1 echo "$message" > $LOGFILE } func1 "$INPUT_MESSAGE"
[æ¨å¥¨] è¤éãªå¦çãå¿ è¦ã«ãªã£ããbash以å¤ã使ããã¨ãèãã
bashã¯ç°¡æãªãã¨ãè¡ããªã便å©ã§ãããè¤éãªãã¼ã¿æ§é ã®ãã®ãæ±ãå ´åãªã©ã«ã¯åãã¾ããã
ãã®ãããbashã§ããã®ãé£ãããã¨ãã§ã¦ãããbashã使ãã®ã諦ãã¦å¥ã®è¨èªãã³ãã³ãã使ããã¨ãèãã¦ãã ããã
bash以å¤ã®ã³ãã³ãã»è¨èª
- sed / tr ã³ãã³ã
- awk
- perl / python / ruby
[å¿
é ] bashã¹ã¯ãªããã«ã¯set -euC
ãä»ãã
bashã®å®è¡æã®ãªãã·ã§ã³ã«ã¹ã¯ãªãããä½æããä¸ã§æç¨ãªãã®ãããã¾ãã
ãªãã·ã§ã³ã¯bashã³ãã³ãã®å¼æ°ã¨ãã¦ãã¦ããããã¹ã¯ãªããä¸ã«set
ã³ãã³ãã§æå®ã§ãã¾ãã
ã¹ã¯ãªãããä½æããã¨ãã¯ããã®ãªãã·ã§ã³ã®ä¸ã§åºæ¬çã«set -euC
ãä»ãã¦ãã ããã
#!/bin/bash # æåã«è¨å® set -euC echo "myscript"
ã·ãã³(#!/bin/bash
)ã®ã¨ããã«è¨è¿°ãããã¨ãã§ãã¾ãããããã ã¨ãããã°æãªã©ã§ã¹ã¯ãªãããbash
ã³ãã³ãã§ç´æ¥å®è¡ãããããªã¨ãã«ãªãã·ã§ã³ãæ©è½ããªããªãã®ã§set
ã§æå®ããæ¹ãããã¨èãã¾ãã
# ãããã°ã¨ãã¦-xã¤ãã§å®è¡ bash -x myscript.sh #=> ãã®å®è¡æ¹æ³ã ã¨ã·ãã³ã使ãããªã
ãªãã·ã§ã³ã®æå³ã¯ä»¥ä¸ã®ã¨ããã§ã:
-e
ãªãã·ã§ã³
ã¹ã¯ãªããä¸ã®ã³ãã³ãå®è¡ãã¨ã©ã¼çµäº(çµäºã³ã¼ã 0以å¤ã§ã®çµäº)ããå ´åã«ã¹ã¯ãªãããåæ¢ãã¾ãã éã«è¨ãã°ããã®ãªãã·ã§ã³ãªãã§ã¯ã³ãã³ããã¨ã©ã¼çµäºãã¦ããã®ã¾ã¾å¦çãç¶ç¶ãã¦ãã¾ãã¾ãã ãã®ãããæ³å®å¤ã®ãã°ãçªçºçãªã¨ã©ã¼çºçæã«ã誤ã£ã¦å¾ç¶ã®å¦çããã¦ãã¾ããããªãã¨ãç¡ãããã«ããã®ãªãã·ã§ã³ã¯å¿ ãã¤ããã¹ãã§ãã
注æç¹ã¨ãã¦ãgrep
ãls
ãªã©ã®ã³ãã³ãã¯çµæã0件ã«ãªããããªå ´åã«çµäºã³ã¼ãã0以å¤ã«ãªãã®ã§ã
ããããå ´åã¯ä¸æçã«-e
ãªãã·ã§ã³ã解é¤ããããã«ããå¿
è¦ãããã¾ãã
set -e echo "ã¨ã©ã¼çµäºããç¯å²" set +e # ä¸æçã«ã¨ã©ã¼ã§åæ¢ããªãããã«ãã myvalue=$(grep "hoge" myfile.txt) RET=$? # ã³ãã³ãã®çµäºå¤ãåå¾ããã¨ãã¯setã®åã«ä¿åããªãã¨ã$?ãsetã³ãã³ãã®çµæã§ä¸æ¸ãããã set -e # å度ãæåå # 以ä¸ã®ããã«æå¾ã«å¿ ãæ£å¸¸çµäºããã³ãã³ããã¤ãªãããã¨ã§ãåé¿ã§ãã¾ã grep "hoge" mfile.txt || true
-u
ãªãã·ã§ã³
-u
ãªãã·ã§ã³ãæå®ããã¨æªå®ç¾©å¤æ°ã使ããã¨ããã¨ã¨ã©ã¼çµäºããããã«ãªãã¾ãã
ããã«ãããå¤æ°åã®ã¿ã¤ããã¹ãåæåãããã§ã®å¤æ°ã®å©ç¨ãªã©ãé²ããã¨ãã§ãã¾ãã
set -u echo "$VAL1" # ããã§ã¨ã©ã¼ã«ãªã
åé¿æ¹æ³
set -u # ããã©ã«ãå¤æå®ãããã°åé¿å¯è½ echo "${VAL1:-}" # ãã®å ´åã¯OK
-C
ãªãã·ã§ã³
-C
ãªãã·ã§ã³ãä»ããã¨ãªãã¤ã¬ã¯ãã§ã®ãã¡ã¤ã«ä¸æ¸ãæã«ã¨ã©ã¼çµäºããããã«ãªãã¾ãã
ãªãã¤ã¬ã¯ãã§ãã¡ã¤ã«ãçæããã¨ãã«æ³å®å¤ã«ä¸æ¸ããã¦ãã¾ããã¨ãé²ããã¨ãã§ãã¾ãã
set -C touch file1.txt echo "Hello" > file1.txt # ããã§ã¨ã©ã¼ # 以ä¸ã®ããã«ã>|ãã¨è¨è¿°ããã¨ä¸æ¸ããå¯è½ echo "Hello" >| file1.txt
[æ¨å¥¨] ã¹ã¯ãªããçµäºæã«å¿
ãå¦çãããããã¨ãã¯trap
ãå©ç¨ãã
trap
ã³ãã³ãã使ããã¨ã§ãã¹ã¯ãªãããçµäºããã¨ãã«å¿
ãå®è¡ããå¦çãæå®ãããã¨ãã§ãã¾ãã
ãããã¤ãã£ã¦ãã¹ã¯ãªããã®æå¾ã«æé¤å¦çãªã©ãè¡ããã¨ãã§ãã¾ãã
TEMPFILE="" function main() { echo "start main" # å¼·å¼ãªä¾ã§ãããããã§TEMPFILEãåæå TEMPFILE=$(mktemp) echo "hoge fuga" > $TEMPFILE } function cleanup() { # ãã¤ã©ããªã¨ãã«å®è¡ããããã¯åãããªãã®ã§ãä¸å¯§ã«äºåãã§ãã¯ãããæ¹ããã # ä¾ãã°ãmktempã失æãã¦TEMPFILEã空ã®ã¨ãã«ãããå®è¡ããããã if [[ -n "$TEMPFILE" && -e "$TEMPFILE" ]]; then mv -n "$TEMPFILE" ~/.trash/ # æ¶ããã«ã´ãç®±ãã©ã«ãã«ç§»å fi } # EXITæã«ãcleanup ãå®è¡ trap cleanup EXIT main
bashã¹ã¯ãªããã®å®è£ ãã¯ããã¯
ããããã³ãã³ãã®Tips
mkdir -p
- æå®ãããã¹ãåé層ãå«ãã¦ãã¹ã¦çæãã
- æ¢ã«ãã£ã¬ã¯ããªãããå ´åã¯ã¨ã©ã¼ã«ããªããç¡è¦ããã
- æ¢åãã£ã¬ã¯ããªã®æç¡ã確èªãã¦ããmkdirããå¿ è¦ããªã
mv -v, cp -v, rm -v
- å¤ãã®ã³ãã³ãã¯
-v
ãã¤ããã¨ã³ãã³ãã®å®è¡ãã°åºåããã - mv, cp, rmã¯ããããã®ãã¡ã¤ã«æä½ã®å ã»å ã®ãã¡ã¤ã«ã表示ããã®ã§åä½çµæã確èªã§ãã
- å¤ãã®ã³ãã³ãã¯
mv -n, cp -n
- ãã¡ã¤ã«æä½ç³»ã®ã³ãã³ãã«
-n
ãªãã·ã§ã³ãä»ããã¨ä¸æ¸ãé²æ¢ã«ãªã - 移åå ãã³ãã¼å ã«æ¢ã«ååã®ãã¡ã¤ã«ããã£ãå ´åã«ã¨ã©ã¼çµäºãã
- ãã¡ã¤ã«æä½ç³»ã®ã³ãã³ãã«
é¢æ°ããè¤æ°ã®å¤ãè¿ãæ¹æ³
é¢æ°ããè¤æ°ã®å¤ãè¿ãããå ´åã¯ä»¥ä¸ã®ããã«printã³ãã³ãã使ãã¨å¯¾å¿ã§ãã¾ãã ãã¾ããããããããããã§ã¯ãªãã§ããã使ãæ¹æ³ã®ãã¯ããã¯ã¨ãã¦ç´¹ä»ãã¾ãã
function some_func() { local retval_output_file=$1 local retval_temp_table=$2 print -v $retval_output_file "%s" output.csv print -v $retval_temp_table "%s" temp_table_name } function main() { ã# 以ä¸ã®`local`ã`&&`ãã³ãã³ãå¼ã³åºãã®æ¸ãæ¹ãæ¨å¥¨ local output_file temp_table && some_func output_file temp_table echo "$output_file" #=> output.csv echo "$temp_table" #=> temp_table_name }
ãã°ã®åºãæ¹
ãã°ãåºåããã¨ãã«ãå¦çä¸ã®ãã¡ã¤ã«ãé¢æ°ãå®è¡è¡æ°ãªã©ãåºåãããã¨ãã§ãã¾ãã
function log() { local fname=${BASH_SOURCE[1]##*/} echo -e "$(date '+%Y-%m-%dT%H:%M:%S') ${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]} $@" } log "message" #=> 2017-01-20T10:53:03 myscript.sh:10:main message
éä¸çµäºã§ãå®å ¨ãªãã¡ã¤ã«çææ¹æ³
ãã¡ã¤ã«ãçæããã¨ãã¯ãä¸æ¦å¥åã®ãã¡ã¤ã«ã«åºåããä¸ã§æå¾ã«ãªãã¼ã ããããã«ããã¨ããã§ãã ãã¡ã¤ã«ã®çæéä¸ã§ã¨ã©ã¼ã«ãªã£ãããæååæ¢ãããããå ´åã«ãä½æéä¸ã®ãã¡ã¤ã«ãééã£ã¦æçµãã¼ã¿ã¨ãã¦ä½¿ã£ã¦ãã¾ããªãããã«ãããã¨ãã§ãã¾ãã ã¾ããåå®è¡æã«æ¢ã«ãã¡ã¤ã«ãããã¨ãã¯ã¹ããããããããªå¦çã«ããã¨ãã«ãæåã§ãã
if [[ ! -e target_file.csv ]]; then find ./hoge/ -name \*.txt >> target_file.csv.tmp echo "special.txt" >> target_file.csv.tmp mv target_file.csv.tmp target_file.csv fi
ä¸æçã«ç°å¢å¤æ°ãè¨å®ã»ä¸æ¸ãããç¶æ ã§ã³ãã³ããå®è¡ãã
ã·ã§ã«ã§ã³ãã³ããå®è¡ããã¨ãã«ãä¸æçã«ç°å¢å¤æ°ãä¸æ¸ããã¦ã³ãã³ãå®è¡ãããã¨ãã§ãã¾ãã
# è¨èªè¨å® export LANG=ja_JP.UTF-8 # æ¥æ¬èªã§æ¥æã表示 date #=> 2018å¹´ 1æ23æ¥ ç«ææ¥ 15æ33å09ç§ JST # 以ä¸ã®ã¨ãã®ã¿ãLANGãå¤ãã LANG=C date #=> Tue Jan 23 15:33:09 JST 2018 # LANGå¤æ°ã¯å¤ãã£ã¦ããªã echo $LANG #=> ja_JP.UTF-8
ã¾ãã以ä¸ã®ããã«ã()ãã§å²ãã¨ããã®ç¯å²å
ã®ã¿ãµãããã»ã¹ã§å®è¡ãããã¨ã«ãªãããã
ãã®å
é¨ã§ã®å¤æ°ã¸ã®å¤æ´ã¯ã()ãã®å¤å´ã«å½±é¿ãã¾ããã
ããã¯ãcd
ã§ã®ãã©ã«ã移åãåãå¹æãããã¾ãã
VAL1=10 echo $VAL1 #=> 10 pwd #=> ~/ ( VAL1=100 echo $VAL1 #=> 100 cd /usr/local pwd #=> /usr/local ) echo $VAL1 #=> 10 pwd #=> ~/
ã³ãã³ããfunctionã«é·ãæååã渡ãã¨ãã¯æ¨æºå ¥åã使ã
表示ç¨ã®æè¨ãSQLæãªã©ã®é·ãæååã渡ããããªå ´åã¯ãå¼æ°ã§æ¸¡ãããããã¤ãã使ã£ã¦æ¨æºå ¥åãã¤ãã£ãæ¹ãè¯ãã§ãã
function func1() { # å¼æ°ã空ã"-"ã®å ´åã¯æ¨æºå ¥åããåå¾ local val=$1 if [[ -z "$val" || "$val" == "-" ]]; then val=$(cat -) fi echo $val } echo "foo" | func1 #=> foo echo "bar" | func1 "-" #=> bar func1 "hoge" #=> hoge
bashã®ãããã°ææ³
read -p "pause"
ã§ä¸æåæ¢
readã³ãã³ããã¤ããã°Enterãã¼ãå
¥åããã¾ã§åæ¢ã§ãã¾ãã
å¦çã®éä¸ç¶æ
ã確èªããã¨ãããtrapãªã©ã§æé¤å¦çãä»è¾¼ãã å ´åã§ãããå®è¡ãããã¾ãã®ç¶æ
ã確èªãããããªã¨ããªã©ã«ä¾¿å©ã§ãã
trap DEBUG, trap ERR
trap ã§ã·ã°ãã«ã¨ãã¦DEBUGãæå®ããã¨ã¹ã¯ãªããä¸ã®å ¨è¡ã®å®è¡æã«å¦çãå²ãè¾¼ãã¾ãã
# ã¹ãããå®è¡ã¨åãåä½ãåºæ¥ã trap 'read -p "pause"' DEBUG
trap ã§ã·ã°ãã«ã¨ãã¦ERRãæå®ããã¨ã³ãã³ãã®ã¨ã©ã¼çµäºæã«å¦çãå²ãè¾¼ãã¾ãã
# ã¨ã©ã¼ãçºçããã¨ãã«ãã®ã¨ãã®stacktraceã表示ãã trap caller ERR
ã¹ã¯ãªããã®éä¸ã§ã³ãã³ãã®å¼æ°ãè¨å®ãã
set --
ã«ç¶ãã¦æååãè¨è¿°ããã¨ãããããå®è¡ä¸ã®ã¹ã¯ãªããã®å¼æ°ã¨ãã¦ä¸æ¸ãã§ãã¾ãã
ã¹ã¯ãªããã®ãã¹ããè¨è¿°ãããããªã¨ãã«æç¨ã§ãã
#!/bin/bash # sample.sh echo ORIGINAL ARGS: "$@" # ã¹ã¯ãªããã®å¼æ°ãè¨å® set -- word1 "some value" echo CHANGED ARGS: "$@"
ä¸è¨ãå®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
$ ./sample.sh hoge fuga ORIGINAL ARGS: hoge fuga CHANGED ARGS: word1 some value
set -x
ãªãã·ã§ã³
bashã®-x
ãªãã·ã§ã³ãã¯ãããã°ãããã¨ãã«æç¨ã§ãã
ãããæå®ããã¨ãã¹ã¯ãªãããå¦çããã³ãã³ãã表示ããã¾ãã
å¤æ°ãå®éã®å¤ã«å±éããã¦è¡¨ç¤ºãããã®ã§ãå®éã«å¦çãããå
容ãææ¡ãããã¨ãã§ãã¾ãã
set -x VAL1=100 echo "Output: ${VAL1:-hoge}"
å®è¡ããã¨ä»¥ä¸ã®ããã«è¡¨ç¤ºããã¾ã
+ VAL1=100 + echo 'Output: 100' Output: 100
注æ
ãã¹ã¯ã¼ããå¤æ°ã«ä¿æãã¦ä½¿ããããªå¦çã§ã-x
ä»ãã®ãã°ããã°ãã¡ã¤ã«ã«ä¿åãããããªããã¨ã
-x
ã¯å¤æ°ã®å
容(ã¤ã¾ãããã¹ã¯ã¼ã)ãå±éããç¶æ
ã§åºåãããã®ã§ããã°ã«ãã¹ã¯ã¼ããæ®ã£ã¦ãã¾ãã¾ãã
åèè³æ
æ¬è³æã®ä½æã«ããã£ã¦ä»¥ä¸ã®è¨äºãªã©ãåèã«ããã¦ããã ãã¾ããã
ã·ã§ã«ã¹ã¯ãªãããæ¸ãã¨ãã«æ°ãã¤ãã9ç®æ¡ - Qiita
bashãããã°Tips - Qiita
What is the bash equivalent to Python's
if __name__ == '__main__'
? - Stack Overflowã·ã§ã«ã¹ã¯ãªããã®ãã®ã³ã°ã楽ã«ããtips - Qiita
Bashã¹ã¯ãªããã®ãã¼ãã·ã¼ãã¨ä¾¿å©ãªã¹ããããã¾ã¨ã - ãªã¼ãã³ã½ã¼ã¹ãããã
ã¨ã³ã¸ãã¢åé
ã¨ã ã¹ãªã¼ã§ã¯èªèº«ã§æãåãããæè¡ã§å»çã®èª²é¡ã解決ããã¨ã³ã¸ãã¢ãåéãã¦ãã¾ãã ãã®è¨äºï¼or ä»ã®è¨äºãï¼ãèªãã§èå³ãæã£ãæ¹ã¯ãã²ä¸è¨ãªã³ã¯ãããå¿åãã ããï¼