1 / 43

# 第十五章 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about '第十五章' - langer

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

shell 编程初步

§15.1shell脚本中的参数传递

§15.2算术运算expr命令

§15.3条件执行

§15.4条件测试命令

§15.5使用if结构

§ 15.7循环结构

§ 15.8shell内部命令

§ 15.9 软中断

§ 15.10 综合示例

shell 编程初步 (2)

15.1 shell 脚本中的参数传递

shell变量分类：

(1) 环境变量

(2)预定义变量

(3)自定义变量

shell预定义变量

\$\$

\$0

\$#

\$*

\$?

\$!

Shell脚本名

\$1，\$2, \$3, . . . , \$9

\${10}, \${11}, . . . , \${n} (仅适用于Korn shell )

\$ cat ascript

echo First Parameter entered was \$1

echo Second Parameter entered was \$2

echo Third Parameter entered was \$3

\$ ascript Good Day Sydney

First Parameter entered was Good

Second Parameter entered was Day

Third Parameter entered was Sydney

15.2 算术运算expr 命令

expr命令提供以下的运算操作符

\*

/

%

+

-

expr 命令(2)

\$ var1 = 6

\$ var2 = 3

\$ expr \$var1 / \$var2

2

\$ expr \$var1 - \$var2

3

expr 命令(3)

\$ expr \( \$var1 + \$var2 \) \* 5

45

\$ var3 = \$(expr \$var1 / \$var2 )

\$ echo \$var3

2

15.3 条件执行

1.与运算

commmand1 && command2 (command1成功则执行command2)

\$ ls s* && rm s*

2. 或运算

command1 || command2（命令1不成功则执行命令2）

\$ cd /dir1 || echo Cannot change to dir1

&& 上一命令正确执行，退出值为0时才执行下一个命令

|| 上一命令执行不成功，退出值为非0时执行下一命令

15.4 条件测试命令

test expression

[ expression ]

[ [ expression ] ]

test多用于if和while控制结构中。

Strings are equal

Strings are not equal

Numbers are equal

Numbers are not equal

File exists and length > 0

File is a directory

File is writable

operator

\$string1 = \$string2

\$string1 != \$string2

\$number1 –eq \$number2

\$number1 –ne \$number2

-s \$file

-d \$file

-r \$file

-w \$file

15.5 使用 if 结构

if condition is true

then

carry out this set of actions

else

carry out these alternative actions

fi

if命令示例

\$ cat active

USAGE=“\$0 userid”if [[ \$# -ne 1 ]]

then

echo “Proper Usage: \$USAGE”

exit

fi

if [ who | grep \$1 > /dev/null ]

then

echo “ \$1 is active”

else

echo “ \$1 is not active”

fi

exit 0

\$ cat delfile

# Usage: delfile

echo “please enter the file name: “

if [[ -f \$name ]]

then

rm \$name

else

echo “Error: \$name is not an ordinary file”

fi

15.7 循环结构

for var in list

do

command(s)

done

for循环示例:

\$ cat count

for var in file1 file2 file3

Do

wc –l \$var

done

\$ count

18 file1

19 file2

20 file3

while语法结构：

While expression

do

command(s)

done

while循环会一直执行到表达式的值为false

while循环示例：

\$ cat information

X=1

while [[ \$x –lt 9 ]]

do

echo “It is now \$(date)”

echo “There are \$(ps –e| wc –l) processes running

echo “There are \$(who | wc –l) users logged in”

x=\$(expr \$x + 1)

sleep 600

done

15.8 shell内部命令

shell内部命令是内建于shell中命令，使用这些

Shell内部命令 (2)

shell内部命令列表：

.

continue

echo

eval

exec

exit

Shell内部命令 (3)

export

pwd

return

set

shift

Shell内部命令 (4)

test

times

trap

ulimit

unset

wait

Shell内部命令 示例

\$ pg set_ex

#!/bin/sh

set accounts.doc accounts.bak

while [ \$# != 0 ]

do

echo \$1

shift

done

\$ set_ex

accounts.doc

accounts.bak

15.9 软中断

trap命令

trap “name” signal(s)

name是捕捉到信号后所采取的处理动作。

singlal(s)就是待捕捉的信号

1) 清除临时文件。

2) 忽略该信号。

3) 询问用户是否终止该脚本的运行。

trap " " 2 3

trap “cmd” 2 3

trap 2 3

#cat trap1

#!/bin/sh

#trap1

trap "my_exit" 2

LOOP=0

my_exit()

{

echo "you just hit <CTRL -C>, at number \$LOOP"

echo " I will now exit"

exit 1

}

while :

do

LOOP=`expr \$LOOP + 1`

echo \$LOOP

done

PATH=/bin:/usr/bin:/etc/:\$HOME/bin:.

PS1=‘\$PWD => ‘

ENV=\$HOME/.kshrc

if [ -s “\$MAIL” ]

then

mail

fi

echo “Enter Terminal Type [Default: ibm3151):\c”

if [ -n “\$a” ]

then TERM=\$a

else TERM=ibm3151

fi

echo “it is now \$(date)”

echo “There are \$(ps –e| wc –l) processes running”

echo “There are \$(who | wc –l) users logged in”

export PATH ENV TERM PS1

#!/bin/sh

#logroll

#roll over the log files if size have reach the MARK

#could also be used for mail boxes

#limit size of log: 4096k

BLOCK_LIMIT=8

MYDATE=`date +%d%m`

#list of logs to check

LOGS="var/spool/audlog /var/spool/networks/netlog /etc/dns/named_log"

for LOGFILE in LOGS

do

if [ -f LOG_FILE ]; then

# get the block size

F_SIZE=`du -a \$LOG_FILE | cut -f1`

else

echo "`basename \$0` cannot find the \$LOG_FILE" > &2

continue

fi

if [ "\$F_SIZE" -gt "\$BLOCK_LIMIT" ]; then

#copy the log across and append a ddmm date on it

cp \$LOG_FILE \$LOG_FILE\$MYDATE

#clear the log

>\$LOG_FILE

fi

done

1. 在命令行中进行整数运算应该采用以下哪个命令:

A. grep

B. expr

C. echo

D. egrep

2. 通过以下哪条命令可以查看到当前进程的PID:

• echo \$\$

• echo \$?

• echo \$#

• echo \$0

3. 简述下列脚本的功能:

TERMTYPE=\$TERM

if [ \$TERMTYPE != “” ]

then

if [ -f /home/team01/customized_script ]

then

/home/team01/customized_script

else

echo No customized script available !

fi

else

echo “you do not have a TERM variable set !”

fi

4. 设计一个脚本显示以下图形:

11,22,33,44,55,66,77,88,99

22,33,44,55,66,77,88,99,

33,44,55,66,77,88,99,,

44,55,66,77,88,99,,,

55,66,77,88,99,,,,

66,77,88,99,,,,,

77,88,99,,,,,,

88,99,,,,,,,

99,,,,,,,,

5. 设计一个脚本实现以下功能：列出指定目录下的所

1. B

2. A

3. 在/home/team01下查找有无customized_script

4.

set 11 22 33 44 55 66 77 88 99

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9;shift

echo \$1,\$2,\$3,\$4,\$5,\$6,\$7,\$8,\$9

5.

#!/bin/sh

#this script is used to list the content of the

# source dir and copy them to the target dir

if [ \$# -lt 2 ];

then

echo "usage: \$0 sourcedir aimdir";

else

cd \$1

echo "the content of \$1 is: "

ls -l

cp -rf * \$2

fi