-
๐ Ch14 Advanced Shell Scripting ๐๐ฎ ํ๊ต ๊ณต๋ถ/๐ ์ ๋์ค ์์คํ 2025. 5. 31. 20:38
Input/output redirection (์ ์ถ๋ ฅ ๋ฆฌ๋ค์ด๋ ์ )
์ธ ๊ฐ์ง ํ์ค ํ์ผ ๋์คํฌ๋ฆฝํฐ
STDIN (0): ๊ธฐ๋ณธ์ ์ผ๋ก ํค๋ณด๋ ์ ๋ ฅ์ ๊ฐ๋ฆฌํค๋ ์คํธ๋ฆผ
STDOUT (1): ํ๋ก๊ทธ๋จ์ ์ผ๋ฐ ์ถ๋ ฅ(๊ฒฐ๊ณผ ๋ฉ์์ง ๋ฑ)์ ๋ชจ๋ํฐ(ํฐ๋ฏธ๋)์ ๋ณด๋ด๋ ์คํธ๋ฆผ
STDERR (2): ์๋ฌ ๋ฉ์์ง ์ ์ฉ ์คํธ๋ฆผ
STDOUT/STDERR์ ๊ตฌ๋ถํ๋ฉด ์ ์ ์ถ๋ ฅ, ์ค๋ฅ ์ถ๋ ฅ์ ๊ฐ๊ฐ ๋ค๋ฅธ ํ์ผ๋ก ๋ฆฌ๋ค์ด๋ ์ ํ๊ฑฐ๋ ํฐ๋ฏธ๋, ํ์ผ์ ๋์ ํ์ฉ ๊ฐ๋ฅ
๋ฆฌ๋ค์ด๋ ์ ์ฐ์ฐ์
>: STDOUT->ํ์ผ (๋ฎ์ด์ฐ๊ธฐ)
>>: STDOUT->ํ์ผ (์ด์ด์ฐ๊ธฐ)
<: ํ์ผ->STDIN
2>: STDERR->ํ์ผ (๋ฎ์ด์ฐ๊ธฐ)
2>>: STDERR->ํ์ผ (์ด์ด์ฐ๊ธฐ)
&>: STDOUT+STDERR->ํ์ผ (๋ฎ์ด์ฐ๊ธฐ, Bash ํ์ฅ)
|: ํ์ดํ
$ cat << AAA The Cat Sat on the Mat . AAA // end string $ find / -name "*.txt" > out.txt 2> err.txt # STDOUT(1)์ out.txt๋ก ๋ฎ์ด์ฐ๊ธฐ, STDERR(2)๋ฅผ err.txt๋ก ๋ฎ์ด์ฐ๊ธฐ $ find / -name "*.txt" > out.txt 2>1& # out.txt๋ก STDOUT์ out.txt์ ๋ฎ์ด์ด ๋ค, STDERR(2)๋ฅผ ํ์ฌ์ STDOUT(1) ์์น(=out.txt)๋ก ๋ณต์ฌ $ find / -name "*.txt" 2> out.txt 1>2& # ๋จผ์ STDERR๋ฅผ ํฐ๋ฏธ๋์ STDOUT(!)์ผ๋ก ๋ณต์ฌํ ๋ค, STDOUT๋ง out.txt๋ก ๋ณด๋ -> STDERR์ ์ฌ์ ํ ํฐ๋ฏธ๋์, STDOUT๋ง ํ์ผ์ ๊ธฐ๋ก๋จ # ๋ง์ง๋ง์ &๋ ์ด ๋ช ๋ น์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ์คํํ๋ผ๋ ๋ป
Command Substitution (๋ช ๋ น ์นํ)
์ ์คํฌ๋ฆฝํธ ๋ด์์ ๋ค๋ฅธ ๋ช ๋ น์ ์ถ๋ ฅ์ ๋ณ์์ ๋ด๊ฑฐ๋, ๊ณง๋ฐ๋ก ๋ค์ ๋ช ๋ น์ ์ธ์๋ก ๋๊ฒจ์ค ๋ ์ฌ์ฉ
- ๋ฐฑํฑ ๋ฐฉ์ (๊ตฌ ๋ฐฉ์)
# ํ์ผ์ ์ค ์๋ฅผ ๊ณ์ฐํ์ฌ ๋ณ์์ ์ ์ฅ Lines=`wc -l textfile` # ์ฌ์น์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ๊ณ์ฐ a=`expr $a + 1`
- $() ๋ฐฉ์ (์ ๋ฐฉ์)
Lines=$(wc -l textFile) a=$(expr $a + 1)
Shell ๋ณ์
์ ธ ์คํฌ๋ฆฝํธ๋ ์ธํฐ๋ํฐ๋ธ ์ ธ ์ธ์ ์์ ๊ฐ ์ ์ฅํ๊ณ ์ฐธ์กฐํ๊ธฐ ์ํด ์ฌ์ฉ
a=5
export a
export b=7
echo $a
set
env
set|grep PATH
Shell Function
name() {commands;}
# 1) ํจ์ ์ ์ repeat() { echo -n "I don't know $1 $2" return 7 } # 2) ํจ์ ํธ์ถ repeat Your Name # 3) ํจ์ ํธ์ถ ์งํ์ ์ข ๋ฃ ์ํ echo $? # 4) ์คํฌ๋ฆฝํธ ์ ์ฒด ์ข ๋ฃ ์ํ๋ฅผ 0์ผ๋ก ์ง์ exit 0 # 5) (์ฌ์ค ์ด ๋ผ์ธ์ ์คํฌ๋ฆฝํธ๊ฐ ์ด๋ฏธ exit ํด์ ์คํ๋์ง ์์) echo $?
-> I don't know your Name
7
$ echo $?
0
scope
scope - local, global
#!/bin/bash # 1) ํจ์ ์ ์ scope() { local lov=1 # โ local ํค์๋๋ก ์ ์ธํ ๋ณ์ → ์ด ํจ์ ๋ด๋ถ์์๋ง ์ ํจ glov=2 # โก ํค์๋ ์์ด ์ ์ธํ ๋ณ์ → ๊ธ๋ก๋ฒ(ํ๊ฒฝ ๋ณ์ ์๋)๋ก ์ทจ๊ธ echo "ํจ์ ๋ด๋ถ: local=$lov, global=$glov" } # 2) ํจ์ ํธ์ถ scope # 3) ํจ์ ํธ์ถ ํ ์ ์ญ ๋ณ์ ํ์ธ echo "ํจ์ ์ธ๋ถ: local=$lov, global=$glov"
local 1 global 2
local global 2
Funcrion libraries
include function script file using dot(.)
../scope.sh // file name is scope.sh scope // function call echo function call test ends.
getopts
a built-in command line parser
๋ช ๋ น์ค ์ต์ ๊ณผ ์ธ์๋ฅผ ํ์ฑํ๊ธฐ ์ํด ๋ด์ฅ๋ ๋๊ตฌ
while getopts "abc:d:e:f:" opt do case $opt in a) echo a specified;; b) echo b specified;; c) echo c specified echo $OPTARG;; d) echo d specified;; e) echo e specified;; f) echo f specified echo $OPTARG;; esac Done
opt: ํ์ฌ ์ฒ๋ฆฌ๋ ์ต์ ๋ฌธ์ ๋ด๋ ๋ณ์
OPTARG: ์ธ์๋ฅผ ํ์๋ก ํ๋ ์ต์ ์ ์ธ์ ๊ฐ์ ๋ด๋ ๋ณ์
OPTIND: getopt๊ฐ ๋ค์์ ์ดํด๋ณผ ์ธ์์ ์ธ๋ฑ์ค๋ฅผ ๋ด๊ณ ์๋ ๋ณ์
$ sh getopts.sh -a -b -c cc -d -e -f ff
$ sh getopts.sh -abc cc -def ff // same
Signals and Traps
trap '<๋ช ๋ น์ด>' <์ ํธ๋ฒํธ> // single quote, ignore ^c(SIGINT)
trap: ์ง์ ํ ์๊ทธ๋์ ์์ ํ์ ๋ <๋ช ๋ น์ด>๋ฅผ ์คํํ๊ฒ ํด์ค/์๊ทธ๋ ๋ฒํธ ๋์ ์ด๋ฆ(SIGINT, SIGTERM ๋ฑ) ์ฌ์ฉ ๊ฐ๋ฅ
trap '' 2 // SIGNIT(Ctrl+C) ์์ ํ ๋ฌด์ while [1 = 1] do read a if [$a = 'a'] then exit else echo you pressed $a fi done
$ ./trap.sh
^c^c^c^cqyou pressed q
File handling
if [-w writefile || -x writefIle ]
if [-r writeFile && -s writeFile ]
-d: directory
-e: file exist
-r: readable
-w: writable
-x: executable
-s: file size is greater than zero
Array{bash}
a[1]=2
a2=(1 3 5 7 9 11)
a3=([0]=1 [7]=2)
b=`expr ${a[3]}+${a[5]}`
ar={a2[@]} // ar=(1 3 5 7 9 11)
alnth=${#a2[@]} // alnth=4
${ar[@]:3} : (7 9 11)
${ar[@]:3:2} : (7 9)
${!ar[@]} : (0 1 2 3 4 5) // ์ธ๋ฑ์ค ๋ชฉ๋ก ์ด๊ธฐ
unset ar[@]; unset a[2] // ๋ฐฐ์ด ์์ฒด ์ ๊ฑฐ
'๐ฎ ํ๊ต ๊ณต๋ถ > ๐ ์ ๋์ค ์์คํ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ Ch18 Backup Tools ๐ (0) 2025.05.31 ๐ Ch16 Unix Networking ๐ (0) 2025.05.31 ๐ Ch13 Basic Shell Scripting ๐ (0) 2025.05.20 ๐ Ch11 Running program at specified times ๐ (0) 2025.04.12 ๐ Ch10 Job Control & Process Management ๐ (0) 2025.04.11