문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
linux:bash [2020/08/27 22:07] kwon37xi [set -e/+e] |
linux:bash [2023/05/25 16:14] kwon37xi [for loop 숫자] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Bash ====== | ====== Bash ====== | ||
- | ===== Oh My Bash ===== | + | * https:// |
- | * [[https://github.com/ohmybash/oh-my-bash|oh-my-bash]] bash 확장. 2018현재 성능 저하가 심했음. | + | |
- | * [[https:// | + | ===== shell template |
+ | * [[https://betterdev.blog/minimal-safe-bash-script-template/|Minimal safe Bash script template | ||
===== Redirect ===== | ===== Redirect ===== | ||
* http:// | * http:// | ||
+ | * [[https:// | ||
* '' | * '' | ||
* '' | * '' | ||
줄 15: | 줄 17: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== $- ===== | ||
+ | * 현재 shell 의 설정값 | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
==== 직전 명령의 argument 대체 ==== | ==== 직전 명령의 argument 대체 ==== | ||
줄 26: | 줄 38: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
+ | * '' | ||
===== 실행 결과 변수에 저장 ===== | ===== 실행 결과 변수에 저장 ===== | ||
<code sh> | <code sh> | ||
줄 34: | 줄 48: | ||
test=`basename " | test=`basename " | ||
</ | </ | ||
+ | |||
+ | ===== 환경변수에 저장된 환경변수 이름으로 값 얻기 ===== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | <code sh> | ||
+ | foo=bar | ||
+ | test=foo | ||
+ | echo ${!test} | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 계산 하기 ===== | ||
+ | * '' | ||
+ | * [[https:// | ||
+ | |||
+ | <code sh> | ||
+ | $ echo "2 + 3 = $((2+3))" | ||
+ | 2 + 3 = 5 | ||
+ | </ | ||
+ | |||
===== 16진수/ | ===== 16진수/ | ||
<code sh> | <code sh> | ||
줄 52: | 줄 86: | ||
[[http:// | [[http:// | ||
<code sh> | <code sh> | ||
+ | # -f " | ||
+ | # -w : 제일큰 숫자를 기준으로 알아서 0 채워주기 | ||
for i in $(seq -f " | for i in $(seq -f " | ||
do | do | ||
줄 107: | 줄 143: | ||
===== VI 모드/ Emacs 모드 ===== | ===== VI 모드/ Emacs 모드 ===== | ||
* VI 에디터 편집 모드로 명령행을 변경한다. | * VI 에디터 편집 모드로 명령행을 변경한다. | ||
- | * [[http:// | + | * [[http:// |
+ | * [[https:// | ||
+ | <code sh> | ||
set -o vi | set -o vi | ||
</ | </ | ||
줄 132: | 줄 170: | ||
* 명령모드 '' | * 명령모드 '' | ||
* 명령모드 ''< | * 명령모드 ''< | ||
+ | |||
+ | ===== Shortcuts ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | |||
===== History에 시간 남기기 ===== | ===== History에 시간 남기기 ===== | ||
[[http:// | [[http:// | ||
줄 151: | 줄 196: | ||
basename "/ | basename "/ | ||
</ | </ | ||
+ | * [[linux: | ||
===== Filename name / extension 분리 ===== | ===== Filename name / extension 분리 ===== | ||
* [[http:// | * [[http:// | ||
줄 182: | 줄 227: | ||
fi | fi | ||
</ | </ | ||
+ | |||
+ | ===== test ===== | ||
+ | * [[https:// | ||
===== Editor로 명령 편집하기 ===== | ===== Editor로 명령 편집하기 ===== | ||
줄 198: | 줄 246: | ||
</ | </ | ||
- | ===== set -e/+e ===== | + | |
+ | ===== Safe Shell Script ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | <code sh> | ||
+ | set -eux -o pipefail | ||
+ | shopt -s failglob | ||
+ | </ | ||
+ | |||
+ | ==== set -e ==== | ||
* '' | * '' | ||
* '' | * '' | ||
+ | |||
+ | ==== set -u ==== | ||
+ | * unset variable 이 있으면 즉시 종료 | ||
+ | |||
+ | ==== set -f ==== | ||
+ | * 파일명 확장(globbing, | ||
+ | * globbing 을 사용한다면 '' | ||
+ | |||
+ | ==== set -o pipefail ==== | ||
+ | * pipeline 의 일부 명령 실패시 ('' | ||
+ | |||
+ | ==== set -x ==== | ||
+ | * 매 명령 실행 직전에 명령 자체를 출력한다. | ||
+ | |||
+ | ==== set +H ==== | ||
+ | * '' | ||
+ | ===== shopt ===== | ||
+ | * [[https:// | ||
+ | |||
+ | ==== shopt extglob ==== | ||
+ | * globbing 시에 추가할 파일이 아닌 제외할 파일을 지정할 수 있다. | ||
+ | * [[https:// | ||
+ | * 제외할 glob 을 '' | ||
+ | <code sh> | ||
+ | shopt -s extglob | ||
+ | # *Music* 을 제외하고 복사 | ||
+ | cp !(*Music*) / | ||
+ | |||
+ | # extglob 끄기 | ||
+ | shopt -u extglob | ||
+ | </ | ||
+ | |||
+ | ===== Special parameters ===== | ||
+ | * [[https:// | ||
+ | |||
+ | ===== function 목록 / 정의 ===== | ||
+ | * [[https:// | ||
+ | |||
+ | <code sh> | ||
+ | # 함수 목록 보기 | ||
+ | declare -F | ||
+ | declare -f # 정의 포함 전체 목록 | ||
+ | # 함수의 정의 보기 | ||
+ | declare -f < | ||
+ | # or | ||
+ | type < | ||
+ | </ | ||
+ | |||
+ | ===== dot(.) ===== | ||
+ | * 현재 디렉토리를 가리키는 '' | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | |||
+ | ===== bash-it ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | ===== background job ===== | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
===== 참조 ===== | ===== 참조 ===== | ||
* http:// | * http:// | ||
줄 218: | 줄 338: | ||
* [[https:// | * [[https:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// |