Shell은 command-line interpreter이다.
(출처: https://saurabhshivde.hashnode.dev/day-4-90daysofdevops)
위 그림처럼, shell은 keyboards 또는 shell script file을 입력으로 받아서
linux kernel를 통해 system의 HW와 interact
shell script: 'x' 실행권한을 갖고 실행하는 text file
일일이 terminal에 입력했던 command들을 shell script에 넣어서 한꺼번에 자동으로 처리하도록 할 수 있음.
그래서 shell scripts는 다음의 것들을 포함할 수 있음
아래와 같은 shell script를 생성. demo.sh
처음 생성하고 나서,
user에 대한 실행 권한이 없어서 demo.sh를 실행할 수 없음.
user에 대해서만 실행 권한 부여 후, 아래와 같이 실행할 수 있음
#
: 주석#!
: 어떠한 interpreter를 쓸 것인지 지정.\
: end of a line;
: Used to interpret what follows as a new command (명령어를 연속으로 실행)&&
: The action will be performed only if the conditions evaluate to true||
: The action will be performed if any of the conditions evaluate to true$
: $변수, ${변수}, $(command)!
: NOT''
: strong quotes. (안에 있는 변수를 변수로 안보고, string으로 봄)""
: weak quotes (안에 있는 변수를 변수로 봄)''
, ""
example:
||
, &&
example:
shell script에서 variable의 type이 없이, 모두 다 string type
local variable 선언
:
변수명=string (중간에 공백이 없다는 가정) 또는
변수명="string" (중간에 공백이 있다면)
= 앞,뒤로 공백이 있으면 안됨
local variable 사용
:
$변수 또는 ${변수}
위와 같은 local variable 말고, environment variables
도 사용 가능
./myscript.sh hello world
$0
: Script name (myscript.sh)$1
: First parameter (hello)$2, $3
: Second, third parameter, ... (world, ...)$*
or $@
: All parameters (hello world)$#
: Number of parameters (2)$?
: Return valuehttps://linuxhint.com/bash_builtin_examples/
echo
: printf와 유사read
: scanf와 유사$0
: script name$1
: First parameter$2, $3
: Second, third parameter$*
or $@
: All parameters$#
: Number of parameters$?
: Return valueexport VAR=value
VAR=value; export VAR
if-then-fi
if-then-elif-then-else-fi
여기서, []는 test 명령어임.
[ conditional expression ]
대괄호 안에 여러가지 condition을 test할 수 있다.
returns 0 if conditional expression is true; otherwise returns 1 (false)
Numeric comparison
String comparison
$(( a + b ))
$ (expr $a + $b)
let sum=a+b
&&
: AND||
: OR!
: NegationR, G, B
1~100 Random 숫자를 생성하여 사용자가 그 random 숫자를 맞추는 게임