创建一个文件,其中包括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
}