---------------------------------------------------------
# 스크립트 실행 방법
---------------------------------------------------------
echo "hello world"
printf "hello world"
printf "%s %s" hello world
두 가지 방법으로 bash 파일을 실행 가능
# 첫번째 방법
$ bash test.sh
# 두번째 방법
$ chmod a+x test.sh
$ ./ test.sh
---------------------------------------------------------
# 주석처리
---------------------------------------------------------
#!/bin/bash
val=25
# Comment - print value
echo $val
# Multiline comment
: '
multiline comment
multiline comment
multiline comment
multiline comment
multiline comment
'
echo $val
# output
$ bash test.sh
25
25
---------------------------------------------------------
# 스크립트 auto indent (줄맞춤)
---------------------------------------------------------
# vim에서 gg=G를 입력한다.
gg - go to beginning of the file
G - go to end of the file
= - indent
# vimrc 설정
set sw=4
filetype indent on
# Reference
https://unix.stackexchange.com/questions/19945/auto-indent-format-code-for-vim
https://www.unix.com/unix-for-dummies-questions-and-answers/87221-vi-auto-indent-whole-file-once.html
# vscode에서 plugin을 설치
Shift + Option + F
# Reference
https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format
https://marketplace.visualstudio.com/items?itemName=shakram02.bash-beautify
---------------------------------------------------------
# 쉘스크립트 문법 체크
---------------------------------------------------------
아래 사이트에서 문법이 잘못된 부분을 체크해준다.
https://www.shellcheck.net/
---------------------------------------------------------
# 디버깅 - set 옵션들 (-e, -u, -x)
---------------------------------------------------------
set은 쉘의 옵션을 설정하는 명령어이다.
-e, -u 옵션을 설정하면 스크립트에서 문제가 발생한것을 찾을수 있다.
* -e: 쉘 스크립트 실행 중에 0이 아닌 값으로 exit을 한 명령이 있으면 나머지 스크립트를 실행하지 않은 채 exit한다.
* -u: 정의되지 않은 변수(undefined)를 참조하면 에러를 출력하며 바로 exit한다.
* -x: 쉘 스크립트가 실행하는 모든 명령을 화면에 출력한다. 디버깅에 유용하다.
# 쉘스크립트의 도입부에 다음과 같이 적으면 된다.
set -e -u -x
# bash xxx.sh 실행시 옵션 적용
bash -x xxx.sh
bash -eux xxx.sh
---------------------------------------------------------
# shell command 실행
---------------------------------------------------------
#!/bin/bash
# use backticks " ` ` " to execute shell command
echo `date`
# executing bash command without backticks
echo date
# use #(command)
echo $(date)
# shell execution
echo "I'm in $(pwd)"
echo "I'm in `pwd`"
# output
$ bash test.sh
2021년 4월 10일 토요일 04시 10분 28초 KST
date
2021년 4월 10일 토요일 04시 10분 28초 KST
I'm in /src/shell
I'm in /src/shell
---------------------------------------------------------
# 스크립트 디렉토리와 이름 출력
---------------------------------------------------------
#!/bin/bash
# Get the location where this script is located since it may have been run from any folder
scriptFolder=$(cd $(dirname "$0") && pwd)
# Also determine the script name, for example for usage and version.
# - this is useful to prevent issues if the script name has been changed by renaming the file
scriptName=$(basename $scriptFolder)
echo "scriptFolder: $scriptFolder"
echo "scriptName: $scriptName"
# output
$ bash test.sh
scriptFolder: /Users/src/shell
scriptName: test.sh
---------------------------------------------------------
# 마지막 작업의 종료 상태 확인
---------------------------------------------------------
#!/bin/bash
# The following will always fail because the file does not exist.
fileToRemove="/tmp/some-file-that-does-not-exist"
# Redirect error to /dev/null so script message is used instead.
rm "$fileToRemove" 2> /dev/null
if [ $? -eq 0 ]; then
echo "Success removing file: $fileToRemove"
else
echo "File does not exist: $fileToRemove"
fi
# output
$ bash test.sh
File does not exist: /tmp/some-file-that-does-not-exist
---------------------------------------------------------
# 학습자료들
---------------------------------------------------------
# 핵심 요약 자료
https://blog.gaerae.com/2015/01/bash-hello-world.html
http://programmingexamples.wikidot.com/bash-scripting
https://linuxconfig.org/bash-scripting-tutorial
# cheetsheet
https://devhints.io/bash
# tutorial
https://linuxhint.com/30_bash_script_examples/
https://linuxconfig.org/bash-scripting-tutorial
http://learn.openwaterfoundation.org/owf-learn-linux-shell/useful-scripts/useful-scripts/
# template
https://betterdev.blog/minimal-safe-bash-script-template/
https://github.com/ralish/bash-script-template/blob/main/template.sh
https://natelandau.com/boilerplate-shell-script-template/
https://jonlabelle.com/snippets/view/shell/another-bash-script-template
http://mars.tekkom.dk/w/index.php/Bash_script_template
https://dev.to/thiht/shell-scripts-matter
# example
https://www.tecmint.com/linux-server-health-monitoring-script/
# youtube
https://www.youtube.com/watch?v=RIKJVIcYNqE&list=PLYRfEO8DA8gikpkXw8wF8kJZDod2lFrML&index=1
https://www.youtube.com/watch?v=08XnZ8YA8MU&list=PLTJfebMjBKSLv0yQrgCOBLZtvBBD5lWqu&index=1
https://www.youtube.com/watch?v=035pZp2R50M
https://www.youtube.com/watch?v=RdY7wiCPN-Q
https://www.youtube.com/watch?v=GtovwKDemnI
https://www.youtube.com/watch?v=e7BufAVwDiM
https://www.youtube.com/watch?v=cQepf9fY6cE&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_
# 참고 자료
http://mug896.github.io/bash-shell/
https://linuxhint.com/category/bash-programming/
https://github.com/awesome-lists/awesome-bash
https://github.com/alebcay/awesome-shell
https://awesomerank.github.io/lists/alebcay/awesome-shell.html