내용으로 건너뛰기
권남
사용자 도구
로그인
사이트 도구
검색
도구
문서 보기
이전 판
역링크
최근 바뀜
미디어 관리자
사이트맵
로그인
>
최근 바뀜
미디어 관리자
사이트맵
추적:
linux:shell_script
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== Linux/Unix shell script ====== * [[linux:bash|Bash]] * [[linux:sed|Linux sed]] * [[linux:awk|awk]] * [[linux:xargs|xargs]] ===== 인자 갯수 검사 Argument count ===== * ''$#'' 가 스크립트의 인자 갯수를 가진 변수이다. <code sh> if [ "$#" -ne 2 ]; then echo "인자 2개를 넘겨주어야 합니다." fi </code> ===== pipe 출력 결과를 그대로 실행하기 ===== * [[https://stackoverflow.com/questions/1279953/how-to-execute-the-output-of-a-command-within-the-current-shell|bash - How to execute the output of a command within the current shell? - Stack Overflow]] * 마지막 pipe 를 ''source /dev/stdin'' 으로 해주면 된다. <code sh> ls | sed ... | source /dev/stdin </code> ===== Script 안에서 특정 내용의 파일 생성하기 ===== * shell script 파일 내용만으로 새로운 파일을 생성한다. * [[aws:user_data|AWS user data]] 에서 shell script 만으로 파일을 생성하려고 사용. * https://stackoverflow.com/questions/4879025/creating-files-with-some-content-with-shell-script <code sh> cat <<EOF >/home/a.config first line second line third line EOF </code> ===== 다른 명령 Stream 으로부터 지속적으로 결과 읽어서 처리하기 ===== * [[https://unix.stackexchange.com/questions/52026/bash-how-to-read-one-line-at-a-time-from-output-of-a-command|shell - Bash: How to read one line at a time from output of a command? - Unix & Linux Stack Exchange]] <code sh> find . -type f | while read -r line do echo "$line" done # one liner find . -type f | while read -r line; do echo "$line"; done # 혹은 while read -r line do echo "$line" done < <(find . -type f) </code> * [[http://mywiki.wooledge.org/ProcessSubstitution|ProcessSubstitution - Greg's Wiki]] ===== 실패시 fallback 처리 ===== * ''명령 || 다른 명령'' 을 하면 첫번째 명령이 성공하면 그대로 넘어가고 실패했을 경우에는 다른 명령을 실행하고 그 결과를 최종 반환한다. <code sh> # curl 명령 실패시에 "{}" 를 변수에 값으로 저장 TEST_RESULT=$(curl -Ss https://.... || echo "{}") </code> ===== 명령의 존재 검사 ===== * ''command'' : POSIX 호환. 정확히는 명령에 대해 설명을 출력하는 것임. alias, function 등도 체크함. <code sh> command -v <원하는명령> if ! command -v <the_command> &> /dev/null then echo "<the_command> could not be found" exit fi </code> * ''hash <명령>'' : 일반적인 명령 검사. alias, function 도 존재여부 exit code로 확인가능. 명령이 존재하면 exit code ''0''이고 아니면 오류 메시지. 따라서 error stream 처리(''2>/dev/null'')이 필요하다. * ''type <명령>'' : shell 내장 명령 혹은 keyword 검사. 설명 메시지 혹은 존재하지 않는 명령이면 오류메시지. ''&>/dev/null'' stream 처리 필요. <code sh> if hash gdate 2>/dev/null; then gdate "$@" else date "$@" fi </code> * [[https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script|How can I check if a program exists from a Bash script? - Stack Overflow]] ===== 참조 ===== * [[https://mug896.github.io/bash-shell/index.html|bash shell script]] * [[https://linuxconfig.org/bash-scripting-tutorial-for-beginners|Bash Scripting Tutorial for Beginners - LinuxConfig.org]] * [[https://www.shellscript.sh/|The Shell Scripting Tutorial]] * [[https://bash.cyberciti.biz/guide/Main_Page|Linux Shell Scripting Tutorial - A Beginner's handbook]] * [[https://www.tutorialspoint.com/unix/shell_scripting.htm|Shell Scripting Tutorial - Tutorialspoint]] * [[https://www.youtube.com/watch?v=X3BIc9EHBuk|(1) Fix Your Shell Scripts With Shellcheck - YouTube]] * [[https://opensource.com/article/20/12/learn-bash|Learn Bash by writing an interactive game | Opensource.com]] * [[https://opensource.com/article/19/10/learn-bash-command-line-games|3 command line games for learning Bash the fun way | Opensource.com]] * [[https://www.baeldung.com/linux/reading-output-into-array|Reading Output of a Command Into an Array in Bash | Baeldung on Linux]]
linux/shell_script.txt
· 마지막으로 수정됨: 2024/01/19 08:09 저자
kwon37xi
문서 도구
문서 보기
이전 판
역링크
맨 위로