编写Shell程序,通过编写完成compress1、decompress1函数,选择“压缩”或“解压”以及输入文件名,来自动完成文件的压缩、解压过程。

来源:互联网 发布:淘宝没有销售属性 编辑:程序博客网 时间:2024/06/02 05:29

创建一个文件,其中包括mycompress1.sh,mydecompress1.sh和mypro.sh文件。其中mypro.sh 写入的代码如下:

 #!/bin/bashwhile :do  echo "weclome to use this program,please choose!"  echo " 1- 压缩文件!"  echo " 2- 解压文件! "  echo " 0- 退出程序!"  read num  case $num in  1)source mycompress1.sh  compress1  ;;  2)source mydecompress1.sh  decompress1  ;;  0)exit 0  ;;  *)echo "请输入正确的代号!"  ;;  esacdone

mycompress1.sh文件的代码如下:(如果想要实现压缩成不同的后缀名的格式,可参照mydecompress1.sh文件写入case语句进行选择)

 #!/bin/bashcompress1(){echo -n "请输入你想要压缩的文件名:"read fileecho "$file"if [ -d $file ];then tar zcvf "$file".tar.gz "$file"echo "文件压缩成功,请查看!"ls -l "$file".tar.gzelseecho "文件无效!"fi}

mydecompress1.sh文件的代码内容如下:

 #!/bin/bashdecompress1(){echo "请输入你要解压的文件名:"read filevar=$filecase $file in  #判断输入的文件后缀名为俩两位还是一位,并获取无后缀名的文件名*.*.*) echo " ${var%.*.*} ";;*.*) echo " ${var%.*} ";;*) echo " 输入格式有误 ";;esacif [ -f $file ] thencase $file in*.tar.bz2) tar xjf $file;;*.tar.gz) tar xzf $file;;*.bz2) bunzip2 $file;;*.rar) unrar e $file;;*.gz) gunzip $file;;*.tar) tar xf $file;;*.tbz2) tar xjf $file;;*.tgz) tar xzf $file;;*.zip) gunzip $file;;*.Z) uncompress $file;;*.7Z) 7z x $file;;*) echo "‘$file‘ cannot be extract()" ;;esacecho "文件解压成功,请查看!"ls -l $varelse echo "'$file'是无效文件! "fi}
原创粉丝点击