Basics
Basics ๊ด๋ จ
if statement
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ if-else ๊ตฌ๋ฌธ์ ํ์
if command; then
# <commands>
fi
bash ์์ if
๋ฌธ ์ค์ ์ ์๋ ๋ช
๋ น์ ์คํํ๋ค. ์ด ๋ช
๋ น์ ์ข
๋ฃ ์ํ๊ฐ 0
(๋ช
๋ น์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋จ) ์ด๋ผ๋ฉด then
์๋์ ์๋ ๋ช
๋ น์ด ์คํ๋๋ค. ๋ช
๋ น์ ์ข
๋ฃ ์ํ๊ฐ 0
์ด ์๋๋ผ๋ฉด then
์๋์ ์๋ ๋ช
๋ น์ ์คํ๋์ง ์๊ณ , bash ์์ ์คํฌ๋ฆฝํธ์ ๋ค๋ฅธ ๋ช
๋ น์ผ๋ก ๋์ด๊ฐ๋ค.
if-then-else
if command; then
# <commands>
else
# <commands>
fi
if
๋ฌธ ์ค์ ๋ช
๋ น์ด 0์ด ์๋ ์ข
๋ฃ ์ํ ์ฝ๋๋ฅผ ๋๋ ค์ฃผ๋ฉด bash ์์ else
๋ถ๋ถ์ ๋ช
๋ น์ ์คํํ๋ค.
์ค์ฒฉ๋ if๋ฌธ
if command1; then
# <commands>
elif command2; then
# <more commands>
fi
elif๋ if-then ๊ตฌ๋ฌธ์ else
๋ถ๋ถ์ ์ด์ด ๋๊ฐ๋ค. elif
๋ช
๋ น์ ์ข
๋ฃ ์ํ ์ฝ๋๊ฐ 0
์ด๋ผ๋ฉด bash๋ ๋ ๋ฒ์งธ then
๋ฌธ ๋ถ๋ถ๋์ ๋ช
๋ น๋ค(more commands)์ ์คํํ๋ค.
ํ ์คํธ ๋ช ๋ น ์จ๋ณด๊ธฐ
if test condition; then
๋ช
๋ น
fi
ํ ์คํธ ๋ช ๋ น์ ๋์์๋ ์กฐ๊ฑด์ด ์ฐธ์ผ๋ก ํ๊ฐ๋๋ฉด ํ ์คํธ ๋ช ๋ น์ ์ข ๋ฃ ์ํ ์ฝ๋๋ฅผ 0์ผ๋ก ๋๋ ค์ค๋ค.
bash ์์ if-then ๊ตฌ๋ฌธ์์ ํ ์คํธ ๋ช ๋ น์ ์ฐ์ง ์๊ณ ๋ ์กฐ๊ฑด์ ํ ์คํธํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
if [ condition ]; then
๋ช
๋ น
fi
๋๊ดํธ๋ ํ ์คํธ ์กฐ๊ฑด์ ์ ์ํ๋ค. ํ ๊ฐ์ง ์ฃผ์ํ ์ ์ด ์๋ค. ์ฌ๋ ๋๊ดํธ ๋ค์ ๋ซ๋ ๋๊ดํธ ์์ ๊ฐ๊ฐ ๋น ์นธ์ด ์์ด์ผ ํ๋ค. ์๊ทธ๋ฌ๋ฉด ์ค๋ฅ ๋ฉ์ธ์ง๊ฐ ๋ํ๋๋ค. ํ ์คํธ ๋ช ๋ น ๋ฐ ํ ์คํธ ์กฐ๊ฑด์ ์ธ ๊ฐ์ง ์ข ๋ฅ์ ์กฐ๊ฑด์ ํ๊ฐํ ์ ์๋ค(ํ์ผ ๋น๊ต, ์ซ์ ๋น๊ต, ๋ฌธ์์ด ๋น๊ต).
ํ์ผ ๋น๊ต
์ ์คํฌ๋ฆฝํธ์์ ๊ฐ์ฅ ๊ฐ๋ ฅํ๊ณ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๋น๊ต๋ค. ๋ฆฌ๋ ์ค ํ์ผ ์์คํ ์์ ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ์ ์ํ๋ฅผ ํ ์คํธํ ์ ์๋ค.
๋น๊ต | ์ค๋ช |
---|---|
-d file | ํ์ผ์ด ์กด์ฌํ๊ณ ๋๋ ํ ๋ฆฌ์ธ์ง ๊ฒ์ฌํ๋ค |
-e file | ํ์ผ์ด ์กด์ฌํ๋์ง ๊ฒ์ฌํ๋ค |
-f file | ํ์ผ์ด ์กด์ฌํ๊ณ ํ์ผ์ธ์ง ๊ฒ์ฌํ๋ค |
-L file | ํ์ผ์ด ์ฌ๋ณผ๋ฆญ ๋งํฌ์ด๋ฉด ์ฐธ |
-r file | ํ์ผ์ด ์กด์ฌํ๊ณ ์ฝ์ ์ ์๋์ง ๊ฒ์ฌํ๋ค |
-s file | ํ์ผ์ด ์กด์ฌํ๊ณ ๋น์ด ์์ง ์์์ง ๊ฒ์ฌํ๋ค |
-w file | ํ์ผ์ด ์กด์ฌํ๊ณ ๊ธฐ๋กํ ์ ์๋์ง ๊ฒ์ฌํ๋ค |
-x file | ํ์ผ์ด ์กด์ฌํ๊ณ ์คํํ ์ ์๋์ง ๊ฒ์ฌํ๋ค |
-O file | ํ์ผ์ด ์กด์ฌํ๊ณ ํ์ฌ ์ฌ์ฉ์๊ฐ ์์ ํ ๊ฒ์ธ์ง ๊ฒ์ฌํ๋ค |
-G file | ํ์ผ์ด ์กด์ฌํ๊ณ ๊ธฐ๋ณธ ๊ทธ๋ฃน์ด ํ์ฌ ์ฌ์ฉ์์ ๊ฐ์์ง ๊ฒ์ฌํ๋ค |
file1 -nt file2 | file1์ด file2๋ณด๋ค ์๊ฒ์ธ์ง ๊ฒ์ฌํ๋ค |
file1 -ot file2 | file1์ด file2๋ณด๋ค ์ค๋๋ ๊ฒ์ธ์ง ๊ฒ์ฌํ๋ค |
๋๋ ํ ๋ฆฌ ํ์ธํ๊ธฐ
์ง์ ๋ ๋๋ ํ ๋ฆฌ๊ฐ ์์คํ
์ ์กด์ฌํ๋์ง ๋ณด๋ ค๋ฉด -d
๊ฒ์ฌ๋ฅผ ํ๋ค. ๋ณดํต์ ๋๋ ํ ๋ฆฌ์ ํ์ผ์ ์ฐ๊ฑฐ๋ ๋๋ ํ ๋ฆฌ์ ์์น๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ ์ ์ฌ์ฉํ๋ฉด ์ข๋ค.
jump_directory=/home/arthur
if [ -d $jump_directory ]; then
echo "hi"
fi
๊ฐ์ฒด๊ฐ ์กด์ฌํ๋์ง ์ฌ๋ถ ๊ฒ์ฌํ๊ธฐ
location=$HOME
file_name="bong"
if [ -e $location ]; then # Directory does exist
if [ -e $location/$file_name ]; then
echo "file does exist"
else
echo "file doesn't exist"
fi
else
echo "Directory doesn't exist"
fi
์คํฌ๋ฆฝํธ์์ ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ์ด ๊ฐ์ฑ๊ฐ ์๋์ง ํ์ธํ๋ ค๋ฉด -e ๋น๊ต๋ฅผ ์ฌ์ฉํ๋ค(-e ๋น๊ต๋ ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ ์์ชฝ ๋ชจ๋์ ์ ์ฉ๋๋ค).
์ซ์ ๋น๊ต
๋น๊ต | ์ค๋ช |
---|---|
n1 -eq n2 | n1 ๊ณผ n2 ๊ฐ ๊ฐ์์ง ๊ฒ์ฌํ๋ค |
n1 -ge n2 | n1 ์ด n2 ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์์ง ๊ฒ์ฌํ๋ค |
n1 -gt n2 | n1 ์ด n2 ๋ณด๋ค ํฐ์ง ๊ฒ์ฌํ๋ค |
n1 -le n2 | n1 ์ด n2 ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์์ง ๊ฒ์ฌํ๋ค |
n1 -lt n2 | n1 ์ด n2 ๋ณด๋ค ์์์ง ๊ฒ์ฌํ๋ค |
n1 -ne n2 | n1 ๊ณผ n2 ๊ฐ ๊ฐ์ง ์์์ง ๊ฒ์ฌํ๋ค |
if [ $val1 -gt 1 ]; then
echo "hi"
fi
๋ฌธ์์ด ๋น๊ต
๋น๊ต | ์ค๋ช |
---|---|
str1 = str2 | str1 ์ด str2 ์ ๊ฐ์์ง ๊ฒ์ฌํ๋ค |
str1 != str2 | str1 ์ด str2 ์ ๊ฐ์ง ์์์ง ๊ฒ์ฌํ๋ค |
str1 < str2 | str1 ์ด str2 ๋ณด๋ค ์์์ง ๊ฒ์ฌํ๋ค |
str1 > str2 | str1 ์ด str2 ๋ณด๋ค ํฐ์ง ๊ฒ์ฌํ๋ค |
-n str1 | str1 ์ ๊ธธ์ด๊ฐ 0๋ณด๋ค ํฐ์ง(0์ด ์๋์ง) ๊ฒ์ฌํ๋ค |
-z str1 | str1 ์ ๊ธธ์ด๊ฐ 0์ธ์ง ๊ฒ์ฌํ๋ค |
๋ฌธ์์ด์ด ๊ฐ์๊ฐ ๊ฐ์ง ์์๊ฐ ๋น๊ตํ ๋๋ ๋ชจ๋ ๋ฌธ์ฅ๋ถํธ์ ๋๋ฌธ์๋ ๊ณ ๋ ค๋๋ค๋ ์ ์ ์์ง ๋ง์.
testuser=bong
if [ $USER = $testuser ]; then
echo "hi"
fi
์ด๋ค ๋ฌธ์์ด์ด ๋ค๋ฅธ ๋ฌธ์์ด๋ณด๋ค ํฐ๊ฐ ์์๊ฐ๋ฅผ ํ๋จํ ๋๋ถํฐ ์ผ์ด ๋ณต์กํด์ง๋ค. ๋ฌธ์์ด์ด ํฐ์ง์ ์ฌ๋ถ๋ฅผ ํ ์คํธํ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ๋๊ฐ์ง ๋ฌธ์ ๊ฐ ์๋ค.
์ฒซ์งธ, ๋ถ๋ฑํธ ๊ธฐํธ๋ฅผ ์ด์ค์ผ์ดํ ํด์ผ ํ๋ ๊ฒ. ๊ทธ๋ ์ง ์์ผ๋ฉด ์์ ์ด๋ฅผ ๋ฆฌ๋ค์ด๋ ํธ ๊ธฐํธ๋ก ํด์ํด์ ๋ฌธ์์ด ๊ฐ์ ํ์ผ ์ด๋ฆ์ผ๋ก ์ฌ์ฉํ๋ค.
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "hi"
fi
๋์งธ, ์ด๋ ๊ฒ์ด ๋ ํฐ์ง ์์๋ฅผ ๊ฒฐ์ ํ๋ ๋ ผ๋ฆฌ๋ sort ๋ช ๋ น์์ ์ฐ์ด๋ ๊ฒ๊ณผ ๊ฐ์ง ์๋ค.
๋น๊ต ํ ์คํธ์์๋ ํ์ค ASCII ์์๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์ ๋ ฌ ์์๋ฅผ ๊ฒฐ์ ํ๊ธฐ ์ํ์ฌ ๊ฐ ๋ฌธ์์ ASCII ์ซ์ ์ฝ๋๊ฐ์ ์ด์ฉํ๋ค. sort ๋ช ๋ น์ ์์คํ ๋ก์ผ์ผ์ ์ธ์ด ์ค์ ์ ์ ์๋ ์ ๋ ฌ ์์๋ฅผ ์ฌ์ฉํ๋ค. ์์ด๋ผ๋ฉด ๋ก์ผ์ผ ์ค์ ์ ์๋ฌธ์๋ฅผ ๋๋ฌธ์๋ณด๋ค ์์์ ์ ๋ ฌํ๋๋ก ์ง์ ํ๋ค.
๋น์ด์๊ณ ์ด๊ธฐํ๋์ง ์์ ๋ณ์๋ ์ ์คํฌ๋ฆฝํธ ํ ์คํธ์ ์น๋ช ์ ์ธ ์ํฅ์ ๋ฏธ์น ์ ์๋ค. ๋ณ์์ ๋ด์ฉ์ด ํ์คํ์ง ์์ผ๋ฉด ์ซ์ ๋๋ ๋ฌธ์์ด ๋น๊ต๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ -n ๋๋ -z๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ํฌํจํ๋์ง ํ ์คํธํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข๋ค.
val1=testing
if [ -n $val1 ]; then
echo "hi"
fi
if [ -z $val2 ]; then
echo "hi"
fi
for๋ฌธ
FILE="/Users/bong"
for state in $(ls $FILE)
do
echo $state
done
for i in ~/bong*.sh; do
if [ -r "$i" ]; then
. $i
fi
done
for๋ฌธ ๋๋ฆฌ๋ฉด์ ์คํฌ๋ฆฝํธ ํ์ผ ์คํ
C ์คํ์ผ for๋ฌธ
TEST_TRIES=7
TEST_INTERVAL_SECONDS=5
for (( TRY_COUNT = 1; TRY_COUNT <= $TEST_TRIES; TRY_COUNT ++ )); do
sleep $TEST_INTERVAL_SECONDS
echo "Checking HTTP port. ("$TRY_COUNT"/$TEST_TRIES)"
HTTP_STATUS_CODE=`curl -sL -o /dev/null -I -w "%{http_code}" $TEST_URL --max-time 10`
if [ $HTTP_STATUS_CODE -gt 199 ] && [ $HTTP_STATUS_CODE -lt 300 ]; then
echo "The Spring Boot process has started successfully."
break
fi
if [ $TRY_COUNT = $TEST_TRIES ]; then
echo "ERROR : The Spring Boot process failed to start."
exit 1
fi
done
while๋ฌธ
๊ธฐ๋ณธ while ํ์
var1=10
while [ $var1 -gt 0 ]; do
echo $var1
var1=$[ $var1 - 1 ]
done
`getopts` ๋ช ๋ น์ด ์ฌ์ฉ
# -a ์ต์
์ด ์๋์ง ํ๋๊ทธ ๋ณ์ a_flag์
# -p ์ต์
์ ๊ตฌ๋ถ์๋ฅผ ์ ์ํ๊ธฐ
a_flag=0
separator=""
while getopts "ap:" option
do
case $option in
a)
a_flag=1
;;
p)
separator="$OPTARG"
;;
\?)
echo "Usage: getopts.sh [-a] [-p separator] target_dir" 1>&2
exit 1
;;
esac
done
case ๋ช ๋ น
case variable in
pattern1 | pattern2)
commands1
;;
pattern3)
commands2
;;
*)
default commands
;;
esac
์์
bong.sh
echo_host() {
host=`hostname`
echo "[$host] $1"
}
#### Main #####
OPTION=$1
case $OPTION in
"start") start_nginx;;
"stop") stop_nginx;;
"restart") restart_nginx;;
*) echo_host "option : $0 start|stop|restart";;
esac
bong.sh
์คํ๊ฒฐ๊ณผ
./bong.sh ttt
# [MyMacBook] option : ./bong.sh start|stop|restart
์คํฌ๋ฆฝํธ ์ข ๋ฃํ๊ธฐ
๋ฆฌ๋ ์ค ์ข ๋ฃ ์ํ์ฝ๋
์ฝ๋ | ์ค๋ช |
---|---|
0 | ๋ช ๋ น์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋จ |
1 | ์ผ๋ฐ ์ ์ ์๋ ์ค๋ฅ |
2 | ์ ๋ช ๋ น์ ์๋ชป ์ฌ์ฉํจ |
126 | ๋ช ๋ น์ ์คํํ ์ ์์(Permission denied) |
127 | ๋ช ๋ น์ ์ฐพ์ ์ ์์ |
128 | ์๋ชป๋ ์ข ๋ฃ ๋งค๊ฐ๋ณ์ |
128+x | ์น๋ช ์ ์ธ ์ค๋ฅ๋ก ๋ฆฌ๋ ์ค ์ ํธ x๋ฅผ ํฌํจ |
130 | Ctrl+C๋ก ๋ช ๋ น์ด ์ข ๋ฃ๋จ |
255 | ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์ข ๋ฃ ์ํ |
์ข ๋ฃ ์ฝ๋ ํ์ธํ๋ ๋ช ๋ น์ด
echo $?
cf) ์ข ๋ฃ ์ํ ์ฝ๋๋ 0 ~ 255๊น์ง ์ธ ์ ์๋ค.
exit ๋ช ๋ น
์ ์คํฌ๋ฆฝํธ๋ ๋ง์ง๋ง ๋ช ๋ น์ ์ข ๋ฃ ์ํ๋ก ๋๋ง์น๋ค. ์ฌ์ฉ์ ์ ์ ์ข ๋ฃ ์ํ ์ฝ๋๋ฅผ ๋๋ ค์ฃผ๋๋ก ์ด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค. exit ๋ช ๋ น์ ์คํฌ๋ฆฝํธ๊ฐ ์ข ๋ฃ๋ ๋ ์ข ๋ฃ ์ํ๋ฅผ ์ง์ ํ ์ ์๋ค.
test13
#!/bin/bash
var1=10
var2=30
var3=$[$var1 + $var2]
echo The answer is $var3
exit5
#exit $var3 ์ฒ๋ผ exit ๋ช
๋ น์ ๋งค๊ฐ๋ณ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค.
๊ฒฐ๊ณผ
chmod u+x test13
./test13
# The answer is 40
echo $?
# 5
Multiline ์ ๋ ฅ
\
์ ๋ถ์ฌ ์ปค๋งจ๋ ์
๋ ฅ
wsdl2java.sh -u -t -ss -sd -g -b \
-o ./BmsSifDctToProcessMobService \
-uri http://onnara.saas.gcloud.go.kr/bms/service/BmsSifDctToProcessMobService?wsdl
aliases
Basics
alias ls='ls -l'
with Argument(s)
alias wrap_args='f(){ echo before "$@" after; unset -f f; }; f'
wrap_args x y z
# before x y z after
You can replace
$@
with $1
if you only want the first argument.
Explanation
This creates a temporary function f
, which is passed the arguments.
Alias arguments are only passed at the end. Note that f
is called at the very end of the alias.
The unset -f
removes the function definition as the alias is executed so it doesn't hang around afterwards.
์ ์คํฌ๋ฆฝํธ ์์ฑํ ์คํํ๊ธฐ ์ ์ ๋ฌธ๋ฒ์ ํ์ธํ๋ -n
์ต์
sh -n script.sh
ps
`java` ํ๋ก์ธ์ค PID ๋ฆฌ์คํธ์ ํด์ ์ถ๋ ฅ
ps -ef | grep java | awk '{print $2}'
`grep` ํ๋ฉด ๋ฐฉ๊ธ ์คํํ ๊ฒ๋ ์กํ๊ธฐ ๋๋ฌธ์ ๊ทธ๊ฑฐ ์ ์ธํด์ฃผ๋ ๋ช ๋ น์ด
ps -ef | grep java | grep -v grep
pid๊ฐ ์ฌ๋ฌ๊ฐ์ผ ๋ checkํ๋ if๋ฌธ ์ถ๊ฐ์ ์ผ๋ก
ps -ef | grep nginx | grep -v grep | wc -l
์์ฉ: check_running()
check_running() {
PID=`ps -ef | grep nginx | grep -v grep | awk '{print $2}'`
if [[ -n $PID ]] ; then
echo "nginx process already started (PID:$PID)"
exit 1
fi
}
Make tasks running in background
์ปค๋ฉ๋ ๋ค์ &
์ถ๊ฐ
tail -f /var/log/messages &
๊ฒฐ๊ณผ์ฐฝ์ [1] 614
์ด ๋์จ๋ค๋ฉด
fg 1
๋ฅผ ์ ๋ ฅํ์ฌ ๋ค์ foreground๋ก ๋ณต๊ท
Quickly jump to frequently-used directories
CDPATH='.:~:/usr/local/apache/htdocs:/disk1/backups'
export CDPATH
cd
์
๋ ฅ ํ ์์ฃผ ์ฐพ๋ ๋๋ ํ ๋ฆฌ๋ฅผ ์๋ ค์ค๋ค.
Perform calculations
์ซ์๊ณ์ฐ
echo $((16/2))