Shell Script Syntax
๐ ๋ชฉ์ฐจ
Syntax
# example
echo "My name is Jack" | awk -F' ' '{print $1,$2,$3}'
exit n
- ํ์ฌ ์์ ์ข
๋ฃํ๋ค. ์ข
๋ฃ์ n๊ฐ์ ๋ฆฌํดํ๋ค.
# example
exit 3
import
- ๋ค๋ฅธ shell script๋ฅผ importํ๋ค.
# example
source ./scripts/example.sh
# ๋๋
. ./scripts/example.sh
# example
echo "./test" | tr -d "./"
# test
sed
- ๋ฌธ์์ด ์นํ
- ์ข ๋ ์ธ๋ถ์ ์ธ ์ปจํธ๋กค์ด ํ์ํ ๋
# example
echo "hi" | sed 's/hi/hello/' # hello
echo "hi hi" | sed 's/hi/hello/' # hello hi
echo "hi hi" | sed 's/hi/hello/g' # hello hello
echo "hello" | sed 's/hello/& world/g' # hello world (`&`๋ ํจํด ๋งค์นญ๋ ๋ถ๋ถ์ ์๋ฏธ)
=~
IFS
- ์ถ๋ ฅ์ ํน์ ๋ฌธ์๋ก ๊ตฌ๋ถ
# example
IFS=$'\n' ARR=(`df`)
echo ${ARR[@]}
# example
#/bin/bash
IFS=$'\n'
GREPS=(`ls -al`)
IFS=$''
- single quote
'
์์์ ๋ณ์ ์ฌ์ฉ
# example
VAR="HELLO"
echo 'This is test $VAR'
echo 'This is test '"$VAR"'
# ์ถ๋ ฅ
# This is test $VAR
# This is test HELLO
# ์ ๋์ ๊ฐ์ ์ฝ๋์ด๋ค.
# [[]]๋ ๋ณ์์ ""๋ฅผ ์ธ ํ์๊ฐ ์๋ค
[ "$foo" = bar ]
[[ $foo = bar ]]
$(($STRING_NUM) + 1)
echo -e "\n\033[4;31mLight Colors\033[0m \t\t\033[1;4;31mDark Colors\033[0m"
echo -e "\e[0;30;47m Black \e[0m 0;30m \t\e[1;30;40m Dark Gray \e[0m 1;30m"
echo -e "\e[0;31;47m Red \e[0m 0;31m \t\e[1;31;40m Dark Red \e[0m 1;31m"
echo -e "\e[0;32;47m Green \e[0m 0;32m \t\e[1;32;40m Dark Green \e[0m 1;32m"
echo -e "\e[0;33;47m Brown \e[0m 0;33m \t\e[1;33;40m Yellow \e[0m 1;33m"
echo -e "\e[0;34;47m Blue \e[0m 0;34m \t\e[1;34;40m Dark Blue \e[0m 1;34m"
echo -e "\e[0;35;47m Magenta \e[0m 0;35m \t\e[1;35;40m DarkMagenta\e[0m 1;35m"
echo -e "\e[0;36;47m Cyan \e[0m 0;36m \t\e[1;36;40m Dark Cyan \e[0m 1;36m"
echo -e "\e[0;37;47m LightGray\e[0m 0;37m \t\e[1;37;40m White \e[0m 1;37m"
- ํ์ผ ์ฌ์ด์ฆ 0์ผ๋ก ๋ง๋ค๊ธฐ
cat /dev/null > file
# example
animal=cat
echo ${#animal}
# 3