관리 메뉴

IT 컴퓨터공학 자료실

리눅스 배쉬 셀, 스크립트 강의 1 - 링크드인, Kevin Dankwardt 본문

리눅스

리눅스 배쉬 셀, 스크립트 강의 1 - 링크드인, Kevin Dankwardt

윤맨1 2021. 10. 25. 13:28

* 이 강의에서 필요한 것
- 리눅스 배쉬 프로그램
- 커맨드라인 경험
- 약간의 프로그래밍 지식

* The Man and info Command
- 배쉬는 많은 기능을 가지고 있다.
- 약간의 온라인 문서가 존재한다.
- 프린트하면 약 100 페이지 정도가 된다.
- https://www.gnu.org/software/bash/manual/bashref.html

Bash Reference Manual

www.gnu.org

- man bash
- info bash
- https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html

Bash Reference Manual

www.gnu.org


* Scrpit File Basics
- 첫 , 두 번째 글자는 #!로 시작 (Known as "Shebang")
- 경로는
#!/usr/bin/env bash

* Script File Basics
- Pearl, Python, Expect, awk, 그리고 Bash 에서도 #!(셔뱅)을 사용합니다.
- The env will look for Bash in your path.
- Just #!/bin/bash 가 가장 일반적이다.

* 스크립트 실행하기
- 스크립트를 chmod u+x 명령어로 실행가능하도록 만들기
- With only read permission, execute a script with bash thescript.sh
- 일반적으로 ./thescript.sh 이런 식으로 ./를 적어서 명령어를 실행함
- .sh 접두사로 사용하면 일반적으로 쉘 스크립트라는 것을 알 수 있음

cat shebang.sh
ps -l

* 쉘 스크립트 실행 시 주의할 점
shebang.sh (X)
./shebang.sh (O)
cat shebang.sh (세모)

* The Bash Time Command
- Bash has a builtin time command to report how much time a process comsumed.

time find / -name core

- real 3m54.103s
user 0m2.676s
sys 0m5.128s

* Variables in Bash
- Bash 에서는 할당을 = 로 해준다.
- =의 앞이나 뒤에 공간은 주면 안된다.
예)
myvar="this is some chars; + $\" )"

===============================

* 쉘 스크립트 실행권한 부여
chmod + x script.sh

*스크립트 실행하기
/home/user1/bin/script.sh
./script.sh

===============================

*export 명령어
: 값을 할당하거나, 변수를 선언할 때 export 명령어를 사용한다.

* Reference the value of a variable with a dollor sign in front of the name :

ehco myvar is $myvar

* For a shell scrpit to get a copy of a shell variable, It needs   to be "exported" :

exported var2="var2 value"

* Grouping in bash
: Bash functions use braces and can modify variable of the shell that calls the function.

a=1     a=1
(           (
a=2     a=2
echo $a     echo $a
# prints 1     # prints 2

* Bash builtins
- Get a list of Bash builtins with the enable command.
- Prefers builtins, keywords, aliases, and functions over commands in the file system.

sleep 2
time sleep 2

ps

echo $a

compgen -k

* Bash Startup
- PATH=$PATH:/usr/local/bin