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

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

 #!/bin/bash
while :
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 "请输入正确的代号!"
  ;;
  esac
done

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

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

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

 #!/bin/bash
decompress1()
{
echo "请输入你要解压的文件名:"
read file
var=$file
case $file in  #判断输入的文件后缀名为俩两位还是一位,并获取无后缀名的文件名
*.*.*) echo " ${var%.*.*} "
;;
*.*) echo " ${var%.*} "
;;
*) echo " 输入格式有误 "
;;
esac
if [ -f $file ]
 then
case $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()" 
;;
esac
echo "文件解压成功,请查看!"
ls -l $var
else
 echo "'$file'是无效文件! "
fi
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值