๐ก Bash Scripting Basics: Parameter Expansion โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for Bash Scripting Basics: Parameter Expansion.
References and Guidelines (NOTE)
Reference
url: Bash shell Programming – Jinlib
Example
bash
#!/bin/bash
# My First Script Program
printf "I love UNIX !\n"
pwd
exit 0
Script component
-
! (Magic number)
- Located at the beginning of the script file
- Specify the program to run the script
- Can be used in commands other than the shell. ex) #!/bin/more
- # (comment)
- Shell command
- Can contain commands executable in the shell.
- Shell program โ described below
Shell Program syntax elements
- Shell variables
- Specification method: varname = value
- Variable expression
format function Result simulation $nameReplace with the value of name $(name)Replace with the value of name. Used when a variable name is adjacent to another statement $(name:-word)If name is defined, use that value. Or use word if name==null { null }
if name==undefined { โwordโ }
if hasVal(name) { name }$(name:=word)If name is undefined or null, substitute word and use its value. if name==null { โwordโ }
if name==undefined { โwordโ }
if hasVal(name) { name }$(name:+word)If name is defined and its value is not null, word is used. if name==null { null }
if name==undefined { undefined }
if hasVal(name) { โwordโ }$(name:?word)If name is defined and the value is not null, use that value. Otherwise, print word and end. if name==null
{ print(โwordโ) and exit() }
if name==undefined
{ print(โwordโ) and exit() }
if hasVal(name)
{ name } - Variable string processing โ If the value is a string, an expression that finds a pattern within the string and removes part of it.
format function ${variable%pattern}Find and remove the first part that matches the pattern from the end of the value in the variable ${variable%%pattern}Find and remove the largest part that matches the pattern from the end of the value in the variable ${variable#pattern}Find and remove the first part matching the pattern from the beginning of the value in the variable ${variable##pattern}Find and remove the largest part that matches the pattern from the beginning of the value in the variable - Parameter processing
- Values and parameters given as arguments when running a script
- Positional parameters
command line arguments meaning $0name of shell script $1 - $9The first to ninth arguments given when executing a command. $(10)10th argument $#Total number of arguments $All arguments โ \$1 \$2 \$3 $@Same as \$* โ$โAll arguments โ โ\$1 \$2 \$3โ โ$@โAll arguments โ โ\$1โ โ\$2โ โ\$3โ $?Exit value of the most recently executed command - Executive syntax
- Special character handling
quotation marks function How to use Single quote ( โ โ)Prevents all special characters from being interpreted echo โ$testโ
$testDouble quotes ( โ โ)Only substitution of variables or commands is allowed echo โ$testโ
100Backslash ( \)Prevents single characters from being interpreted echo \$test
$test - Returns the command execution result as a value (string)
| symbol | How to use |
| โ | โ |
| back quarter (` `) | echo `date` |
| \$(command) | echo \$(date) | - Input Stream
- Static input โ input from a terminal or file, single line
- Format
format Use read x Receives one line from standard input and stores it in variable \$x read first last Receives a line from standard input and stores the first word in the variable \$first and all the rest in the variable \$last. read -p prompt Prints a prompt and waits for input. The entered value is stored in variable \$REPLY - Example of use
- Format
bash
#!/bin/bash
# ํค๋ณด๋ ์
๋ ฅ ์ฒ๋ฆฌ๋ฅผ ํ
์คํธ ํ๋ ์คํฌ๋ฆฝํธ
read x # ์๋ฌด ๋ฉ์์ง ์์ด ์ฌ์ฉ์ ์
๋ ฅ์ ๊ธฐ๋ค๋ฆผ
echo "x : $x" # ์ฌ์ฉ์๊ฐ ์์์ ๊ฐ์ ์
๋ ฅํ๋ฉด ์ถ๋ ฅ
read x y # ์ฒซ ๋จ์ด๋ x, ๋๋จธ์ง๋ y์ ์ ์ฅ
echo "x is $x y is $y" # x, y ๊ฐ ์ถ๋ ฅ
read -p โInput : โ # Input : ์ ์ถ๋ ฅํ ํ ์
๋ ฅ ๊ธฐ๋ค๋ฆผ
echo "input : $REPLY" # $REPLY์ ์๋ ์ ์ฅ๋ ์
๋ ฅ๊ฐ ์ถ๋ ฅ
- Dynamic input โ receiving input as a statement, multiple lines
- Format
command << TERMINATOR
Input contents
TERMINATOR
TERMINATOR is a name chosen arbitrarily and EOF (End Of File) is often used. - Example of use
- Format
bash
#!/bin/bash
# ์ฃผ์
<< EOF
AAAAAA
BBBBBB
CCCCCC
EOF
# here ๋ฌธ์ ํ
์คํธ. user1์๊ฒ ๋ฉ์ผ์ ๋ณด๋
mail user1 << END
This is a test mail for here document
END
-
Operator
operator meaning Example usage -negative number -x!Logical negation (NOT) (( ! x < y ))~Bit inversion (NOT) ~y*/%Multiplication, division, remainder let y=3 * 5+-addition, subtraction let x=x+1<<>>Bit left shift, bit right shift (( y = x << 3 ))<=>=<>==!=comparison operation (( x < y ))&^|Bitwise AND, XOR, OR let โz = x ^ yโ&&||Logical AND, OR (( x=Specify variable value let z=1*=/=%=+===<<=>>=&=^=|=shortcut operation let z+=1 -
Control statements
- Conditional Test – String Operator
| string operators | Action |
| — | — |
|str = pattern| str matches pattern. Space on both sides of the = operator |
str == pattern
|str != pattern| str does not match pattern |
|str| str is not null |
|-z str| str length is 0 |
|-n str| str length is not zero |
|-l str| Returns the length of str (not a conditional expression) | -
Condition Test – Flags
test flag action -a filefile exists -e filefile exists -L filesymbolic link file -O fileUser is the owner of the file -G fileGroup ID in file is same as group ID in script -S filesocket file -r filereadable -w filewritable -x fileexecutable -b fileblock device special file -c filecharacter device special file -d filedirectory file -p filepipe file -u filesetuid authorization file -g filesetgid authorization file -k filesticky bit access permission file -s fileSize of file is not zero -
conditional statement – if
bash
#!/bin/bash # test_elif: if-elif ๋ฌธ ํ ์คํธ echo โInput Score : โ read score if (( $score >= 90 )); then echo โYour score is great. โ elif (( $score >= 80 )); then echo โYour score is good. โ else echo โYour score is not good. โ fi
-
conditional statement – if
-
Conditional statement – case
bash
#!/bin/bash
# case ํ
์คํธ ์คํฌ๋ฆฝํธ
: <<EOF
์ฃผ์ด์ง ๋ณ์์ ๊ฐ์ ๋ฐ๋ผ ์คํํ ๋ช
๋ น ๋ฐ๋ก ์ง์
๋ณ์์ ๊ฐ์ด value1 ์ด๋ฉด value1๋ถํฐ ;;์ ๋ง๋ ๋๊น์ง ๋ช
๋ น ์คํ
๊ฐ์ ์ง์ ์ ํน์๊ธฐํธ, | (or ์ฐ์ฐ์) ์ฌ์ฉ ๊ฐ๋ฅ
์ผ์นํ๋ ๊ฐ์ด ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ธ * ๋ถํฐ ์คํ
EOF
echo "Select command :"
read cmd
case $cmd in
[0-9]) # 0๋ถํฐ 9๊น์ง ์์์ ์ซ์
date
;;
"cd" | "CD") # cd ๋๋ CD
echo $HOME
;;
[aA-C]*) # ์๋ฌธ์ a, ๋๋ฌธ์ A,B,C๋ก ์์ํ๋ ์์์ ๋ฌธ์์ด
pwd
;;
*)
echo" Usage : select command"
;;
esac
- Repeat statement – for
bash
#!/bin/bash
# test_for: for ํ
์คํธ ์คํฌ๋ฆฝํธ
: <<EOF
list ๋ด ๋ชจ๋ ์๋ ๋จผํธ๋ฅผ ๋ณ์๋ก ๋ฐํํ๋ฉฐ ๋ฐ๋ณต (forEach)
list ๋์ $(<file)์ ์ฌ์ฉํ๋ฉด ์ธ๋ถ ํ์ผ์ ๋ด์ฉ์ ์
๋ ฅ์ผ๋ก ๋ฐ์์ ์ฒ๋ฆฌํ๋ค.
EOF
for num in 0 1 2
do
echo "Number is $num"
done
- Repeat statement – while
bash
#!/bin/bash
# test_while: while์ ์ด์ฉํด 1๋ถํฐ 10๊น์ง ํฉ์ ๊ตฌํ๋ ์คํฌ๋ฆฝํธ
: <<EOF
์กฐ๊ฑด์ด true์ธ ๋์ ๋ช
๋ น ๋ฐ๋ณต
EOF
count=1
sum=0
while (( count <= 10 ))
do
(( sum += count ))
let count+=1
done
echo "Sum(1~10) : $sum"
- Repeat statement – until
bash
#!/bin/bash
# test_until: ์ง์ ํ ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธํ๋ฉด ์๋ฆฌ๋ ์คํฌ๋ฆฝํธ
: <<EOF
#์กฐ๊ฑด์ด true๊ฐ ๋ ๋๊น์ง ๋ช
๋ น ๋ฐ๋ณต
EOF
echo "Input name :" # ์
๋ ฅ ์๋ด๋ฌธ ์ถ๋ ฅ
read person # ์ ์ ์ด๋ฆ์ person์ ์ ์ฅ
until who | grep $person # > /dev/null
do
sleep 5 # ์ ์ ๊ฐ ์ ์ ์ค์ด ์๋๋ฉด 5์ด ์ผ
done
- Repeat statement – select
bash
#!/bin/bash
# test_select: ์ฌ์ฉ์ ์
๋ ฅ์ ๋ฐ๋ผ pwd,date ๋ช
๋ น์คํ
: <<EOF
๋ฉ๋ด๋ฅผ ์์ฑํ ์ ์๋ ๋ฐ๋ณต ์คํ๋ฌธ์ด๋ค.
list์ ์ง์ ํ ํญ๋ชฉ์ ์๋์ผ๋ก ์ ํ ๊ฐ๋ฅํ ๋ฉ๋ด๋ก ๋ง๋ค์ด ํ๋ฉด์ ์ถ๋ ฅํด์ค๋ค.
์ฌ์ฉ์๋ ๊ฐ ํญ๋ชฉ์ ์๋์ผ๋ก ๋ถ์ฌ๋ ๋ฒํธ๋ฅผ ์ ํํ๋ค.
์ฌ์ฉ์ ์
๋ ฅ์ select์ in ์ฌ์ด์ ์ง์ ๋ ๋ณ์์ ์ ์ฅ
๋ณดํต case ๋ฌธ๊ณผ ๊ฒฐํฉํ์ฌ ์
๋ ฅ ๊ฐ์ ์ฒ๋ฆฌํ๋ค.
EOF
PS3="Input command(1~3) "
select cmd in pwd date quit
do
case $cmd in
pwd)
pwd ;;
date)
date;;
quit)
break;;
*)
echo "Invalid input. select number"
;;
esac
REPLY=
done
- Repeat control statement – continue
bash
#!/bin/bash
# test_cont: continue ํ
์คํธ
: << EOF
๋ฃจํ ์์์ ์ฌ์ฉํ๋ค.
์ดํ ์คํ ์์๋ฅผ ๋ฌด์ํ๊ณ ๋ฃจํ์ ์ฒ์์ผ๋ก ๋์๊ฐ๋ ๋ช
๋ น
์ซ์๋ฅผ ์ง์ ํ๋ฉด ์ค์ฒฉ๋ ๋ฃจํ ์์์ ํน์ ๋ฃจํ์ ์ฒ์์ผ๋ก ๋์๊ฐ ์ ์๋ค.
EOF
for person in $(< list) # `cat list`์ ๋์ผ
do
if [[ $person == user2 ]] then
continue # user2 ์ด๋ฉด ๊ฑด๋๋
fi
echo โHello, $personโ
done
- Function
- Declaration
bash
# typeset -f ๋ช
๋ น์ด๋ฅผ ํตํด ์์คํ
์ ๋ฑ๋ก๋ ํจ์๋ฅผ ํ์ธ ํ ์ ์๋ค.
function {{ ํจ์๋ช
}}
{
...
}
- End function – return
bash
#!/bin/bash
# ํจ์ ๋ฆฌํด๊ฐ ํ
์คํธ
function sum {
typeset sum
(( sum= $1 + $2 ))
return $sum
}
sum $1 $2
echo "$1 + $2 = $?"
# ํจ์ ๋ณธ๋ฌธ ์์ ๋ง์ง๋ง ๋ฌธ์ฅ ์คํ
# return ๋ฌธ ์คํ
# +) ์ง์ ํ ๊ฐ์ด ํจ์์ ์ข
๋ฃ๊ฐ์ผ๋ก $?์ ์ ์ฅ๋๋ค.
# ๊ฒฐ๊ณผ :
# bash {{ filename }} 3 4
# 3 + 4 = 7
- Delete function – unset
bash
unset -f { ํจ์๋ช
}
- Debugging
-
bash -x
bash
# ๊ฐ์ฅ ๊ฐ๋จํ ์คํฌ๋ฆฝํธ ์คํ ์ค๋ฅ ์์ ๋ฐฉ๋ฒ์ด๋ค. # ์คํฌ๋ฆฝํธ์ ๊ฐ ํ์ด ์คํ๋ ๋๋ง๋ค ํ๋ฉด์ ์ถ๋ ฅ๋๋ค. bash -x { ํจ์๋ช }
-
bash -x
-
trap
bash
#!/bin/bash # test_trap: trap ํ ์คํธ ์คํฌ๋ฆฝํธ trap 'echo "$LINENO : count=$count " ' DEBUG count=1 sum=0 while (( count <= 10 )) do (( sum += count )) let count+=1 done echo "Sum(1~10) : $sum " # * ์ง์ ํ ์๊ทธ๋์ด ์คํฌ๋ฆฝํธ๋ก ์ ๋ฌ๋ ๋๋ง๋ค ์ง์ ํ ๋ช ๋ น ์คํ # * ์คํฌ๋ฆฝํธ์ ๋ช ๋ น์ด ํ ์ค์ฉ ์คํ๋ ๋๋ง๋ค DEBUG ์๊ทธ๋์ด ์คํฌ๋ฆฝํธ๋ก ์ ๋ฌ๋๋ค. # * DEBUG ์๊ทธ๋์ ๋ฐ์ ๋๋ง๋ค ์ํ๋ ๋ณ์ ๊ฐ ์ถ๋ ฅ์ด ๊ฐ๋ฅํ๋ค. # => ์ฆ. ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๋ ๋์ค ๋ณ์๊ฐ ํ์ธ์ด ๊ฐ๋ฅํ๋ค.




Leave a Reply