shell程序设计.doc_第1页
shell程序设计.doc_第2页
shell程序设计.doc_第3页
shell程序设计.doc_第4页
shell程序设计.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验二 shell程序设计【实验目的】(1) 掌握shell的概念,了解shell的工作原理(2) 熟悉shell环境(3) 掌握基本的shell程序设计【基础知识】1、if语句(1)判断输入的是目录、文件还是其它,如果是文件,获取文件的读写执行权限#! /bin/bash#an example script of ifclearecho input a file or directory name,plsread path_nameif -d $path_name then echo $path_name is a directoryelif -f $path_name then echo $path_name is a regular file if -r $path_name then echo $path_name is a readable file fi if -w $path_name then echo $path_name is a writeable file fi if -x $path_name then echo $path_name is a executeable file fielse echo this script cannot get the file/directory( $path_name) information!fi2. 以shell返回值作为判断条件,判断能否进入输入的目录#! /bin/bash#another example script of ifecho input a directory, plsread dir_nameif cd $dir_name /dev/null 2&1then echo enter directory: $dir_name succeedelse echo enter directory: $dir_name failedfi3. case语句,判断输入值的几种状态#! /bin/bash#an example script of caseclearecho Do you like Linux?read like_itcase $like_it iny|Y|yes|Yes) echo Linux is a good friend of us. ;n|N|no|No) echo Try it, and you will like it. ;* ) echo your answer is: $like_it ;esac 4. for循环(1)列表值在程序中#! /bin/bash#an example script of FORclearfor os in Linux Windows UNIX MACdo echo Operation System is: $osdone(2)列表值通过命令行参数列表传入#! /bin/bash#an example script of FOR without arglistclearfor os do echo Operation System is: $osdone5. while和until循环(1)while循环#! /bin/bash#an example script of whileclearloop=0while $loop -ne 10 do let loop=$loop+1 echo current value of loop is: $loop done(2)until循环#! /bin/bash#an example script of untilclearloop=0until $loop -eq 10 do let loop=$loop+1 echo current value of loop is: $loop done6. 函数调用及参数传递(1)利用数组使用参数#! /bin/bash#an example script of functionfunction demo_fun() echo Your command is: $0 $* echo Number of parameters($#) is: $# echo Parameters($*) is: $* echo Parameters($) is: $ count=1 for param in $ do echo Parameters($count) is: $param let count=$count+1 donecleardemo_fun $(2) 利用shift操作使用参数#! /bin/bash#an example script of functionfunction demo_fun() echo Your command is: $0 $* echo Number of parameters($#) is: $# echo Parameters($*) is: $* echo Parameters($) is: $ count=1 while -n $1 do echo Parameters($count) is: $1 let count=$count+1 shift donecleardemo_fun $7. 函数返回值(1)显示返回#! /bin/bash#an example script of returnfunction fun_return() dir_name=$1 rtn_value=0 if ! cd $dir_name /dev/null 2&1; then rtn_value=1 fi return $rtn_valueclearif fun_return $; then echo function executes sucessfully!else echo function executes failed!fi(2)隐式返回#! /bin/bash#an example script of returnfunction fun_return() dir_name=$1 cd $dir_name /dev/null 2&1 returnclearif fun_return $; then echo function executes sucessfully!else echo function executes failed!fi8. 综合练习:文件备份脚本#! /bin/bash#an example script of backup filesLOG_START_TIME=date +%Y%m%d%H%M%SBACKUP_DIR=/Lesson10/backupBACKUP_LOG=$BACKUP_DIR/$LOG_START_TIME.logfunction write_log() log_time=date +%Y-%m-%d-%H-%M-%s backup_file_name=$2 err_msg=$log_time ERROR in backup file/directory($backup_file_name) suc_msg=$log_time SUCCESS in backup file/directory($backup_file_name) if $1 -eq 0 ;then echo $suc_msg echo $suc_msg $BACKUP_LOG else echo $err_msg echo $err_msg $BACKUP_LOG fifunction backup_file() cp -fr $1 $BACKUP_DIR /dev/null 2&1 write_log $? $1function create_log_file() if ! -e $BACKUP_DIR ;then mkdir $BACKUP_DIR fi if -e $BACKUP_LOG ; then rm -f $BACKUP_LOG fi touch $BACKUP_LOGclearecho Backup Process Beginscreate_log_filefor file in $do backup_file $filedoneecho Backup Process ends【实验内容】1.带一个(目录)参数,可浏览目录下的内容。(lsdir.sh)实现功能:(1)首先判断(if)是否带有参数,若无则显示(echo)用法信息后,报错返回(exit);(2)首先判断该参数是不是目录。若是则列目录的内容(ls),否则,提示用户不是目录,则显示警告信息后报错返回。运行:设程序名为lsdir.sh,为其增加执行权chmod 755 lsdir.sh运行:./lsdir.sh./lsdir.sh dir#如果dir是个目录,则列其内容./lsdir.sh file#若file是文件或不存在则给出警告后返回2. 只用嵌套结构if-fi实现上述程序 (lsdirif.sh)3、带数值参数,并可计算这些数值参数的和 (sum.sh)4、计算前n个正整数的和 (sum_n.sh)注:此程序带有1个参数作为整数n,以计算1至n个正整数的和5、编写Shell程序 (setmod.sh),在/home目录下建立10个目录,即u

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论