Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

psj2867

bash shell script 정리 본문

기타

bash shell script 정리

psj2867 2023. 1. 20. 10:02

#!/bin/bash

#변수

var=1
str="str"
str_var="str$var" # str+var
str_var="str${var}" # str+var

#배열

array=()
array=( a b c)
array[i]
"${array[i]}"
"${array[@]}"
"${array[*]}"
"${#array[*/@]}"
array+=(str)

#특수 변수
$0 command
$1 ~ $9 args
$# args num
$* args string with blank(IFS)
$@ args string with blank & double quote
$? last command return(success=0/fail=1)
$$  curr pid
$! last background pid
!$  last arg of last command

#변수표현 및 변환
$var
${var}
${var:-word} = null or blank / return default
${var:=word} = null or blank / return default & set
${var:?word} = null or blank / err word
${var:+word} = not null or blank / return default
${parameter:offset} = substring
${parameter:offset:length} = substring
${!prefix*}/${!prefix@} = variables startwith prefix
${!name[*]}/${!name[@]} = indices(key) in name array
${!var} = name recursion
${parameter#word}/${parameter##word} = remove matching head(short/long)
${parameter%word}/${parameter%%word} = remove matching tail(short/long)
${parameter/pattern/string} = replace(short/long) 
${parameter//pattern/string} = replace all
${parameter/#pattern/string}/${parameter/$pattern/string} = replace - #=start(^), %=end($)

${parameter^pattern}/${parameter^^pattern} = convert matching case to lower -> upper case / convert all
${parameter,pattern}/${parameter,,pattern} = convert matching case to upper -> lower case / convert all


#이스케이프

\
''(single quote) = 대부분의 문자($,`) 이스케이프 처리, ''(single quote) 제외
""(double quote) = 공백 등 이스케이프 처리, ‘$’, ‘`’, ‘\’, [‘!’] 제외, \[$'"\] 만 이스케이프
$'' = ANSI-C quoting, ANSI C 문자열로 처리, [\n\a\t] 등
$"" = Locale-Specific Translation, gettext 사용

#조합 구문

while test-commands; do consequent-commands; done
until test-commands; do consequent-commands; done

for name in [words …] ; do commands; done
for name [;] do commands; done # default $@
for (( expr1 ; expr2 ; expr3 )) ; do commands ; done # i=0;i<n;i++

if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi

case word in
    [ [(] pattern [| pattern]…) command-list ;;]…
    [()] pattern) command-list ;;    
    ( pattern | pattern ) command-list ;;]…
esac

select name [in words …]; do commands; done # list menu, select

(()) = eval
[[]] = condition


( list ) = subshell
{ list; } = curr shell

fname () compound-command [ redirections ]
function fname [()] compound-command [ redirections ]
fname() {}


# 패턴

*/?/[]
[XYZ]/[x-Z]/[[:class:]]/[^XYZ]/[!XYZ]

[[:alnum:]] [A-Za-z0-9] Alphanumeric characters
[[:alpha:]] [A-Za-z] Alphabetic characters
[[:lower:]] [a-z] Lower-case alphabetic characters
[[:upper:]] [A-Z] Upper-case alphabetic characters
[[:digit:]] [0-9] Numeric characters
[[:xdigit:]] [0-9a-fA-F] Hexadecimal digit characters
[[:space:]] [ \t\n\v\f\r] All whitespace chars (form feed \x0c)
[[:blank:]] [ \t] Space, tab only
[[:punct:]] [!@#$%^&*(){}[]...] 키보드에 있는 숫자, 대,소문자 빼고 모든 문자
[[:graph:]] [!-~] Printable and visible characters (ASCII 테이블에서 ! 부터 ~ 까지)
[[:print:]] [ -~] Printable (non-Control) characters (ASCII 테이블에서 space 부터 ~ 까지)
[[:cntrl:]] [\x00-\x19\x7F] Control characters

# [[ ]] 내부에서

[[ var == ?(p1|p2) ]]

?(<PATTERN-LIST>) 주어진 패턴이 zero or one 발생하면 매칭됩니다.
*(<PATTERN-LIST>) 주어진 패턴이 zero or more 발생하면 매칭됩니다.
+(<PATTERN-LIST>) 주어진 패턴이 one or more 발생하면 매칭됩니다.
@(<PATTERN-LIST>) 주어진 패턴이 one 발생하면 매칭됩니다.
!(<PATTERN-LIST>) ! 문자는 not 의 의미로 주어진 패턴과 일치하지 않으면 매칭됩니다. ( 주의: empty 일 경우도 매칭에 포함됩니다. )

# 토큰해석 시 - 따음표 없이

a{a,b,c}d = aad abd acd
~+  pwd
~   home

'기타' 카테고리의 다른 글

hadoop mapreduce 정리  (0) 2023.02.16
elastic search 정리  (0) 2023.01.27
c언어의 main 은 함수여야 할까  (0) 2022.11.24
nginx - auth_basic 간단한 구조 설명  (0) 2022.10.18
언어별 GC 비교  (0) 2022.10.07
Comments