본문 바로가기

Linux/Bash shell

[Linux] read 읽기 명령, 파일 실행(sh & ./ ) 명령

# 정리

//읽기 명령어 (read)를 사용할 수 있다.
 - read는 입력 받는 값을 지정된 변수에 저장한다.

// 실행 명령어 (sh),(./)를 사용하고, 두 명령어의 차이를 설명할 수 있다.
 - sh - (실행 파일이 아니어도) 실행해줘
 - ./ - (실행 파일일 때) 실행해줘

 


 

# read

<read는 명령 후, 하단에 다시 값을 입력해줘야함>

pi@raspberry:~/shellPro $ read a   >>입력>>  999

- 변수 a에 저장할 값을 읽어줘 >> a변수에 999 입력됨

pi@raspberry:~/shellPro $ read a   >>입력>>   hello

- 변수 a에 hello 입력 
pi@raspberry:~/shellPro $ read -p "input your name:" name  >> (input your name:)입력>>  kim
- (-p옵션) "input your name:" 쓰고 변수name에  입력값 받아줘

** " " 변수 >> "" 과 변수 사이에 띄어쓰기 해줘야함

- pi@raspberry:~/shellPro $ read -p "input kor[0-100]:" kor
- pi@raspberry:~/shellPro $ read -p "input eng[0-100]:" eng

 

//읽은 값은 echo, printf 등의 출력 명령어로 확인가능
pi@raspberry:~/shellPro $ printf "name:%s\n" $name
pi@raspberry:~/shellPro $ echo "name:$name"
pi@raspberry:~/shellPro $ printf "name:%5s\n" $name

 


# 파일 실행

// $ ./ 파일명 - (실행 파일) 실행 해줘

// $ sh 파일명 - (실행 파일이 아닐 ) 실행 해줘

// cat으로 입력했을 때

 - ./로 실행 불가

 - sh로 했을 때는 예전에 등록된 값을 기준으로 변수에 값만 들어간채로 문장 전체가 출력됨(실행 x)