Advanced Bash Shell Scripting: Signal Traps

๐Ÿ’ก 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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *