Bourne Shell及shell编程

Similar documents
Microsoft Word - linux命令及建议.doc

C/C++ - 字符输入输出和字符确认

投影片 1

ebook35-2

像 客 样 使 命令行 徐 东

ebook70-5

PowerPoint 演示文稿

本文由筱驀釹贡献

I B :

Unix®t Œ fi z.PDF

Microsoft Word - PS2_linux_guide_cn.doc

ebook15-C

WWW PHP

ebook 99-1

C/C++ - 函数

LSC操作说明

数据结构与算法 - Python基础

電子商業伺服器管理(終極版).doc

Learn_Perl 3-02.pdf

Microsoft Word - 第3章.doc

Microsoft PowerPoint ppt [相容模式]

Microsoft Word - 國文.doc

四川省普通高等学校

CC213

ebook8-30

资源管理软件TORQUE与作业调度软件Maui的安装、设置及使用

國 立 政 治 大 學 教 育 學 系 2016 新 生 入 學 手 冊 目 錄 表 11 國 立 政 治 大 學 教 育 學 系 博 士 班 資 格 考 試 抵 免 申 請 表 論 文 題 目 申 報 暨 指 導 教 授 表 12 國 立 政 治 大 學 碩 博 士 班 論

Linux操作系统使用

软件测试(TA07)第一学期考试

科学计算的语言-FORTRAN95

epub 63-3

P4i45GL_GV-R50-CN.p65

PTS7_Manual.PDF

untitled

C/C++语言 - 分支结构

ebook70-14

GoFlex Home UG Book.book

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

292 shell shell C shell ~/.cshrc shell ~/.login ~/.logout ~/.history.cshrc shell shell /etc/passwd ~name NIS NIS+ * 0? [abc...] a-za-z0-9

TX-NR3030_BAS_Cs_ indd

WinMDI 28

Perl

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

1.ai

FY.DOC

C/C++语言 - 运算符、表达式和语句

Microsoft Word - template.doc

Windows XP

C/C++程序设计 - 字符串与格式化输入/输出

untitled

技 巧 5: 避 免 除 以 0 的 運 算 在 做 除 的 運 算 時, 先 檢 查 除 數 的 數 值, 避 免 有 除 以 0 的 情 況 若 運 算 中 除 數 為 0,SAS 會 在 LOG 中 註 記 提 醒 並 將 運 算 結 果 設 定 為 遺 漏 值, 減 慢 程 式 的 執 行

User ID 150 Password - User ID 150 Password Mon- Cam-- Invalid Terminal Mode No User Terminal Mode No User Mon- Cam-- 2

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

Microsoft Word - SupplyIT manual 3_cn_david.doc

概述

Microsoft Word - OPIGIMAC 譯本.doc

C/C++ - 文件IO

Microsoft Word - Final Exam Review Packet.docx

C/C++ - 字符串与字符串函数

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

Junos Pulse Mobile Security R1 2012, Juniper Networks, Inc.

教學計畫書

C/C++ 语言 - 循环

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

ebook14-4

CAUTION RISK OF ELECTRIC SHOCK DO NOT OPEN 2

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

Guide to Install SATA Hard Disks

Cygwin & vim

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Microsoft Word - 09.數學 docx

els0xu_zh_nf_v8.book Page Wednesday, June, 009 9:5 AM ELS-0/0C.8

C语言的应用.PDF

電腦做什麼事~第七章

Microsoft Word - 小心翼翼的二十一點N.doc

IP505SM_manual_cn.doc

ebook140-8

Microsoft Word - 01.DOC

PowerPoint 簡報

ebook65-5

プログラムの設計と実現II

Transcription:

Bourne Shell shell Altmayer.bbs@altmayer.dhs.org javalee LINUX hbwork@dlut.edu.cn, April 1999. URL: ftp://ftp.dlut.edu.cn/pub/people/albin/ : ------------------------------------------------------------------------------ Bourne Shell Bourne Shell shell.shell.shell (shell ).shell script.shell 1.shell Stephen Bourne Bell man sh UNIX (1)shell /etc/passwd $ /etc/prole $HOME/.prole (2)shell -n shell script shell script -x -a Tag all variables for export -c "string" strings -e -f shell 1

