⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux shell

📁 对linux下边的gzip的文件的分析的代码
💻
📖 第 1 页 / 共 4 页
字号:
#check arguments
if [ $# -ne 1 ]
then
echo "usage: $0 directory"
exit 1
fi

#check for valid directory name
if [ ! -d "$1" ]
then
echo "$1 is not a directory"
exit 2
fi

cd $1

ls -a | cpio -o > /dev/rmt/0h

if [ $? -eq 0 ]
then
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
fi
# end of unload

在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二
设置程序的退出状态

g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi

fi
fi

改进:使用elif结构
if command
then
command
elif command
then
command
elif command
then
command
fi

elif结构同if结构类似,但结构更清淅,其执行结果完全相同

返回页首


chg1226
公社特别嘉宾

注册: Mar 22, 2003
文章: 148
位置: China
发表于: 2003-08-21, 3:01pm 发表主题:

--------------------------------------------------------------------------------

[ZZ]Bourne Shell及shell编程(2)
发信人: Altmayer (alt>>追求>>堕落>>极限>> ), 信区: GNULinux
标 题: Bourne Shell及shell编程(2)
发信站: 饮水思源 (2001年12月30日00:21:51 星期天), 站内信件

【 以下文字转载自 Altmayer 的信箱 】
【 原文由 Altmayer.bbs@altmayer.dhs.org 所发表 】
来 源: from altmayer.dhs.org ([211.80.41.106])
日 期: Sun Dec 30 00:20:37 2001

标 题: LINUX选修课讲课义:Bourne Shell及shell编程(2)
发信站: 碧海青天 (Thu Apr 22 23:33:45 1999), 转信

版权声明:
本文内容为大连理工大学LINUX选修课讲义,欢迎大家转载,但禁止使用本材料进行
任何商业性或赢利性活动。转载时请保留本版权声明。

作者:何斌武,hbwork@dlut.edu.cn,大连理工大学网络中心,April 1999.

URL: ftp://ftp.dlut.edu.cn/pub/PEOPLE/albin/

/********抱歉,为了格式不乱,我用代码模式粘贴全文*********/
------------------------------------------------------------------------------


源码:--------------------------------------------------------------------------------
Bourne Shell及Shell编程(2)
h.交互式从键盘读入数据
使用read语句,其格式如下:

read var1 var2 ... varn

read将不作变量替换,但会删除多余的空格,直到遇到第一个换行符(回车),
并将输入值依次赋值给相应的变量。

例:
$ read var1 var2 var3
Hello my friends
$ 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 ]
then
exit
fi

i. case结构:结构较elif-then结构更清楚

比较if-then语句:

if [ variable1 = value1 ]
then
command
command
elif [ variable1 = value2 ]
then
command
command
elif [ variable1 = value3 ]
then
command
command
fi

相应的case结构:

case value in
pattern1)
command
command;;
pattern2)
command
command;;
...
patternn)
command;
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 ) command
command ;;

这样可以将上面示例程序中允许用户输入数字或每一个大写字母。

case "$CHOICE" in
1|R) echo "Restore";;
2|B) echo "Backup";;
3|U) echo "Unload";;
*) echo "Sorry $CHOICE is not a valid choice
exit 1
esac

(5)循环控制
<1> while循环:
格式:
while command
do
command
command
command
...
done

例: 计算1到5的平方
#!/bin/sh
#
#Filename: square.sh
int=1

while [ $int -le 5 ]
do
sq=`expr $int \\* $int`
echo $sq
int=`expr $int + 1`
done
echo "Job completed"

$ sh square.sh
1
4
9
16
25
Job completed

<2> until循环结构:
格式:
until command
do
command
command
....
command
done

示例:使用until结构计算1-5的平方
#!/bin/sh

int=1

until [ $int -gt 5 ]
do
sq=`expr $int \\* $int`
echo $sq
int=`expr $int + 1`
done
echo "Job completed"

<3> 使用shift对不定长的参数进行处理
在以上的示例中我们总是假设命令行参数唯一或其个数固定,或者使用$*将整个命
行参数传递给shell script进行处理。对于参数个数不固定并且希望对每个命令参
进行单独处理时则需要shift命令。使用shift可以将命令行位置参数依次移动位置
即$2->$1, $3->$2. 在移位之前的第一个位置参数$1在移位后将不在存在。

示例如下:

#!/bin/sh
#
#Filename: shifter

until [ $# -eq 0 ]
do
echo "Argument is $1 and `expr $# - 1` argument(s) remain"
shift
done


$ 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,使用这一特性可以用until循环对每个参
数进行处理,如下示例中是一个求整数和的shell script:

#!/bin/sh
# sumints - a program to sum a series of integers
#

if [ $# -eq 0 ]
then
echo "Usage: sumints integer list"
exit 1
fi

sum=0

until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum


$ sh sumints 324 34 34 12 34
438
$

使用shift的另一个原因是Bourne Shell的位置参数变量为$1~$9, 因此通过位置变
只能访问前9个参数。但这并不等于在命令行上最多只能输入9个参数。此时如果想
访问
前9个参数之后的参数,就必须使用shift.

另外shift后可加整数进行一次多个移位,如:

shift 3


<4>. for循环
格式:
for var in arg1 arg2 ... argn
do
command
....
command
done

示例:
$ for letter in a b c d e; do echo $letter;done
a
b
c
d
e

对当前目录下的所有文件操作:
$ for i in *
do
if [ -f $i ]
then
echo "$i is a file"
elif [ -d $i ]
echo "$i is a directory"
fi
done

求命令行上所有整数之和:
#!/bin/sh

sum=0

for INT in $*
do
sum=`expr $sum + $INT`
done

echo $sum


<6> 从循环中退出: break和continue命令
break 立即退出循环
continue 忽略本循环中的其他命令,继续下一下循环

在shell编程中有时我们要用到进行无限循环的技巧,也就是说这种循环一直执行
到break或continue命令。这种无限循环通常是使用true或false命令开始的。UNIX
系统中的true总是返加0值,而false则返回非零值。如下所示:

#一直执行到程序执行了break或用户强行中断时才结束循环
while true
do
command
....
command
done

上面所示的循环也可以使用until false, 如下:

until false
do
command
....
command
done

在如下shell script中同时使用了continue,break以及case语句中的正规表达式用
法:

#!/bin/sh
# Interactive program to restore, backup, or unload
# a directory

echo "Welcome to the menu driven Archive program"

while true
do
# 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

case $CHOICE in
[1-3] ) echo
# Read and validate the name of the directory

echo -n "What directory do you want? "
read WORKDIR

if [ ! -d "$WORKDIR" ]
then
echo "Sorry, $WORKDIR is not a directory"
continue
fi

# Make the directory the current working directory
cd $WORKDIR;;

4) :;; # :为空语句,不执行任何动作
*) echo "Sorry, $CHOICE is not a valid choice"
continue
esac

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;;

4) echo "Quitting"
break;;
esac

#Check for cpio errors

if [ $? -ne 0 ]
then
echo "A problem has occurred during the process"
if [ $CHOICE = 3 ]
then
echo "The directory will not be erased"
fi

echo "Please check the device and try again"
continue
else
if [ $CHOICE = 3 ]
then
rm *
fi
fi
done

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -