Programming/Shell-script
자동 입력 쉘 스크립트 작성 (Here Document / Input Redirection 활용)
woonizzooni
2020. 11. 23. 00:16
얼마전 누구(?)의 급한 요청을 받음.
$ sudo /usr/whatap/php/install.sh
Input license key xxxxxxxxxxxxxxxx <-- 발급된 라이센스 key 입력
Input whatap.server.host 10.10.10.10/.. <-- 발급된 서버 IP 입력
대충 이렇게 동작하는 스크립트를 오토스케일링 되는 EC2에 자동 입력되도록 하고 싶은데 방법을 모르겠다. 도와달라~
> install.sh 를 대충 아래와 같다고 가정. (stdin을 두번 받도록...)
#!/bin/sh while true; do echo "serial"; read input if [ ${#input} -gt 0 ]; then echo "${input} is OK"; fi echo "host?"; read input if [ ${#input} -gt 0 ]; then echo "${input} is OK"; break; fi done |
[방법 1] << : Here Document 이용
<< 로 임시 파일(?) 생성 & 내용 작성해서 stdin에 연결함.
ex) 형식
$ command << delimiter bla bla bla delimeter |
- 다음과 같은 자동 입력 스크립트 작성(auto_input.sh)
- 실행
#!/bin/sh serial="1233456abcde" host="10.10.10.1" ${HOME}/install.sh << EOF ${serial} ${host} EOF |
<-- EOF대신 다른 단어 사용 가능한데, <-- 이 곳과 같은 문자로만 표기하면 됨. |
$ ./auto_input.sh$ chmod 755 ./auto_input.sh
serial?
123456abcdefg is OK!
host?:
10.10.10.1 is OK!
[방법 2] < : Input Redirection 이용
- 입력할 텍스트 문서 작성 후 install.sh에 input redirection
$ vim input.txt
123456abcdefg <-- 줄이 new line으로 구분되어 있어야 함. (원하는 input개수 만큼)
10.10.10.1
$ ./install.sh < input.txt
serial?
123456abcdefg is OK!
host?:
10.10.10.1 is OK!
방법은 여러가지가 있겠지...