사용자 도구

사이트 도구


linux:bash

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
linux:bash [2016/03/28 14:11]
kwon37xi
linux:bash [2024/02/07 08:41] (현재)
kwon37xi [File Path dir/filename 분리]
줄 1: 줄 1:
 ====== Bash ====== ====== Bash ======
 +  * https://www.gnu.org/software/bash/manual/bash.html
 +
 +===== shell template =====
 +  * [[https://betterdev.blog/minimal-safe-bash-script-template/|Minimal safe Bash script template | Better Dev]]
 +
 ===== Redirect ===== ===== Redirect =====
   * http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/   * http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/
 +  * [[https://catonmat.net/bash-one-liners-explained-part-three|Bash One-Liners Explained, Part III: All about redirections]]
   * ''command-name &>file'' : 명령의 표준 출력과 표준 에러를 모두 file로 지정   * ''command-name &>file'' : 명령의 표준 출력과 표준 에러를 모두 file로 지정
   * ''cat textfile.txt > somefile 2>&1'' : 표준 출력은 somefile로 지정되고 표준에러도 표준출력(핸재 somefile)로 함께 보낸다.   * ''cat textfile.txt > somefile 2>&1'' : 표준 출력은 somefile로 지정되고 표준에러도 표준출력(핸재 somefile)로 함께 보낸다.
줄 11: 줄 17:
   * ''$@''   * ''$@''
   * ''$?'' : 직전 명령 exit code. ''0''이면 정상 종료.   * ''$?'' : 직전 명령 exit code. ''0''이면 정상 종료.
 +  * ''$-'' : 현재 shell 의 설정
 +
 +===== $- =====
 +  * 현재 shell 의 설정값
 +  * [[https://stackoverflow.com/questions/42757236/what-does-mean-in-bash|shell - What does $- mean in Bash? - Stack Overflow]]
 +  * ''H'' : histexpand
 +  * ''m'' : monitor
 +  * ''h'' : hashall
 +  * ''B'' : braceexpand
 +  * ''i'' : interactive
  
 +==== 직전 명령의 argument 대체 ====
 +  * [[https://stackoverflow.com/questions/41385015/what-is-bang-dollar-in-bash|syntax - What is bang dollar (!$) in Bash? - Stack Overflow]]
 +  * ''!$'' : 직전 명령의 마지막 인자. history로 보면 대체된 결과가 나옴.
 +  * ''$_'' : 직전 명령의 마지막 인자. history 로 보면 그냥 ''$_''가 나옴.
 +  * ''!^'' : 첫번째 인자
 +  * ''!:2'' : 두번째 인자
 +  * ''!:2-$'', ''!:2*'' : 두번째부터 마지막 인자까지
 +  * ''!:2-'' : 두번째 부터 마지막에서 바로 앞 인자까지
 +  * ''!:2-3'' : 두번째~세번째 인자까지
 +  * ''!*'' : 모든 인자들
 +  * ''$$'' : 현재 스크립트 혹은 shell 의 PID
 +  * ''$!'' : 직전 명령의 PID [[https://www.cyberciti.biz/faq/how-to-return-pid-of-a-last-command-in-linux-unix/|How to return pid of a last command in Linux / Unix - nixCraft]]
 ===== 실행 결과 변수에 저장 ===== ===== 실행 결과 변수에 저장 =====
 <code sh> <code sh>
줄 20: 줄 48:
 test=`basename "$file"` test=`basename "$file"`
 </code> </code>
 +
 +===== 환경변수에 저장된 환경변수 이름으로 값 얻기 =====
 +  * [[https://unix.stackexchange.com/questions/229849/indirectly-expand-variables-in-shell|bash - Indirectly expand variables in shell - Unix & Linux Stack Exchange]]
 +  * ''!환경변수이름을가진변수'' : 붙여써야 한다.
 +<code sh>
 +foo=bar
 +test=foo
 +echo ${!test}
 +</code>
 +
 +
 +===== 계산 하기 =====
 +  * ''%%$((계산식))%%'' 로 계산결과를 받을 수 있다.
 +  * [[https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html#Shell-Arithmetic|Shell Arithmetic (Bash Reference Manual)]]
 +
 +<code sh>
 +$ echo "2 + 3 = $((2+3))"
 +2 + 3 = 5
 +</code>
 +
 ===== 16진수/10진수(hex/dec)간 변환 ===== ===== 16진수/10진수(hex/dec)간 변환 =====
 <code sh> <code sh>
줄 34: 줄 82:
 </code> </code>
 [[http://www.unixcl.com/2007/12/hex-to-decimal-conversion-bash-newbie.html|Hex to decimal conversion bash newbie]] [[http://www.unixcl.com/2007/12/hex-to-decimal-conversion-bash-newbie.html|Hex to decimal conversion bash newbie]]
 +
 +===== for loop 숫자 =====
 +[[http://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-width|numbers - How to zero pad a sequence of integers in bash so that all have the same width?]] 포맷팅된 숫자로 for loop 돌기
 +<code sh>
 +# -f "%0숫자g" : 숫자만큼 0채우기
 +# -w : 제일큰 숫자를 기준으로 알아서 0 채워주기
 +for i in $(seq -f "%05g" 10 15)
 +do
 +  echo $i
 +done
 +
 +# 결과
 +00010
 +00011
 +00012
 +00013
 +00014
 +00015
 +</code>
 +
 +<code sh>
 +# printf 사용
 +i=99
 +printf "%05d\n" $i
 +# 결과
 +00099
 +
 +# 결과를 변수에 저장하기 -v
 +i=99
 +printf -v j "%05d" $i
 +echo $j
 +#결과
 +00099
 +</code>
 +
 +bash 4.x 에서는
 +<code sh>
 +for i in {00..10}; do
 +  echo $i
 +done
 +# 결과
 +00
 +01
 +02
 +03
 +04...
 +</code>
 +
 ===== 변수에 값 입력 받기 ===== ===== 변수에 값 입력 받기 =====
 <code sh> <code sh>
줄 47: 줄 143:
 ===== VI 모드/ Emacs 모드 ===== ===== VI 모드/ Emacs 모드 =====
   * VI 에디터 편집 모드로 명령행을 변경한다.   * VI 에디터 편집 모드로 명령행을 변경한다.
-  * [[http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/|Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)]] <code sh>+  * [[http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/|Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)]] 
 +  * [[https://www.computerhope.com/unix/bash/bind.htm|Bash bind builtin command help and examples]] 
 +<code sh>
 set -o vi set -o vi
 </code> </code>
줄 72: 줄 170:
   * 명령모드 ''*'' : 모든 자동완성 대상을 입력하기   * 명령모드 ''*'' : 모든 자동완성 대상을 입력하기
   * 명령모드 ''<Ctrl-t>'' : 문자 두 개 교체   * 명령모드 ''<Ctrl-t>'' : 문자 두 개 교체
 +
 +===== Shortcuts =====
 +  * [[https://blog.ssdnodes.com/blog/cheatsheet-bash-shortcuts/|Cheatsheet: Productivity-boosting Bash shortcuts | Serverwise]]
 +  * [[https://ostechnix.com/list-useful-bash-keyboard-shortcuts/|The List Of Useful Bash Keyboard Shortcuts - OSTechNix]]
 +  * [[https://www.youtube.com/watch?v=iKzoYUErEM0|Custom Bash config - Set up of .bashrc/.inputrc files for a fast and efficient shell experience - YouTube]]
 +  * ''.inpurc'' 등을 통해 bash 용 단축키를 만들 수 있다.
 +
 ===== History에 시간 남기기 ===== ===== History에 시간 남기기 =====
 [[http://man7.org/linux/man-pages/man3/strftime.3.html|strftime 포맷]] [[http://man7.org/linux/man-pages/man3/strftime.3.html|strftime 포맷]]
줄 90: 줄 195:
 dirname "/path/to/filename.ext" # /path/to dirname "/path/to/filename.ext" # /path/to
 basename "/path/to/filename.ext" # filename.ext basename "/path/to/filename.ext" # filename.ext
-</code>+basename "/path/to/filename.ext" .ext # filename - 확장자까지 제거
  
 +
 +# filename 이라는 환경변수가 있을때
 +filename="filename.ext"
 +
 +# 확장자 (extension) 추출
 +"${filename##*.}" 
 +# 확장자를 제외한 파일 이름 추출
 +"${file%.*}"
 +</code>
 +  * [[linux:realpath|realpath]] : full 경로 확인
 ===== Filename name / extension 분리 ===== ===== Filename name / extension 분리 =====
   * [[http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash|파일 이름에서 이름과 확장자 분리하기]]<code sh>   * [[http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash|파일 이름에서 이름과 확장자 분리하기]]<code sh>
줄 105: 줄 220:
 read -p 'press enter' varname # prompt read -p 'press enter' varname # prompt
 </code> </code>
 +
 +===== shell script 파라미터 갯수 검사 =====
 +[[http://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script|Check existence of input argument in a Bash shell script]]
 +<code sh>
 +if [ $# -eq 0 ]
 +  then
 +    echo "No arguments supplied"
 +    exit
 +fi
 +
 +# 혹은 특정 파라미터가 "" empty 인지 검사
 +if [ -z "$1" ]
 +  then
 +    echo "No argument supplied"
 +    exit
 +fi
 +</code>
 +
 +===== environment variable empty 검사 혹은 기본값 지정 =====
 +  * 따옴표로 감싸야 globbing 과 문자열 분할을 막을 수 있다.
 +  * [[https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash/28085062#28085062|Assigning default values to shell variables with a single command in bash - Stack Overflow]]
 +<code sh>
 +# if 문
 +if [ -z "${VARIABLE}" ]; then 
 +    FOO='default'
 +else 
 +    FOO=${VARIABLE}
 +fi
 +
 +# VARIABLE 환경변수가 존재하지 않으면 empty 이면 FOO를 default 로 설정
 +FOO="${VARIABLE:-default}
 +
 +# VARIABLE 환경변수가 존재하지 않거나 empty 이면 FOO와 VARIABLE을 모두 default 로 설정
 +FOO="${VARIABLE:=default}"
 +
 +# 첫번째 명령행 인자에 대해 존재하지 않으면 DEFAULTVALUE 환경변수의 값으로 지정하기
 +FOO="${1:-$DEFAULTVALUE}"
 +
 +# chainig도 된다.
 +DOCKER_LABEL=${GIT_TAG:-${GIT_COMMIT_AND_DATE:-latest}}
 +</code>
 +
 +
 +===== test =====
 +  * [[https://storycompiler.tistory.com/107|[Ubuntu/Linux] 쉘스크립트 test 명령문의 모든 것]]
 +
 +===== Editor로 명령 편집하기 =====
 +  * ''Ctrl-X Ctrl-E''를 누르면 ''$EDITOR'' 혹은 ''$VISUAL''에 지정된 에디터로 명령행을 편집할 수 있다.
 +  * ''set -o vi'' 모드일 때는 ''~/.inputrc''에 아래 내용을 넣고 ''Ctrl-X Ctrl-E''를 누른다<code sh>
 +bind -m vi-insert '"\C-x\C-e": edit-and-execute-command'
 +</code>
 +  * 위 명령 실행후 ''b'' 등 몇몇 글자가 안쳐지는 현상이 있었음.
 +
 +===== echo 로 환경변수 내용을 파일로 저장하기 =====
 +  * ''echo'' 명령으로 환경변수 내용을 파일로 저장할 때 환경변수를 따옴표로 감싸지 않으면 환경변수 내의 새줄 기호나 ''echo'' 관련 플래그 등이 해석이 돼 버린다.
 +  * 환경변수 내용을 그대로 파일로 만들고 싶다면 환경변수를 따옴표로 감싸야한다.
 +
 +<code sh>
 +echo "${MY_ENV}" > myfile.txt
 +</code>
 +
 +
 +===== Safe Shell Script =====
 +  * [[https://sipb.mit.edu/doc/safe-shell/|Writing Safe Shell Scripts]]
 +  * [[https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/|Safer bash scripts with 'set -euxo pipefail' · vaneyckt.io]]
 +  * [[https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html|The Set Builtin (Bash Reference Manual)]]
 +
 +<code sh>
 +set -eux -o pipefail
 +shopt -s failglob
 +</code>
 +
 +==== set -e ====
 +  * ''set -e'' : 오류 발생시 즉시 종료. 이것보다는 ''trap'' 사용을 권장함. [[http://mywiki.wooledge.org/BashFAQ/105|BashFAQ/105 -Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?]]
 +  * ''set +e'' : 기본값. 오류 발생해도 무시하고 스크립트 진행.
 +
 +==== set -u ====
 +  * unset variable 이 있으면 즉시 종료
 +
 +==== set -f ====
 +  * 파일명 확장(globbing, ''*'', ''?'' 사용)을 금지시킨다.
 +  * globbing 을 사용한다면 ''shopt -s failglob'' 이게 낫다. ''shopt -s failglob''는 globbing 에 일치하는 파일명들이 존재하지 않으면 오류 발생.
 +
 +==== set -o pipefail ====
 +  * pipeline 의 일부 명령 실패시 (''xx | yy'') 실패시에 해당 줄 전체를 실패로 만든다. 이는 ''set -e''와 합쳐져서 스크립트 전체를 중단하게 한다.
 +
 +==== set -x ====
 +  * 매 명령 실행 직전에 명령 자체를 출력한다.
 +
 +==== set +H ====
 +  * ''!'' 문자의 history 치환 비활성화
 +===== shopt =====
 +  * [[https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html|The Shopt Builtin (Bash Reference Manual)]]
 +
 +==== shopt extglob ====
 +  * globbing 시에 추가할 파일이 아닌 제외할 파일을 지정할 수 있다.
 +  * [[https://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu|bash - How can I use inverse or negative wildcards when pattern matching in a unix/linux shell? - Stack Overflow]]
 +  * 제외할 glob 을 ''!(<glob>)'' 으로 지정한다.
 +<code sh>
 +shopt -s extglob
 +# *Music* 을 제외하고 복사
 +cp !(*Music*) /target_directory
 +
 +# extglob 끄기
 +shopt -u extglob
 +</code>
 +
 +===== Special parameters =====
 +  * [[https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html|Special Parameters (Bash Reference Manual)]]
 +
 +===== function 목록 / 정의 =====
 +  * [[https://stackoverflow.com/questions/4471364/how-do-i-list-the-functions-defined-in-my-shell|bash - How do I list the functions defined in my shell? - Stack Overflow]]
 +
 +<code sh>
 +# 함수 목록 보기
 +declare -F
 +declare -f # 정의 포함 전체 목록
 +# 함수의 정의 보기
 +declare -f <function이름>
 +# or
 +type <function이름>
 +</code>
 +
 +===== dot(.) =====
 +  * 현재 디렉토리를 가리키는 ''./xx/yy''와 혼란을 줌.
 +  * [[https://www.shell-tips.com/bash/source-dot-command/|How and When to Use the Dot Command in Bash?]]
 +  * ''. 다른파일'' : 은 ''source'' 명령과 같다.
 +
 +===== bash-it =====
 +  * [[https://github.com/Bash-it/bash-it|bash-it]] bash framework. 다양한 bash 확장
 +  * [[https://github.com/ohmybash/oh-my-bash|oh-my-bash]] bash 확장. bash-it 이 더 인기 좋음. 성능저하가 있었음(2018)
 +
 +===== background job =====
 +  * ''명령어 &'' : background로 실행.
 +  * ''(명령어 &)'' : background로 실행하되 관련 실행 정보가 화면에 뿌려지는 것을 막음(예: Done, PID등의 정보)
  
 ===== 참조 ===== ===== 참조 =====
줄 119: 줄 369:
   * [[https://www.gitbook.com/book/mug896/shell-script/details|Bash Shell Script]] 한국어   * [[https://www.gitbook.com/book/mug896/shell-script/details|Bash Shell Script]] 한국어
   * [[http://coffeenix.net/doc/shell/introbashscript.htm|Bash Script intro]]   * [[http://coffeenix.net/doc/shell/introbashscript.htm|Bash Script intro]]
 +  * [[https://zwischenzugs.com/2018/01/06/ten-things-i-wish-id-known-about-bash/|Ten Things I Wish I’d Known About bash – zwischenzugs]]
 +  * [[https://devhints.io/bash.html|Bash scripting cheatsheet]]
 +  * [[https://github.com/Idnan/bash-guide|GitHub - Idnan/bash-guide: A guide to learn bash]]
 +  * [[http://mywiki.wooledge.org/BashFAQ#BashFAQ.2F035.How_can_I_handle_command-line_options_and_arguments_in_my_script_easily.3F|BashFAQ - Greg's Wiki]]
 +  * [[https://medium.com/better-programming/the-essential-bash-cheat-sheet-e1c3df06560|The Essential Bash Cheat Sheet | Better Programming]]
 +  * [[https://github.com/dylanaraps/pure-bash-bible|dylanaraps/pure-bash-bible: 📖 A collection of pure bash alternatives to external processes.]]
 +  * [[https://www.ubuntupit.com/how-to-use-the-linux-export-command-in-everyday-computing/|How to Use the Linux Export Command in Everyday Computing]]
 +  * [[https://www.shell-tips.com/bash/math-arithmetic-calculation/|Math Arithmetic: How To Do Calculation in Bash?]]
 +  * [[https://www.cyberciti.biz/tips/linux-unix-pause-command.html|Bash add pause in shell script with bash pause command - nixCraft]]
linux/bash.1459143696.txt.gz · 마지막으로 수정됨: 2016/03/28 14:11 저자 kwon37xi