1.shell 변수1
| shell 변수1 | |
| echo “Hello World” | echo”는  쉘  프로그램에서  출력을  수행 “Hello World”라는 문자열을 표준출력(standard Output)으로 보냄. | 
| shvar=“Hello World” | 쉘을  통해  변수에  값을  저장할  수  있다. 문자열은 큰 따옴표로 묶어 변수가 전체 문자열을 나타낼 수 있도록 하고 “=“ 주위에 공백이 없어야 함. | 
| echo $shvar | 쉘 변수의 값은 앞에 “$”를 붙여서 얻을 수 있다. 해당 쉘 변수에 값을 저장하지 않았다면 빈 줄이 생김 | 
| $shvar=“” | 널 (null)문자열을 지정하여 쉘 변수에 저장된 값을 지울 수 있다. | 
| cp $olddir $newdir | 쉘 변수에 저장된 값은 다른 프로그램의 매개 변수로도 사용할 수 있다. | 
| 주의! mv $testfile $testfile2 | testfile 이라는 쉘  변수에  파일  이름이  있고 같은 이름을 가지고 있지만 “2”가 붙은 다른 파일로 해당 파일을 복사하려고 한다고 가정하자. 그러나 쉘은 “testfile2”가 다른 쉘 변수라고 생각하고 작동하지 않음 | 
| mv $testfile ${testfile}2 | 이렇게 해야 testfile 과 testfile 2가 생김. | 
2.shell 변수2 및 실습
| shell 변수2 | |
| $1 | “$(숫자)”를 통해 쉘 프로그램 매개변수를 참조할 수 있다. “$1”은 첫 번째 매개변수, “$2”는 두 번째 매개변수, ... | 
| $# | 쉘 프로그램 매개변수의 개수 | 
| $* | 쉘 프로그램 매개변수 전체 내용($1, $2, ...) | 
| $$ | 쉘 프로그램 실행 프로세스 ID | 
| $! | 쉘 프로그램이 실행시킨 백그라운드 프로세스 ID | 
| $? | 쉘 프로그램이 실행한 프로그램 종료값(리턴값) | 
실습은 더보기를 클릭하면 해당 script를 수행하기 위해 썼던 명령어들을 접어놓았다.
[root@centos7 shell_test]# ll
total 4
-rw-r--r-- 1 root root 66 Oct 29 00:05 sh_test3.sh
[root@centos7 shell_test]# pwd
/tmp/shell_test
[root@centos7 shell_test]# vi sh_test3.sh
[root@centos7 shell_test]# cat sh_test3.sh
#!/bin/sh
first=$1
second=$2
echo “1- $1”
echo “2- $2”
[root@centos7 shell_test]# chmod +x ./sh_test3.sh
[root@centos7 shell_test]# ll
total 4
-rwxr-xr-x 1 root root 66 Oct 29 00:05 sh_test3.sh
[root@centos7 shell_test]# ./sh_test3.sh Infra leesh
“1- Infra”
“2- leesh”
3 shell 의사결정
3-1 if, case 문
if 문, case문을 해당 표에 작성하였다.
| if ~ then exit elif ~ then exit else | if ~ then exit else ----------------------- if ~ then exit elif ~ then exit else 계속해서 늘릴려면 이런 형태로 elif ~ then exit 구조로 활용하면 된다. | 
| case "조건" in “명령어1” exit;; "명령어2” exit;; 명령어;; esac | case 명령어는 in 후 해당 명령어나 조건 내용을 작성해야 한다. 하나의 명령어나 조건이 끝날때마다 끝에 ;; 붙어주어야 되며, 맨 마지막에는 esac으로 닫아준다. | 
실습 script활용 및 실습
| 1.#vi sh_test5.sh 또는 touch 명령어로 해당내용 작성 #!/bin/sh if [ "$1" = "A" ] then echo "A not allowed." exit elif [ "$1" = "B" ] then echo "B not allowed." exit else echo "A&B not allowed" fi echo "C" 2.#chmod +x sh_test5.sh //실행권한을 넣어줌 3. #./sh_test5.sh //해당 명령어로 실행 | <sh_test5.sh 결과값> [root@centos7 shell_test]# ./sh_test5.sh A&B not allowed C [root@centos7 shell_test]# ./sh_test5.sh A A not allowed. [root@centos7 shell_test]# ./sh_test5.sh B B not allowed. [root@centos7 shell_test]# ./sh_test5.sh C A&B not allowed C <주의점> "$ 1"을 큰 따옴표로 묶는 방법에 유의할 것! 따라서 테스트에서 null 결과가 발생하면 오류 메시지가 생성되지 않음. if [ $1 = “A” ] 와 같이 큰 따옴표를 쓰지 않았을 때 인자가 없다면 ./shtest5.sh: line 2: [: =: unary operator expected (에러 발생 | 
| 위의 순서처럼 해당파일 만들고 실행권한 준 후 명령어로 실행하기 case "$1" in “A”) echo “A not allowed.” exit;; “B”) echo “B not allowed.” exit;; *) echo “A & B not allowed";; esac | <sh_test6.sh 결과값> [root@centos7 shell_test]# vi ./sh_test6.sh [root@centos7 shell_test]# ./sh_test6.sh A A not allowed. [root@centos7 shell_test]# ./sh_test6.sh b A&B not allowed [root@centos7 shell_test]# ./sh_test6.sh B B not allowed. [root@centos7 shell_test]# ./sh_test6.sh C A&B not allowed [root@centos7 shell_test]# <특징> 문자열 ";;" 각 "case"절을 종료하는 데 사용되며, 맨 마지막에는 esac 으로 마무리해준다. | 
3-2 shell 의사결정 및 공식
| shell 치환 공식 | ||
| 문자열 | [ -n $shvar ] | 문자열의 길이가 0보다 큰지 | 
| [ -z $shvar ] | 문자열의 길이가 0인지 | |
| [ “$shvar” = “fox” ] [ “$shvar” != “fox” ] | 문자열이  같으면  true 문자열이 다르면 true | |
| [ “$shvar” = “” ] [ “$shvar” != “” ] | 문자열이  null이면  true 문자열이 null이 아니면 true | |
| 숫자 | [ “$nval” –gt 0 ] | 0보다 크면 true | 
| [ “$nval” –ge 0 ] | - 0과 같거나 크면 true | |
| [ “$nval” –eq 0 ] | 0과 같으면 true | |
| [ “$nval” –le 0 ] | 0보다 작거나 같으면 true | |
| [ “$nval” –lt 0 ] | 0보다 작으면 true | |
| [ “$nval” –ne 0 ] | 0과 다르면 true | |
| 결합, 조건문 | conditionA –o conditionB ] | 조건문 A, B 중 하나라도 참인지,o는 OR | 
| conditionA –a conditionB ] | 조건문 A, B 모두 참인지, a는 AND | |
숫자해당 옵션은 이렇게 이해하면 된다.
eq = equal
gt = greater than
ge = greater or equal
le = less or equal
lt = less than
ne = not equal
3-3 for, while, func 구문 활용
| 구문 | 형태 | 응용 실습 | 결과값 | 
| for 반복문 (C언어의 for문과 비슷함) | for variable in value1 value2 ... do command done | for nvar in 1 2 3 4 5 do echo $nvar done | [root@centos7 shell_test] # ./sh_test7.sh 1 2 3 4 5 | 
| while 구문 | while [ condition ] do command done | #!/bin/sh n=5 while [ "$n" -ne 0 ] do echo $n n=`expr $n - 1` done | [root@centos7 shell_test] # ./sh_test8.sh 5 4 3 2 1 | 
| 함수 func_ | func_name() { command } | num=5 func1() { echo "func1 process" } func2() { func_val=0 echo "func2 process" while [ $func_val -lt $1 ] do echo $func_val func_val=`expr $func_val + 1` done } func1 func2 $num | [root@centos7 shell_test] # ./sh_test9.sh func1 process func2 process 0 1 2 3 4 | 
while구문에서
n=`expr $n - 1` 여기선 작은 따옴표가 아닌 물결 마크(~)쪽의 키보드인 ` 를 의미한다.
func 함수문에서도
func_val=`expr $func_val + 1`도 동일하다.
숫자구문에 특히 expr 을 사용한다.
'lecture > script 언어' 카테고리의 다른 글
| 입력&출력[ 재지향(>,>>,2&1), 파이프(|)] (0) | 2021.11.25 | 
|---|---|
| 환경변수 및 shell (0) | 2021.11.09 | 
| 기초 shell script 문법 2 (awk ,sed 설명 및 실습활용) (0) | 2021.10.29 |