문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
linux:bash [2017/01/03 13:13] kwon37xi [16진수/10진수(hex/dec)간 변환] |
linux:bash [2022/07/07 17:46] kwon37xi [환경변수에 저장된 환경변수 이름으로 값 얻기] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Bash ====== | ====== Bash ====== | ||
+ | * https:// | ||
+ | |||
+ | ===== shell template ===== | ||
+ | * [[https:// | ||
+ | |||
===== Redirect ===== | ===== Redirect ===== | ||
* http:// | * http:// | ||
+ | * [[https:// | ||
* '' | * '' | ||
* '' | * '' | ||
줄 11: | 줄 17: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== $- ===== | ||
+ | * 현재 shell 의 설정값 | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | ==== 직전 명령의 argument 대체 ==== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
===== 실행 결과 변수에 저장 ===== | ===== 실행 결과 변수에 저장 ===== | ||
<code sh> | <code sh> | ||
줄 20: | 줄 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> | ||
줄 65: | 줄 113: | ||
#결과 | #결과 | ||
00099 | 00099 | ||
+ | </ | ||
+ | |||
+ | bash 4.x 에서는 | ||
+ | <code sh> | ||
+ | for i in {00..10}; do | ||
+ | echo $i | ||
+ | done | ||
+ | # 결과 | ||
+ | 00 | ||
+ | 01 | ||
+ | 02 | ||
+ | 03 | ||
+ | 04... | ||
+ | </ | ||
+ | |||
===== 변수에 값 입력 받기 ===== | ===== 변수에 값 입력 받기 ===== | ||
<code sh> | <code sh> | ||
줄 78: | 줄 141: | ||
===== VI 모드/ Emacs 모드 ===== | ===== VI 모드/ Emacs 모드 ===== | ||
* VI 에디터 편집 모드로 명령행을 변경한다. | * VI 에디터 편집 모드로 명령행을 변경한다. | ||
- | * [[http:// | + | * [[http:// |
+ | * [[https:// | ||
+ | <code sh> | ||
set -o vi | set -o vi | ||
</ | </ | ||
줄 103: | 줄 168: | ||
* 명령모드 '' | * 명령모드 '' | ||
* 명령모드 ''< | * 명령모드 ''< | ||
+ | |||
+ | ===== Shortcuts ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | |||
===== History에 시간 남기기 ===== | ===== History에 시간 남기기 ===== | ||
[[http:// | [[http:// | ||
줄 122: | 줄 194: | ||
basename "/ | basename "/ | ||
</ | </ | ||
+ | * [[linux: | ||
===== Filename name / extension 분리 ===== | ===== Filename name / extension 분리 ===== | ||
* [[http:// | * [[http:// | ||
줄 136: | 줄 208: | ||
read -p 'press enter' varname # prompt | read -p 'press enter' varname # prompt | ||
</ | </ | ||
+ | |||
+ | ===== shell script 파라미터 갯수 검사 ===== | ||
+ | [[http:// | ||
+ | <code sh> | ||
+ | if [ $# -eq 0 ] | ||
+ | then | ||
+ | echo "No arguments supplied" | ||
+ | exit | ||
+ | fi | ||
+ | |||
+ | # 혹은 특정 파라미터가 "" | ||
+ | if [ -z " | ||
+ | then | ||
+ | echo "No argument supplied" | ||
+ | exit | ||
+ | fi | ||
+ | </ | ||
+ | |||
+ | ===== test ===== | ||
+ | * [[https:// | ||
+ | |||
+ | ===== Editor로 명령 편집하기 ===== | ||
+ | * '' | ||
+ | * '' | ||
+ | bind -m vi-insert '" | ||
+ | </ | ||
+ | * 위 명령 실행후 '' | ||
+ | |||
+ | ===== echo 로 환경변수 내용을 파일로 저장하기 ===== | ||
+ | * '' | ||
+ | * 환경변수 내용을 그대로 파일로 만들고 싶다면 환경변수를 따옴표로 감싸야한다. | ||
+ | |||
+ | <code sh> | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 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 ===== | ||
+ | * '' | ||
+ | * '' | ||
===== 참조 ===== | ===== 참조 ===== | ||
줄 150: | 줄 332: | ||
* [[https:// | * [[https:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// |