문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 마지막 판 양쪽 다음 판 | ||
linux:bash [2013/10/27 15:13] kwon37xi [VI 모드/ Emacs 모드] |
linux:bash [2024/01/26 15:14] kwon37xi [File Path dir/filename 분리] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Bash ====== | ====== Bash ====== | ||
- | * http://tldp.org/ | + | * https://www.gnu.org/software/bash/manual/bash.html |
- | * [[http:// | + | |
- | * [[http:// | + | |
- | * [[http:// | + | |
- | * [[http:// | + | |
- | * [[http:// | + | |
+ | ===== shell template ===== | ||
+ | * [[https:// | ||
===== Redirect ===== | ===== Redirect ===== | ||
* http:// | * http:// | ||
+ | * [[https:// | ||
* '' | * '' | ||
* '' | * '' | ||
줄 19: | 줄 17: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
- | ===== ls 컬러 변경 | + | ===== $- ===== |
- | * [[http://www.geekgumbo.com/2011/11/04/changing-the-directory-color-in-the-bash-shell/ | + | * 현재 shell 의 설정값 |
- | export LS_COLORS='di=0;35' | + | * [[https://stackoverflow.com/questions/42757236/what-does-mean-in-bash|shell |
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ==== 직전 명령의 argument 대체 ==== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | ===== 실행 결과 변수에 저장 ===== | ||
+ | <code sh> | ||
+ | # $(명령어) 문법 | ||
+ | test=$(basename " | ||
+ | |||
+ | # backquote `명령어` 문법 | ||
+ | test=`basename " | ||
+ | </ | ||
+ | |||
+ | ===== 환경변수에 저장된 환경변수 이름으로 값 얻기 ===== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | <code sh> | ||
+ | foo=bar | ||
+ | test=foo | ||
+ | echo ${!test} | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 계산 하기 ===== | ||
+ | * '' | ||
+ | * [[https:// | ||
+ | |||
+ | <code sh> | ||
+ | $ echo "2 + 3 = $((2+3))" | ||
+ | 2 + 3 = 5 | ||
</ | </ | ||
- | * [[http:// | ||
- | * ''/ | ||
- | * '' | ||
===== 16진수/ | ===== 16진수/ | ||
줄 42: | 줄 82: | ||
</ | </ | ||
[[http:// | [[http:// | ||
+ | |||
+ | ===== for loop 숫자 ===== | ||
+ | [[http:// | ||
+ | <code sh> | ||
+ | # -f " | ||
+ | # -w : 제일큰 숫자를 기준으로 알아서 0 채워주기 | ||
+ | for i in $(seq -f " | ||
+ | do | ||
+ | echo $i | ||
+ | done | ||
+ | |||
+ | # 결과 | ||
+ | 00010 | ||
+ | 00011 | ||
+ | 00012 | ||
+ | 00013 | ||
+ | 00014 | ||
+ | 00015 | ||
+ | </ | ||
+ | |||
+ | <code sh> | ||
+ | # printf 사용 | ||
+ | i=99 | ||
+ | printf " | ||
+ | # 결과 | ||
+ | 00099 | ||
+ | |||
+ | # 결과를 변수에 저장하기 -v | ||
+ | i=99 | ||
+ | printf -v j " | ||
+ | echo $j | ||
+ | #결과 | ||
+ | 00099 | ||
+ | </ | ||
+ | |||
+ | bash 4.x 에서는 | ||
+ | <code sh> | ||
+ | for i in {00..10}; do | ||
+ | echo $i | ||
+ | done | ||
+ | # 결과 | ||
+ | 00 | ||
+ | 01 | ||
+ | 02 | ||
+ | 03 | ||
+ | 04... | ||
+ | </ | ||
+ | |||
===== 변수에 값 입력 받기 ===== | ===== 변수에 값 입력 받기 ===== | ||
<code sh> | <code sh> | ||
줄 55: | 줄 143: | ||
===== VI 모드/ Emacs 모드 ===== | ===== VI 모드/ Emacs 모드 ===== | ||
* VI 에디터 편집 모드로 명령행을 변경한다. | * VI 에디터 편집 모드로 명령행을 변경한다. | ||
- | * [[http:// | + | * [[http:// |
+ | * [[https:// | ||
+ | <code sh> | ||
set -o vi | set -o vi | ||
</ | </ | ||
줄 67: | 줄 157: | ||
==== VI 모드 명령 ==== | ==== VI 모드 명령 ==== | ||
* [[http:// | * [[http:// | ||
- | * '' | + | * '' |
- | * '' | + | * 명령모드 |
- | * ''#'' | + | * 명령모드 |
- | | + | |
+ | * | ||
+ | * 편집모드 ''< | ||
+ | * 편집모드 ''< | ||
+ | * 편집모드 ''< | ||
+ | * 편집모드 ''< | ||
+ | * '' | ||
+ | * 편집모드 '' | ||
+ | * 명령모드 '' | ||
+ | * 명령모드 ''< | ||
+ | |||
+ | ===== Shortcuts ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * '' | ||
===== History에 시간 남기기 ===== | ===== History에 시간 남기기 ===== | ||
줄 78: | 줄 183: | ||
</ | </ | ||
+ | ===== 사용자 입력 ===== | ||
+ | * [[http:// | ||
+ | * '' | ||
+ | echo -n "Enter your name and press [ENTER]: " | ||
+ | read name | ||
+ | </ | ||
+ | * '' | ||
+ | |||
+ | ===== File Path dir/ | ||
+ | <code sh> | ||
+ | dirname "/ | ||
+ | basename "/ | ||
+ | basename "/ | ||
+ | </ | ||
+ | * [[linux: | ||
+ | ===== Filename name / extension 분리 ===== | ||
+ | * [[http:// | ||
+ | filename=$(basename " | ||
+ | extension=" | ||
+ | filename=" | ||
+ | </ | ||
+ | |||
+ | ===== User Input 사용자 입력 ===== | ||
+ | * http:// | ||
+ | * '' | ||
+ | read varname # varname 으로 입력값 저장 | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | ===== environment variable empty 검사 혹은 기본값 지정 ===== | ||
+ | * 따옴표로 감싸야 globbing 과 문자열 분할을 막을 수 있다. | ||
+ | * [[https:// | ||
+ | <code sh> | ||
+ | # if 문 | ||
+ | if [ -z " | ||
+ | FOO=' | ||
+ | else | ||
+ | FOO=${VARIABLE} | ||
+ | fi | ||
+ | |||
+ | # VARIABLE 환경변수가 존재하지 않으면 empty 이면 FOO를 default 로 설정 | ||
+ | FOO=" | ||
+ | |||
+ | # VARIABLE 환경변수가 존재하지 않거나 empty 이면 FOO와 VARIABLE을 모두 default 로 설정 | ||
+ | FOO=" | ||
+ | |||
+ | # 첫번째 명령행 인자에 대해 존재하지 않으면 DEFAULTVALUE 환경변수의 값으로 지정하기 | ||
+ | FOO=" | ||
+ | |||
+ | # chainig도 된다. | ||
+ | DOCKER_LABEL=${GIT_TAG: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 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 ===== | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== 참조 ===== | ||
+ | * http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// |