DEV Community

Cover image for Shell Scripting in Linux
Waji
Waji

Posted on • Edited on

Shell Scripting in Linux

Shell is a UNIX term for interactive user interface with Linux. It understands the commands written by the user. It exists outside of the operating system and it helps in communication between the kernel and the user.

๐Ÿ’ก Some examples are cshell, bourneshell, bashshell, tcshell, kornshell.


์ง€์—ญ ๋ณ€์ˆ˜๋Š” ์ž์‹ ์„ ์ƒ์„ฑํ•œ Shell์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” ์ž์‹ ์„ ์ƒ์„ฑํ•œ Shell ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ด๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋˜์–ด ๋‚˜์˜จ ์ž์‹ ํ”„๋กœ์„ธ์Šค์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

โžœ  ~ pstree
systemdโ”€โ”ฌโ”€ModemManagerโ”€โ”€โ”€2*[{ModemManager}]
        โ”œโ”€NetworkManagerโ”€โ”€โ”€2*[{NetworkManager}]
        โ”œโ”€VGAuthService
.
.
.
**โ”œโ”€sshdโ”€โ”€โ”€sshdโ”€โ”€โ”€zshโ”€โ”€โ”€pstree**
.
.
.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Just a reminder that init starts the processes 'one by one' however, the systemd starts processes in a 'group'


๋ณ€์ˆ˜ ์„ ์–ธ

โžœ  ~ A=itbank
โžœ  ~ export NAME=waji  
โžœ  ~ echo A
A
โžœ  ~ echo NAME
NAME
โžœ  ~ echo $NAME
waji
Enter fullscreen mode Exit fullscreen mode
โžœ  ~ env
.
.
.
LESS=-R
LSCOLORS=Gxfxcxdxbxegedabagacad
**NAME=waji**
Enter fullscreen mode Exit fullscreen mode
.bash_profile

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
**export NAME=waji**
Enter fullscreen mode Exit fullscreen mode

์ œ์–ด๋ฌธ

If-else

if [ ์กฐ๊ฑด 1 ]; then
        โ€œ์กฐ๊ฑด 1์ด ์ฐธ์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ๋‚ด์šฉโ€
elif [ ์กฐ๊ฑด 2 ]; then
        โ€œ์กฐ๊ฑด 2๊ฐ€ ์ฐธ์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ๋‚ด์šฉโ€
else
        โ€œ๋ชจ๋“  ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ๋‚ด์šฉโ€
fi
Enter fullscreen mode Exit fullscreen mode

Case

case ์กฐ๊ฑด in
        ์กฐ๊ฑด 1 ) 
        โ€œ์กฐ๊ฑด 1์ด ์ฐธ์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€;;
        ์กฐ๊ฑด 2 ) 
        โ€œ์กฐ๊ฑด 2๊ฐ€ ์ฐธ์ผ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€;;
        * ) 
        โ€œ์กฐ๊ฑด 1, ์กฐ๊ฑด 2๊ฐ€ ์ฐธ์ด ์•„๋‹ ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€;;
esac
Enter fullscreen mode Exit fullscreen mode

Select

select ๋ณ€์ˆ˜ in [ ํ•ญ๋ชฉ1 ํ•ญ๋ชฉ2 ํ•ญ๋ชฉ3 .. ]
do 
        โ€œํ•ญ๋ชฉ์ด ์œ ํšจ ํ•  ๊ฒฝ์šฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€
done
Enter fullscreen mode Exit fullscreen mode

For

for ๋ณ€์ˆ˜ in ์ธ์ž1, ์ธ์ž2, ์ธ์ž3 โ€ฆ [ ๋ฐฉ๋ฒ•.1 ]
for (( ์ดˆ๊ธฐ๊ฐ’;์ข…๋ฃŒ๊ฐ’;์ฆ๊ฐ๊ฐ’ )) [ ๋ฐฉ๋ฒ•.2 ] ( โ˜… )
do 
            โ€œ๋ฐ˜๋ณตํ•˜๋ฉฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€
done
Enter fullscreen mode Exit fullscreen mode

While

while [ ์กฐ๊ฑด ]
do 
        โ€œ์กฐ๊ฑด์ด ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€
done
Enter fullscreen mode Exit fullscreen mode

Function

function ํ•จ์ˆ˜๋ช…( ) {
            โ€œํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰ ํ•  ์‹คํ–‰๋ฌธโ€
}

ํ•จ์ˆ˜๋ช… # ํ•จ์ˆ˜ ํ˜ธ์ถœ
Enter fullscreen mode Exit fullscreen mode

ETC

1. [eval]
- ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜๊ณ  ์›ํ•˜๋Š” ์‹œ์ ์— ์‹คํ–‰ ๋  ๋•Œ ์‚ฌ์šฉ ๋œ๋‹ค.
- EX) str=โ€œls -l /homeโ€
 eval $str

2. [ set, unset ]
- ํŠน์ • ๋ณ€์ˆ˜ ํ• ๋‹น ๋ฐ ํ• ๋‹น ํ•ด์ œ์‹œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
- ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ShellScript๋ฅผ ์ฆ‰์‹œ ์ข…๋ฃŒํ•˜๋Š” ์šฉ๋„๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ ( set -e )

3. [ $(๋ช…๋ น์–ด) ]
- ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)