Shell Script Syntax

์žญ์žญ์ดยท2021๋…„ 4์›” 13์ผ
1

Shell Script

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail

Shell Script Syntax

๐ŸŽ ๋ชฉ์ฐจ

Syntax

  • awk
    • ๋ฌธ์ž์—ด split
# 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    
  • tr
    • ๋ฌธ์ž์—ด ์น˜ํ™˜
# 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

0๊ฐœ์˜ ๋Œ“๊ธ€