新北市教師工作坊 -- Bash script programming 介紹

Embed Size (px)

DESCRIPTION

2014/06/24 在新北市蘆洲國中的教師工作坊,介紹 bash script programming 基礎

Citation preview

  • 1. Bash Script Franklin 2014/06/24

2. Shell? Shell Core User Shell 3. Shell PC-DOS, MS-DOS Windows Cmd.exe Sh / csh / bash / zsh / ... Shell Gnome shell 4. /bin/sh : bash vs dash Ubuntu /bin/sh dash dash bash 5. Shell script Shebang: #! shell interpreter #!/bin/sh #!/bin/bash #!/bin/python #!/usr/bin/env python ... POSIX: the results are unspecified. /bin/sh 6. Hello World #!/bin/bash echo "Hello World!" echo "Today is `date +%x`" echo 'Another example is date +%x' echo, ", ', ` 7. #!/bin/bash MONTH=`date +%m` DATE=`date +%d` echo " $MONTH $DATE " echo " ${MONTH} ${DATE} " $MONTH ${MONTH} 8. #!/bin/bash STR="A B C D" echo $STR echo "$STR" 9. Exercise 1 xx xx man date 10. 1 #!/bin/bash HOUR=`date +%H` MIN=`date +%M` echo " ${HOUR} ${MIN} " 11. test #!/bin/bash DAY=`date +%d` if [ ${DAY} -eq 5 ]; then echo " " elif [ ${DAY} -ge 28 ]; then echo " ..." else echo " " fi test , [ ], if, elif, then, -eq, -ge test if, while if 12. test ( ) : true, ! : not -a : and, -o : or -eq, -gt, -lt, -ge, -le, -ne =, != = -eq -z, -n -b, -c, -d, -e, -f, -r, -w, -x man test 13. while, until #!/bin/bash i=0 while [ $i -lt 10 ]; do echo "i=$i" let i+=1 done #!/bin/bash i=0 until [ $i -eq 10 ]; do echo "i=$i" let i+=1 done while, until 14. for #!/bin/bash for FILENAME in /usr; do echo "$FILENAME" done #!/bin/bash for FILENAME in /usr/*; do echo "$FILENAME" done for 15. Exercise 2 /usr/lib man test for let 16. 2 #!/bin/bash DIR=0 SYMLINK=0 EXEC=0 OTHER=0 for FILE in /usr/lib/*; do if [ -d $FILE ]; then let DIR+=1 elif [ -x $FILE ]; then let EXEC+=1 elif [ -L $FILE ]; then let SYMLINK+=1 else let OTHER+=1 fi done echo "Dir:$DIR, Exec: $EXEC, SYMLINK: $SYMLINK, OTHER:$OTHER" 17. Bash Script #!/bin/bash echo "0=$0, 1=$1, 2=$2" if [ $1 = "Good" ]; then echo "Thanks." else echo "Oops..." fi $0, $1, $2 $0: $1: ./test.sh Good ya ./test.sh 18. Bash Script #!/bin/bash echo "0=$0, 1=$1, 2=$2" if [ "$1" = "Good" ]; then echo "Thanks." else echo "Oops..." fi Bash $1 parsing error 19. Bash Script 2 #!/bin/bash echo "0=$0, 1=$1, 2=$2" if [ -z "$1" ]; then echo "No parameters." elif [ "$1" = "Good" ]; then echo "Thanks." else echo "Oops..." fi 20. Bash Script #!/bin/bash ls -lR /usr > /tmp/usr.list 2>&1 RET=$? if [ $RET -ne 0 ]; then echo "ls failed." fi >, >&, $? $?: 21. > : (stdout) ls -lR /usr > /tmp/file.list >> : ls -lR /var >> /tmp/file.list >& : fd ls -lR /home >> /tmp/file.list 2>&1 fd: 0 stdin, 1 stdout, 2 stderr | : pipe ls -l / | grep home sed, awk, grep 22. Exercise 3 wget ftp://goodhorse.idv.tw/tetris.sh wget, $? man wget 23. 3 #!/bin/bash wget ftp://goodhorse.idv.tw/tetris.sh RET=$? if [ $RET -ne 0 ]; then echo "wget failed." fi 24. Bash Script #!/bin/bash printValue() { if [ -z "$1" ]; then echo "Empty value" else echo "parameter=$1" fi } printValue i=0 until [ $i -eq 10 ]; do printValue $i let i+=1 done $1, $2 25. Source source alias: . . /etc/default/locale 26. #!/bin/bash declare -i NUM NUM=4*2 echo $NUM declare bash declare -i: -a | -A : -f : +i 27. #!/bin/bash echo -n " " read NAME echo "Hello, $NAME." read read NAME $NAME 28. Shell Script grep sed awk zenity / kdialog 29. grep ps axw | grep ba.h dmesg | grep eth0 -r grep -r bash * -v: 30. awk ps axw | awk ' { print $1 " " $5 }; ' ps axw | awk ' { if ($1 >= 4000 && $1 = 80) { print $1 " " $5 " " $6 }} ' 39. 3: ppp0 #!/bin/bash CHECK=`/sbin/ifconfig ppp0 | grep "inet addr"` if [ -z "${CHECK}" ]; then /etc/init.d/network-manager restart fi if [ "${CHECK}z" = "z" ]; then 40.