-h locate and remember functions as dend -i -k -r -s -t (shell exits) -u -v verbose, shell -e -i. (3) shell(restircted Shell) sh -r /bin/rsh cd, PATH, rsh sh,rsh vi more shell, rsh (4)set shell $ set shell - + UNIX a,e,f,h,k,n,u,v x / set -xv set -tu ; echo $- shell (5).prole PATH=$PATH:/usr/loacl/bin; export PATH (6)shell CDPATH cd HOME IFS /etc/passwd Internal Field Separator, tab 2

MAIL /var/mail/$username mail PATH PS1 PS2 ($) (> ) TERM vt100,ansi,vt200,xterm $PS1="test:";export PS1 test: PS1="\$";export PS1 $echo $MAIL /var/mail/username (7) $ shell $var # & csh` * 0 (DOS.) $- set shell $! $# shell script $* shell script $@ $? $0 shell $n (n:1-) $$ (Process Identier Number, PID) >le <le `` >>ile lename=`basename /usr/local/bin/t append $echo "$HOME $PATH" /home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/x11r6 /bin: $echo '$HOME $PATH' $HOME $PATH $echo \$HOME $PATH $HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/x11r6/bin:/h ome/hbw ork/bin $dir=ls 3

$$dir $alias dir ls $dir ls > lelist ls >> lelist wc -l < lelist wc -l lelist sleep 5; echo 5 seconds reaches; ls -l ps ax egrep inetd nd / -name core -exec rm {} \; & lename=`date "+%Y%m%d"`.log 2. shell $HOME,cd $HOME, Bourne Shell.. shell script.. ( shell ) (1) $ COUNT=1 $ NAME="He Binwu" UNIX shell $ $ echo $HOME /home/hbwork $ WEEK=Satur $ echo Today is $WEEKday Today is $echo Today is ${WEEK}day Today is Saturday Shell (Linux Shell/bash!) $ X=$Y Y=y $ echo $X 4

y $ Z=z Y=$Z $ echo $Y $ unset $ Z=hello $ echo $Z hello $ unset Z $ echo $Z $ Bourne Shell. Bourne Shell ${variable:-defaultvalue} $ echo Hello $UNAME Hello $ echo Hello ${UNAME:-there} Hello there $ echo $UNAME # $ UNAME=hbwork $ echo Hello ${UNAME:-there} Hello hbwork $. ${variable:=value} $ echo Hello $UNAME Hello $ echo Hello ${UNAME:=there} Hello there 5

$ echo $UNAME # there $. $USERDIR=${$MYDIR:-`pwd`}. ${variable:+value}. ${variable:?value} $ UNAME= $ echo ${UNAME:?"UNAME has not been set"} UNAME: UNAME has not been set $ echo ${UNAME:?} UNAME: parameter null or not set (2) (Shell ) shell script $1..$9 $0. reanly variable. export shell shell shell subshell. Bourne Sh ell shell export subshell PS1 export shell $ PS1=`hostname`$ peony$sh $ echo $PS1 $ <- $ exit peony$export PS1 peony$sh peony$ echo $PS1 peony$ <- peony$ 3.Shell Script UNIX 6

(1) Shell $ls -R / grep myname more $ cd /usr/yourname; ls * cpio -o > /dev/rmt/0h $ cat > backup.sh cd /home/hbwork ls * cpio -o > /dev/rmt/0h ^D $ sh backup.sh $ chmod +x backup.sh $./backup.sh shell script (2)shell shell sh ell script. shell back up.sh cd $WORKDIR ls * cpio -o > /dev/rmt/0h. Shell #. shell expr expr integer operator integer operator + - * / %, * \, $expr 4 \* 5 20 $int=`expr 5 + 7` $echo $int 12 7

(3)Shell, (read) restoreall.sh backup.sh $cat > restoreall.sh cd $WORKDIR cpio -i < /dev/rmt/0h ^D restore1.sh: #restore1 --program to restore a single le cd $WORKDIR cpio -i $i < /dev/rmt/0h $restore1 le1 restoreany : #restoreany --program to restore a single le cd $WORKDIR cpio -i $* < /dev/rmt/0h $ restoreany le1 le2 le3 (4). if-, : if _1 _4 _2 _3 if- $?, _1 _2 _3,_4. unload:, cd $1 #! ls -a cpio -o > /dev/rmt/0h rm -rf * : 8

#, if ls -a cpio -o > /dev/rmt/0h rm -rf *. if--else if _1 _2 else _3 : shell,.,. test : test conditions test : a. b. c., d., and/or, a. : shell str1 = str2 str1!= str2, -n string string ( ) -z string string string string : $ str1=abcd # $ test $str1=abcd $ echo $? 0 $ str1="abcd " $ test $str1=abcd $ echo $? 1 9

Note: test,, shell, shell. : $ str1=" " $ test $str1 $ echo $? 1 $ test "$str1" $ echo $? 0 $ test -n $str1 test: argument expected $ test -n "$str1" $ echo $? 0 $ b. : test expr,,expr,test. --------------------------------------- int1 -eq int2 int1 -ne int2?? int1 -gt int2 int1 > int2? int1 -ge int2 int1 >= int2? int1 -lt int2 int1 < int2? int1 -le int2 int1 <= int2? : $ int1=1234 $ int2=01234 $ test $int1 -eq $int2 $ echo $? 0 c. : -r lename lename? 10

-w lename lename? -x lename lename? -f lename lename? -d lename lename? -c lename lename? -b lename lename? -s lename lename? -t fnumb fnumb( 1)? d.,! : $ cat /dev/null > empty $ test -r empty $ echo $? 0 $ test -s empty 1 $ test! -s empty $ echo $? 0 e. -a And -o Or : $ test -r empty -a -s empty $ echo $? 1 f. test test shell, shell, Bourne Shell test : test : $ int1=4 $ [ $int1 -gt 2 ] $ echo $? 0 : unload, test #!/bin/sh 11

#unload - program to backup and remove les #syntax: unload directory #check arguments if [ $# -ne 1 ] echo "usage: $0 directory" exit 1 #check for valid directory name if [! -d "$1" ] echo "$1 is not a directory" exit 2 cd $1 ls -a cpio -o > /dev/rmt/0h if [ $? -eq 0 ] rm -rf * else echo "A problem has occured in creating backup" echo "The directory will not be ereased" echo "Please check the backup device" exit 3 # end of unload exit, exit :, g. if elif if else if 12

else if : elif if elif elif elif,,. /******** *********/ ------------------------------------------------------------------------------ : Bourne Shell Shell (2) h. read read var1 var2... varn read $ read var1 var2 var3 Hello my friends 13

$ echo $var1 $var2 $var3 Hello my friends $ echo $var1 Hello $ read var1 var2 var3 Hello my dear friends $ echo $var3 dear friends $ read var1 var2 var3 Hello friends $ echo $var3 $ <- <- var3 shell script read... #echo -n message echo -n "Do you want to continue: Y or N" read ANSWER if [ $ANSWER=N -o $ANSWER=n ] exit i. case elif- if- if [ variable1 = value1 ] elif [ variable1 = value2 ] elif [ variable1 = value3 ] 14

case case value in pattern1) ;; pattern2) ;;... patternn) ; esac * case case shell script #Display a menu echo _ echo "1 Restore" echo "2 Backup" echo "3 Unload" echo #Read and excute the user's selection echo -n "Enter Choice:" read CHOICE case "$CHOICE" in 1) echo "Restore";; 2) echo "Backup";; 3) echo "Unload";; *) echo "Sorry $CHOICE is not a valid choice exit 1 esac * case pattern1 pattern2 ) ;; 15

case "$CHOICE" in 1 R) echo "Restore";; 2 B) echo "Backup";; 3 U) echo "Unload";; *) echo "Sorry $CHOICE is not a valid choice esac exit 1 (5) <1> while : while ne... 1 5 #!/bin/sh # #Filename: square.sh int=1 while [ $int -le 5 ] sq=`expr $int \* $int` echo $sq int=`expr $int + 1` ne echo "Job completed" $ sh square.sh 1 4 9 16 25 16

