리눅스 서버를 자동화하여 관리하기 위한 쉘 스크립트(Shell Script) 심화 강좌를 정리합니다.
출처 : inflearn
1. 배열
배열은 쉼표가 아닌 공백으로 분리 해야 함.
linux:/home/shkim$ declare -a array1=("water" "blue" "super")
linux:/home/shkim$ declare -a array2=("melon" "mountain" "stars")
linux:/home/shkim$ for i in "${!array1[@]}"; do
> printf "%s\t%s\t%s\n" "$i" "${array1[$i]}" "${array2[$i]}"
> done
0 water melon
1 blue mountain
2 super stars
linux:/home/shkim$
2. 배열과 glob 그리고 루프문
linux:/home/shkim$ ARRAY=( "sky:blue" "snow:white" "night:black" "apple:red" )
linux:/home/shkim$ for object in "${ARRAY[@]}" ; do
> KEY=${object%%:*}
> VALUE=${object#*:}
> printf "%s's color is %s.\n" "$KEY" "$VALUE"
> done
sky's color is blue.
snow's color is white.
night's color is black.
apple's color is red.
linux:/home/shkim$
linux:/home/shkim$ files=$(ls) # bad , 단순 문자열
linux:/home/shkim$ files=($(ls)) # bad, 배열의 형태
linux:/home/shkim$ files=(*) # good, 파일 목록을 가져올때 glob 를 이용해야 한다.
실습(DRILL)
- 스크립트 실행시 파일이 생성되어야 하고, 파일의 내용은 아래와 같아야함.
- hint : read, for loop, 배열, IFS 활용
linux:/home/shkim$ cat tech.txt
youtube,ai,alphago,arduino,IOT
linux:/home/shkim$ ./makeTagDirectory.sh tech.txt
linux:/home/shkim$ ll youtube.md ai.md alphago.md arduino.md IOT.md # 파일이 생성되도록 작성
linux:/home/shkim$ cat ai.md
---
name: ai
title: 'ai'
---
정답
#!/bin/bash
IFS=$',' read -r -a array < $1
for element in "${array[@]}" ;do
echo "----" > "$element.md"
echo "name: $element" >> "$element.md"
echo "title: '$element'" >> "$element.md"
echo "----" >> "$element.md"
done
3. find 와 print0
linux:/home/shkim/b$ ll
total 0
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 For Whom the Bell Tolls.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 Gone With the Wind.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 old man and ?the sea.mp3
# 하기 명령어로 mp3 파일이 삭제될거라고 생각했지만 삭제되지 않음.
linux:/home/shkim/b$ find . -name "*.mp3" | xargs rm -rf
linux:/home/shkim/b$ ll
total 0
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 For Whom the Bell Tolls.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 Gone With the Wind.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 old man and ?the sea.mp3
# 삭제되지 않은 이유 : 파일의 공백이 포함되어 있고 줄바꿈 문자가 포함되어 있음.
linux:/home/shkim/b$ find . -name "*.mp3" | xargs ls -al
ls: cannot access ./For: No such file or directory
ls: cannot access Whom: No such file or directory
ls: cannot access the: No such file or directory
ls: cannot access Bell: No such file or directory
ls: cannot access Tolls.mp3: No such file or directory
ls: cannot access ./old: No such file or directory
ls: cannot access man: No such file or directory
ls: cannot access and: No such file or directory
ls: cannot access ?the: No such file or directory
ls: cannot access sea.mp3: No such file or directory
ls: cannot access ./Gone: No such file or directory
ls: cannot access With: No such file or directory
ls: cannot access the: No such file or directory
ls: cannot access Wind.mp3: No such file or directory
# 파일 hexdump / 20: 공백, 0a: 줄바꿈
linux:/home/shkim/b$ find . -iname "*.mp3" | hexdump -C
00000000 2e 2f 46 6f 72 20 57 68 6f 6d 20 74 68 65 20 42 |./For Whom the B|
00000010 65 6c 6c 20 54 6f 6c 6c 73 2e 6d 70 33 0a 2e 2f |ell Tolls.mp3../|
00000020 6f 6c 64 20 6d 61 6e 20 61 6e 64 20 3f 74 68 65 |old man and ?the|
00000030 20 73 65 61 2e 6d 70 33 0a 2e 2f 47 6f 6e 65 20 | sea.mp3../Gone |
00000040 57 69 74 68 20 74 68 65 20 57 69 6e 64 2e 6d 70 |With the Wind.mp|
00000050 33 0a |3.|
00000052
# print0 은 find 명령어에 의해 검색된 모든 검색 결과의 마지막에 널문자를 넣어줌
# 널문자 00 으로 파일의 시작과 끝을 알 수 있음.
linux:/home/shkim/b$ find . -iname "*.mp3" -print0 | hexdump -C
00000000 2e 2f 46 6f 72 20 57 68 6f 6d 20 74 68 65 20 42 |./For Whom the B|
00000010 65 6c 6c 20 54 6f 6c 6c 73 2e 6d 70 33 00 2e 2f |ell Tolls.mp3../|
00000020 6f 6c 64 20 6d 61 6e 20 61 6e 64 20 3f 74 68 65 |old man and ?the|
00000030 20 73 65 61 2e 6d 70 33 00 2e 2f 47 6f 6e 65 20 | sea.mp3../Gone |
00000040 57 69 74 68 20 74 68 65 20 57 69 6e 64 2e 6d 70 |With the Wind.mp|
00000050 33 00 |3.|
00000052
# xargs 뒤에 -0 은 문자열을 널문자로 분리하려면 필수적으로 써야하는 옵션임.
linux:/home/shkim/b$ find . -iname "*.mp3" -print0 | xargs -0 ls -al
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 ./For Whom the Bell Tolls.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 ./Gone With the Wind.mp3
-rw-rw-r-- 1 shkim shkim 0 Nov 19 00:35 ./old man and ?the sea.mp3
# 정상적으로 삭제됨.
linux:/home/shkim/b$ find . -name "*.mp3" -print0 | xargs -0 rm -rf
linux:/home/shkim/b$ ls
4. 명령어(find)
linux:/home/shkim/shell_cmd$ find ./ -maxdepth 1 -iname '*.sh'
./Anaconda2-2.4.1-MacOSX-x86_64.sh
./case.sh
./debug.sh
./euid.sh
./fnum.sh
./goodday.sh
./helloworld.sh
./locale.sh
./makeTagDirectory.sh
./mydaemon.sh
./mydate.sh
./myscript.sh
./mysleep.sh
./rename.sh
./sleep.sh
./watch.sh
./whereis.sh
./whois.sh
./select.sh
linux:/home/shkim/shell_cmd$
linux:/home/shkim/shell_cmd$ find ./ -maxdepth 2 -iname '*.sh'
./Anaconda2-2.4.1-MacOSX-x86_64.sh
./case.sh
./debug.sh
./euid.sh
./fnum.sh
./goodday.sh
./helloworld.sh
./locale.sh
./makeTagDirectory.sh
./md5sum/helloworld.sh
./md5sum/helloworld_md5sum.sh
./mydaemon.sh
./mydate.sh
./myscript.sh
./mysleep.sh
./rename.sh
./sleep.sh
./watch.sh
./whereis.sh
./whois.sh
./select.sh
linux:/home/shkim/shell_cmd$
# " " 인용부호 적는 습관 중요.
# 파일 삭제시 ls -l 대신 rm -rf 사용(rm 은 복구가 되지 않으므로 신중하게 써야 한다.)
linux:/home/shkim/shell_cmd$ find ./ -name "*.bak" -exec ls -l {} \;
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/Balloon.jpg.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/Balloon.jpg_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/Candy.jpg.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/Candy.jpg_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/glob.gif.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/glob.gif_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/settings_down.png.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/settings_down.png_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/settings_up.png.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/settings_up.png_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/shadingimage.tiff.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/shadingimage.tiff_backup.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/smaller.tiff.bak
-rw-rw-r-- 1 shkim shkim 0 Oct 17 21:36 ./images/drill/smaller.tiff_backup.bak
linux:/home/shkim/shell_cmd$
#디렉토리만 출력
linux:/home/shkim/shell_cmd$ find ./ -type d
./
./csv
./image4
./imagemagick
./images
./images/drill
./images_mirror
./md5sum
./mydir2
./quiz
linux:/home/shkim/shell_cmd$
# 홈 디렉토리내 숨긴파일
linux:/home/shkim/shell_cmd$ find ~/ -maxdepth 1 -name ".*"
/home/shkim/.bash_logout
/home/shkim/.bashrc
/home/shkim/.bash_history
/home/shkim/.mysql_history
/home/shkim/.bash_profile
/home/shkim/.vimrc
/home/shkim/.viminfo
/home/shkim/.ssh
/home/shkim/.config
linux:/home/shkim/shell_cmd$
# -a : and , 퍼미션 644 인 파일 출력 ??
linux:/home/shkim/shell_cmd$ find ./ \( -user shkim -a -perm 644 \) -print | xargs ls -al
total 228
drwxrwxr-x 10 shkim shkim 4096 Oct 17 22:59 .
drwx------ 16 shkim shkim 4096 Dec 27 14:39 ..
-rw-rw-r-- 1 shkim shkim 29 Sep 1 2020 ai.md
-rw-rw-r-- 1 shkim shkim 39 Sep 1 2020 alphago.md
-rwxr-xr-x 1 shkim shkim 15931 Sep 1 2020 Anaconda2-2.4.1-MacOSX-x86_64.sh
-rw-rw-r-- 1 shkim shkim 39 Sep 1 2020 arduino.md
-rwxr-xr-x 1 shkim shkim 154 Sep 1 2020 case.sh
drwxrwxr-x 2 shkim shkim 284 Sep 1 2020 csv
-rwxr-xr-x 1 shkim shkim 325 Sep 1 2020 debug.sh
-rw-rw-r-- 1 shkim shkim 106 Sep 1 2020 err.log
-rwxr-xr-x 1 shkim shkim 83 Sep 1 2020 euid.sh
-rw-rw-r-- 1 shkim shkim 10 Sep 1 2020 file
PREVIOUS섹션 5. 비교와 루프문
NEXT섹션 7. 입출력과 환경변수