Shell script Basics

Basic script

#!/bin/sh
# comment
# command
echo "shell script"

Exit

# ์Šคํฌ๋ฆฝํŠธ ์ข…๋ฃŒ
exit
# ์Šคํฌ๋ฆฝํŠธ ์ข…๋ฃŒ, ์ข…๋ฃŒ ์ƒํƒœ ๋ฐ˜ํ™˜
# exit state
exit 1

Shellcheck

sudo apt install -y shellcheck

Variables

# str1 = hello ์•ˆ๋จ
str1=hello
# str2=hello world ์•ˆ๋จ
str2="hello world"
# ์ˆซ์ž๋„ ๋ฌธ์ž์—ด๋กœ ์ €์žฅ๋จ
str3=100
# ์ƒ์ˆ˜
readonly STR4="CONST"
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜
export STR5="ENV"
echo $str1
# echo $str2!! ์•ˆ๋จ
echo ${str2}!!
caution

shell script๋Š” ๊ณต๋ฐฑ ์‚ฌ์šฉ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Reserved Variable

  • $HOME: ์‚ฌ์šฉ์ž ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ
  • $PATH: ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ
  • $EUID: Effective User ID

Commands

ํ„ฐ๋ฏธ๋„์—์„œ ์‚ฌ์šฉํ•˜๋˜ ์ปค๋งจ๋“œ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# commands
ls /dev/i2c*
echo "hi"
stdout=$(command)

Functions

sample_func() {
# parameter 1, 2 ,3 ๋ฒˆ
echo $1 $2 $3 ...
global_var=1
local local_var=1
return 1
}
sample_func arg1 arg2 arg3 ...
# return -> $?
echo $?

Special parameters

  • $0: ์Šคํฌ๋ฆฝํŠธ ์ด๋ฆ„
  • $n: n๋ฒˆ์งธ ์ธ์ž
  • $*: ๋ชจ๋“  ์ธ์ž
  • $@: ๋ชจ๋“  ์ธ์ž
  • $#: ์ธ์ž ๊ฐœ์ˆ˜
  • $?: ์Šคํฌ๋ฆฝํŠธ์—์„œ ์ตœ๊ทผ ์‹คํ–‰๋œ ๋ช…๋ น์–ด, ํ•จ์ˆ˜, ์ž์‹ ์Šคํฌ๋ฆฝํŠธ ์ข…๋ฃŒ ์ƒํƒœ

if ๋ฌธ

if [ conditional expression ]; then
# ํ•˜๋‚˜ ์ด์ƒ ์‹คํ–‰์ด ์—†์œผ๋ฉด ์˜ค๋ฅ˜ -> : ์‚ฌ์šฉ
:
fi
if test conditional expression; then
:
fi
if $(return 0); then
# ์•„๋ฌด ํ•จ์ˆ˜๋‚˜ ์‚ฌ์šฉํ•ด๋„ ๋จ return 0 -> ์„ฑ๊ณต
:
fi
if [ expression ]; then
echo "true"
else
echo "false"
fi
if [ expression1 ]; then
echo "ex1 true"
elif [ expression2 ]; then
echo "ex2 true"
fi
# [ expression1 -a expression2 ]
if [ expression1 ] && [ expression2 ]
then
:
fi
# [ expression1 -o expression2 ]
if [ expression1 ] || [ expression2 ]
then
:
fi

Conditional expressions

Relation

  • $num1 -eq $num2: num1 == num2
  • $num1 -ne $num2: num1 != num2
  • $num1 -gt $num2: num1 > num2
  • $num1 -ge $num2: num1 >= num2
  • $num1 -lt $num2: num1 < num2
  • $num1 -le $num2: num1 < = num2

String

  • "$str1" = "$str2": ๋ฌธ์ž์—ด์ด ๊ฐ™์œผ๋ฉด true
  • "$str1" != "$str2": ๋ฌธ์ž์—ด์ด ๋‹ค๋ฅธ๋ฉด true
  • -z "$str": ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด true
  • -n "$str": ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฉด true

File test

  • -e file: ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด true
  • -d directory: ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋ฉด true
  • -r file: ํŒŒ์ผ์ด ์ฝ๊ธฐ ๊ฐ€๋Šฅํ•˜๋ฉด true
  • -w file
  • -x file

Boolean

  • ! expression: ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ด๋ฉด true

Return 0

#!/bin/sh
a() {
return 0
}
if a; then
echo "return 0 -> true"
fi

while ๋ฌธ

while [ conditional expression ]; do
:
done

for ๋ฌธ

for i in one two three; do
echo "$i"
done
# seq first last
# $(seq 1 last) -> $(seq last)
for i in $(seq 3 10); do
echo "$i"
done
# seq first increment last
for i in $(seq 1 2 10); do
echo "$i"
done

case ๋ฌธ

case ${var} in
a)
echo "a ์ž…๋‹ˆ๋‹ค."
;;
b|c)
echo "b ๋˜๋Š” c ์ž…๋‹ˆ๋‹ค."
;;
d*)
echo "d๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค."
;;
*)
echo "์œ„ ์กฐ๊ฑด์— ํ•ด๋‹น๋˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด ์ž…๋‹ˆ๋‹ค."
;;
esac

Importing other scripts

#!/bin/sh
sub_func() {
echo "sub"
}
#!/bin/sh
# . /abs_path/sub.sh
. ./sub.sh
sub_func

Arithmetic operations

$((expression))
a=1
b=2
c=$(( a + b )) # 3
Last updated on