Script
- computer에 내리는 명령어들의 모음
- .sh 형식
- program은 hard disk에 저장되어 있다가, RAM 위에 복사되어 process로 실행됨
- file이 실행되지 않는 경우 chmod 755로 권한을 수정해야 하는 경우가 있음
#!/bin/bash
- 어떤 shell을 실행할 지 선언
- 최상단에 선언되어야 이 file이 shell script인 것을 알 수 있음
변수 (Variables)
- variable=value : variable이라는 변수에 value라는 값을 할당
- variable=$(command) : command 명령어의 결과를 variable 변수에 할당
- $variable : variable 변수를 가져옴
- $0 - The name of the Bash script.
- $1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
- $# - How many arguments were passed to the Bash script.
- $@ - All the arguments supplied to the Bash script.
- $? - The exit status of the most recently run process.
- $$ - The process ID of the current script.
- $USER - The username of the user running the script.
- $HOSTNAME - The hostname of the machine the script is running on.
- $SECONDS - The number of seconds since the script was started.
- $RANDOM - Returns a different random number each time is it referred to.
- $LINENO - Returns the current line number in the Bash script.
* echo
- 화면에 해당 값을 출력하는 명령어
* export
- script의 변수를 script 바깥에서도 사용할 수 있게끔 꺼냄
Quotes (', ")
- ' : '' 안의 script들은 문자열로 취급함
ex) newvar='More $myvar'; echo $newvar; -> More $myvar
- " : "" 안의 script들은 script의 결과를 가져옴
ex) newvar="More $myvar"; echo $newvar; -> More Hello World
입력 받기 (Input)
- read : script 실행 시, terminal에서 입력받을 수 있음
$(( ))
- 수학 연산은 (( ))를 통해 연산값을 가져올 수 있음
ex) a=$(( 4 * 5 )) -> echo $a # 20
조건문 (if)
- if (( )); then ~ fi (수학적 연산이 있는 경우)
- 대괄호 안의 앞 뒤에 반드시 공백이 있어야 함
- if로 시작해서 fi로 끝남
- 조건문 [ ] 사이에 && 또는 || 가능
ex) if [ statements ] && [ statements ]; then ~ fi
if [ <조건> ]
then
<commands>
elif [ <조건> ]
then
<different commands>
else
<other commands>
fi
Operator | True 반환 조건 |
! EXPRESSION | EXPRESSION의 결과가 false일 경우 |
-n STRING | STRING의 길이가 0보다 클 경우 (STRING이 있을 경우) |
-z STRING | STRING의 길이가 0일 경우 (빈 문자열일 경우) |
STRING1 == STRING2 | STRING1, STRING2가 같을 경우 |
STRING1 != STRING2 | STRING1, STRING2가 다를 경우 |
INTEGER1 -eq INTEGER2 | INTEGER1, INTEGER2가 같을 경우 (숫자) |
INTEGER1 -gt INTEGER2 | INTEGER1이 INTEGER2 보다 클 경우 (숫자) |
INTEGER1 -lt INTEGER2 | INTEGER1이 INTEGER2보다 작을 경우 (숫자) |
-d FILE | FILE이 directory인 경우 |
-e FILE | FILE이 존재할 경우 |
-r FILE | FILE이 읽을 수 있는 경우 |
-s FILE | FILE이 비어있지 않을 경우 |
-w FILE | FILE이 쓸 수 있는 경우 |
-x FILE | FILE이 실행할 수 있는 경우 |
반복문 (for)
- 대괄호 안의 앞 뒤에 반드시 공백이 있어야 함
- done으로 끝남
while [ <조건> ]
do
<commands>
done
for var in <list>
do
<commands>
done
함수 (function)
- argument는 일반적인 언어와 달리 function_name 옆의 ()에서 받지 않고, $1, $2, ... 로 받음
ex) print_something() { echo Hello $1 }
function_name () {
<commands>
return <value>
}
function function_name {
<commands>
return <value>
}
출처
https://ryanstutorials.net/bash-scripting-tutorial/
https://reakwon.tistory.com/136
https://ansan-survivor.tistory.com/539
'IT > OS' 카테고리의 다른 글
Interrupt, IRQ란 (0) | 2022.04.02 |
---|---|
[Windows] 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치 파일이 아닙니다. (0) | 2022.03.18 |
[Linux] shell script 함수의 return (0) | 2022.02.27 |
[Linux] Debian vs RHEL (0) | 2022.02.26 |
Mutex, Semaphore, Critical Section (0) | 2022.02.15 |
댓글