Job completed <2> until until... ne until 1-5 #!/bin/sh int=1 until [ $int -gt 5 ] sq=`expr $int \* $int` echo $sq int=`expr $int + 1` ne echo "Job completed" <3> shift $* shell script shift shift $2->$1, $3->$2. $1 #!/bin/sh # #Filename: shifter 17

main" until [ $# -eq 0 ] echo "Argument is $1 and `expr $# - 1` argument(s) re shift ne $ shifter 1 2 3 4 Argument is 1 and 3 argument(s) remain Argument is 2 and 2 argument(s) remain Argument is 3 and 1 argument(s) remain Argument is 4 and 0 argument(s) remain $ shift $#1 u ntil shell script: #!/bin/sh # sumints - a program to sum a series of integers # if [ $# -eq 0 ] echo "Usage: sumints integer list" exit 1 sum=0 until [ $# -eq 0 ] sum=`expr $sum + $1` shift ne echo $sum $ sh sumints 324 34 34 12 34 438 $ 18

shift Bourne Shell $1~$9, 9 9 9 shift. shift shift 3 <4>. for for var in arg1 arg2... argn... ne $ for letter in a b c d e; echo $letter;ne a b c d e $ for i in * if [ -f $i ] echo "$i is a le" elif [ -d $i ] echo "$i is a directory" ne #!/bin/sh 19

sum=0 for INT in $* sum=`expr $sum + $INT` ne echo $sum <6> break continue break continue shell break continue true false UNIX true 0 false # break while true ne... until false, until false... ne shell script continue,break case #!/bin/sh 20

# Interactive program to restore, backup, or unload # a directory echo "Welcome to the menu driven Archive program" while true # Display a Menu echo echo "Make a Choice from the Menu below" echo _ echo "1 Restore Archive" echo "2 Backup directory" echo "3 Unload directory" echo "4 Quit" echo # Read the user's selection echo -n "Enter Choice: " read CHOICE ry case $CHOICE in [1-3] ) echo # Read and validate the name of the directo echo -n "What directory you want? " read WORKDIR y" if [! -d "$WORKDIR" ] echo "Sorry, $WORKDIR is not a director continue ectory # Make the directory the current working dir cd $WORKDIR;; 4) :;; # : *) echo "Sorry, $CHOICE is not a valid choice" 21

esac continue case "$CHOICE" in 1) echo "Restoring..." cpio -i </dev/rmt/0h;; 2) echo "Archiving..." ls cpio -o >/dev/rmt/0h;; 3) echo "Unloading..." ls cpio -o >/dev/rmt/0h;; esac 4) echo "Quitting" break;; #Check for cpio errors if [ $? -ne 0 ] echo "A problem has occurred during the process" if [ $CHOICE = 3 ] echo "The directory will not be erased" echo "Please check the device and try again" continue else if [ $CHOICE = 3 ] rm * ne (6) shell (subroutine), 22

funcname() {... ; # } shell # start program setup () { list ; } _data () { list ; } cleanup () { list ; } errors () { list ; } setup _data cleanup # end program.. ( ), iscontinue() 23

{ while true echo -n "Continue?(Y/N)" read ANSWER } ne case $ANSWER in [Yy]) return 0;; [Nn]) return 1;; *) echo "Answer Y or N";; esac shell iscontinue if iscontinue continue else break ** shell shell she ll sh ell shell shell shell shell( shell) Z $ dir dir: not found $ dir () { ls -l ;} $ dir total 5875 -rw-r--r-- 1 hbwork 100 Nov 10 23:16 c -rw-r--r-- 1 hbwork 2973806 Nov 10 23:47 ns40cs.zip -rw-r--r-- 1 hbwork 1715011 Nov 10 23:30 ns840usr.pdf -rw-r--r-- 1 hbwork 1273409 Sep 23 1998 radsol21b6.tar. -rw-r--r-- 1 hbwork 7526 Nov 10 23:47 wget-log 24

-rw-r--r-- 1 hbwork 1748 Nov 13 21:51 wget-log.1 $ unset dir $ dir () { > echo "Permission Link Owner Group File_SZ LastAccess FileName" > echo "-----------------------------------------------------------" > ls -l $*; > } ame $ dir Permission Link Owner Group File_SZ LastAccess FileN ----------------------------------------------------------- total 5875 -rw-r--r-- 1 hbwork 100 Nov 10 23:16 c -rw-r--r-- 1 hbwork 2973806 Nov 10 23:47 ns40cs.zip -rw-r--r-- 1 hbwork 1715011 Nov 10 23:30 ns840usr.pdf ar.z -rw-r--r-- 1 hbwork 1273409 Sep 23 1998 radsol21b6.t -rw-r--r-- 1 hbwork 7526 Nov 10 23:47 wget-log -rw-r--r-- 1 hbwork 1748 Nov 13 21:51 wget-log.1 shell script shell shell shell (.) shell shell shell shell script, (.).prole login $..prole (csh. source ). (7) And/Or <1> And,, 1 && 2 25

rm $TEMPDIR/* && echo "File successfully removed" if rm $TEMPDIR/* echo "File successfully removed" <2>Or, AND rm $TEMPDIR/* echo "File not removed" if rm $TEMPDIR/* else echo "File not removed" <3> 1 && 2 && 3 1, 2 3 1 && 2 comamnd3 1 2 3 (8) getopts unix UNIX -options parameters getopts option_string variable getopts (man getopts). 26

#newdate if [ $# -lt 1 ] date else while getopts mdydhmstjjwahr OPTION case $OPTION in m) date '+%m ';; # Month of Year d) date '+%d ';; # Day of Month y) date '+%y ';; # Year D) date '+%D ';; # MM/DD/YY H) date '+%H ';; # Hour M) date '+%M ';; # Minute S) date '+%S ';; # Second T) date '+%T ';; # HH:MM:SS j) date '+%j ';; # day of year J) date '+%y%j ';;# 5 digit Julian date w) date '+%w ';; # Day of the Week a) date '+%a ';; # Day abbreviation h) date '+%h ';; # Month abbreviation r) date '+%r ';; # AM-PM time \?) echo "Invalid option $OPTION";; esac ne $ newdate -J 94031 $ newdate -a -h -d Mon Jan 31 $ newdate -ahd Mon Jan 31 $ # Syntax: duplicate [-c integer] [-v] lename 27

# where integer is the number of duplicate copies # and -v is the verbose option COPIES=1 VERBOSE=N while getopts vc: OPTION case $OPTION in c) COPIES=$OPTARG;; v) VERBOSE=Y;; \?) echo "Illegal Option" exit 1;; esac ne if [ $OPTIND -gt $# ] echo "No le name specied" exit 2 shift `expr $OPTIND -1` FILE=$1 COPY=0 while [ $COPIES -gt $COPY ] COPY=`expr $COPY + 1` cp $FILE ${FILE}${COPY} if [ VERBOSE = Y ] echo ${FILE}${COPY} ne $ duplicate -v lea lea1 $ duplicate -c 3 -v leb 28

leb1 leb2 leb3 4. Shell shell shell ( ) (1) shell shell shell shell. IFS shell IFS $ IFS=":" $ echo:hello:my:friend Hello my Friend (2) #Directory and Prompt change program #Syntax: chdir directory if [! -d "$1" ] echo "$1 is not a directory" exit 1 cd $1 PS1=`pwd`$ export PS1 $ chdir /usr/home/teresa $ 29

shell shell shell.prole.persfuncs #Personal function le persfuncs chdir() { #Directory and Prompt change program #Syntax: chdir directory if [! -d "$1" ] echo "$1 is not a directory" exit 1 cd $1 PS1=`pwd`$ export PS1; } $..persfuncs $ chdir temp /home/hbbwork/temp$.prole..persfuncs.persfuncs. bash/tcsh 5. shell (1)shell -x (2) 30

shell {com mand_list;} shell shell shel l shell $ NUMBER=2 $ (A=2;B=2;NUMBER=`expr $A + $B`; echo $NUMBER) 4 $ echo $NUMBER 2 $ { A=2;B=2;NUMBER=`expr $A + $B`; echo $NUMBER; } 4 $ echo $NUMBER 4 Bourne Shell, shell shell script Korn Shell, csh script OS script UNIX SHELL script shell script ( ) shell. PERL script PERL shell, awk, sed, grep Perl By Examples" PERL 31