셸 스 크 립 트 기초 지식 정리: 프로 세 스 제어 케이스

3494 단어 caselinux각본shell
케이스 사용 형식
case 값 in 모드 1) 명령 1 명령 2...;모드 2) 명령 1 명령 2...;escaecho '입력 1 에서 4 사이 의 숫자:' echo '입력 한 숫자 는' read aNumcase $aNum in1) echo '입 니 다.2) echo '당신 은 2 를 선 택 했 습 니 다';3) echo '당신 은 3 을 선 택 했 습 니 다';4) echo '당신 은 4 를 선 택 했 습 니 다';; *)echo '1 에서 4 사이 의 숫자 를 입력 하지 않 았 습 니 다';esac
실례 1
#!/bin/bash#case...esacecho "What is your preferred scripting language?"echo "1) bash"echo "2) prel"echo "3) python"echo "4) ruby"echo "5) I do not know !"read langcase $lang in1) echo "you selected bash";;2) echo "you selected prel";;3)echo "you selected python";;4)echo "you selected ruby";;5)echo "I do not know!";;esac
실행 결과
[root@localhost shell]# sh case.shWhat is your preferred scripting language?1) bash2) prel3) python4) ruby5) I do not know !4you selected ruby[root@localhost shell]# sh case.shWhat is your preferred scripting language?1) bash2) prel3) python4) ruby5) I do not know !5I do not know![root@localhost shell]#
실례 2
#!/bin/bashecho -n "Do you agree whith this? [yes or no]: "read yncase $yn in[Yy] | [Yy][Ee][Ss])echo "Agreed.";;[Nn] | [Nn][Oo])echo "Not Agreed."exit 1;;*)echo "Invalid input.";;esac
실행 결과:
[root@localhost shell]# sh case2.sh Do you agree whith this? [yes or no]: 1Invalid input.[root@localhost shell]# sh case2.sh Do you agree whith this? [yes or no]: nNot Agreed.[root@localhost shell]# sh case2.sh Do you agree whith this? [yes or no]: YesAgreed.[root@localhost shell]#
실례 3
#!/bin/bashcase $1 insql) echo "Runing mysql backup using mysqldump tool...";;sync) echo "Runing backup using rsyuc tool...";;git) echo "Runing backup using gistore tool...";;tar) echo "Runing tape backup using tar tool...";;*)echo "Backup shell script utility"echo "Usage: $0 {sql|sync|git|tar}"echo "sql : Run mysql backup utility."echo "sync : Run web backup utility."echo "git : Run gistore backup utility."echo "tar : Run tape backup utility.";;esac
실행 결과:
[root@localhost shell]# sh case3.shBackup shell script utilityUsage: case3.sh {sql|sync|git|tar}sql : Run mysql backup utility.sync : Run web backup utility.git : Run gistore backup utility.tar : Run tape backup utility.[root@localhost shell]# sh case3.sh sqlRuning mysql backup using mysqldump tool...[root@localhost shell]#
실례 4
#!/빈/bash\# 디스크 사용률 이 가장 높 은 maxusage=$(df -Ph |awk '{print $5}'|grep %|grep -v "Use"|sort -n|tail -1|cut -d "%"-f1)max_dir= df -Ph | awk '{print $5,$6}'|grep -v Use|sort -n|tail -1|awk '{print $2}' case ${max_usage} in[1-6])MSG="All is quiet.\"${max_dir}\"is ${max_usage}% used";;[7-8])MSG="Start thinking about cleaning out some stuff. "MSG="$MSG There's a partition ${max_dir} is ${max_usage}% used.";;9[1-8])MSG="Better hurry with that new disk... "MSG="$MSG One partition ${max_dir} is ${max_usage}% used.";;99)MSG="I'm drowning herel There's a partiton at ${max_usage}% used";;)MSG="I seem to be runing with an nonexitent amount of disk space...";;esac#echo $MSG| mail -s "disk report date "rootecho $MSG
실행 결과:
[root@localhost shell]# sh case4.shAll is quiet."/"is 22% used[root@localhost shell]#

좋은 웹페이지 즐겨찾기