๐ก Advanced Bash Shell Scripting: Signal Traps โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for Advanced Bash Shell Scripting: Signal Traps.
Summary
bash
# cmd๊ฒฐ๊ณผ๋ฅผ ๋ณ์๋ก ์ฌ์ฉํ์ฌ cmd ์คํ
command $({ command })
ENV
BASH_SOURCE
bash
# ํ์ฌ ์คํฌ๋ฆฝํธ์ ์์น๋ฅผ ์ฐพ๊ธฐ
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ์คํฌ๋ฆฝํธ ํ์ผ์ ์ด๋ฆ
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
# ์คํฌ๋ฆฝํธ ํ์ผ์ ์ ์ฒด ๊ฒฝ๋ก
SCRIPT_PATH="$SCRIPT_DIR/$SCRIPT_NAME"
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
echo "ํ์ฌ ์คํฌ๋ฆฝํธ์ ๋๋ ํ ๋ฆฌ: $SCRIPT_DIR"
echo "ํ์ฌ ์คํฌ๋ฆฝํธ์ ์ด๋ฆ: $SCRIPT_NAME"
echo "ํ์ฌ ์คํฌ๋ฆฝํธ์ ์ ์ฒด ๊ฒฝ๋ก: $SCRIPT_PATH"
Bash Command
bash
# ๋น ํ์ผ ์์ฑ
>file
# ์ถ๋ ฅ์ ํ์ผ์ ์
๋ ฅ ํ ์ ์ฅ
{ print } > file
# subcommand ๊ฒฐ๊ณผ ์ถ๋ ฅ์ command์ ํ์ผ ์
๋ ฅ์ผ๋ก ์ฌ์ฉ
#ex) cat <({ subcommand }) #์ถ๋ ฅ๋จ
#ex) echo <({ subcommand }) #์์ํ์ผ ๊ฒฝ๋ก ์ถ๋ ฅ
command <({ subcommand })
# command์ ํ์ผ ์
๋ ฅ
command < { file }
# command์ ์ถ๋ ฅ์ ์
๋ ฅ
{ print } | command
# subcommand ๊ฒฐ๊ณผ ๊ฐ์ command ์ธ์๊ฐ(ํ๋ฌธ)์ผ๋ก ์ฌ์ฉ
#ex) echo `{ subcommand }`
command `{ subcommand }`
# backtick 2์ค์ฒฉ์
command `subcommand \`subcommand2\``
# ํ์ค์ถ๋ ฅ์ ํ์ผ์ ๋ง์ง๋ง์ค์ ์ถ๊ฐ
echo 'Text' >> file
cat sourcefile >> file
# ํ
์คํธ๋ฅผ ํ์ผ์ ๋ง์ง๋ง์ค์ ์ถ๊ฐํ๊ณ ์ถ๋ ฅ
cat << EOF >> file
TEXTS
TESTS
EOF
# daemon ์คํ์ cli ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์คํ
dockerd $
# ๋ฐฑ๊ทธ๋ผ์ด๋ ๊ด๋ จ ๋ณด๊ธฐ ๋ณ๊ฒฝ
bg
fg
# ๋ฐฐ์ด์ ๊ธธ์ด ์ถ๋ ฅ
export ARRAY=(A B C D)
echo ${#ARRAY[@]} #4
Expression
bash
'\n' #์ค๋ฐ๊ฟ
Shell implementation
getops
References and Guidelines (NOTE)
When using getopt $string, the string becomes a flag that requires a value if : is connected after 1 char
ex) a\:bcd\:
-a 1 -b -c -d 2
Features
Redirect operation
| Purpose | Command |
| output stdout to stderr |
command 1>&2
|
| Output stderr to stdout |
command 2>&1
|
| Save both stdout and stderr to files |
command > out.log 2>&1
|
| Save only stderr to file |
command 2> error.log
|
| Save only stdout to file |
command > output.log
|
| ignore stderr |
command 2>/dev/null
|
| ignore stdout |
command > /dev/null
|
| ignore both |
command > /dev/null 2>&1
|
Move to execution location of script
bash
SCRIPT_DIR="$(dirname "$0")" && cd $SCRIPT_DIR




Leave a Reply