shell 编程 根据文件名实现智能打包 解压

利用tar命令,根据文件名实现智能压缩,解压。

#!/bin/bash

####################################
# Smart package shell
####################################

aim_file=$1

function package(){
	read -p "Input the package name." name
	while [[ true ]]; do
		read -p "Input the package type [gz/bz2]" tartype
		case ${tartype} in
			"gz" )
				tar -czvf ./${name}.tar.${tartype} $aim_file
				echo -e "\033[32mpackage complete.\033[0m"
				exit 0
				;;
			"bz2" )
				tar -cjvf ./${name}.tar.${tartype} $aim_file
				echo -e "\033[32mpackage complete.\033[0m"
				exit 0
				;;
			* )
				echo -e "Only accept \033[031mgz\033[0m or \033[031mbz2\033[0m!Now,try again."
		esac
		
	done
}

function gzunpackage(){
	while [[ true ]]; do
		read -p "Input the aim directory." dir
		if [[ -d $dir ]]; then
			tar -xzvf $aim_file -C $dir
			exit 0
		else
			echo "No such a directory,please input again!"
		fi
	done
	echo -e "\033[32munpackage complete.\033[0m"
}

function bz2unpackage(){
	while [[ true ]]; do
		read -p "Input the aim directory." dir
		if [[ -d $dir ]]; then
			tar -xjvf $aim_file -C $dir
			exit 0
		else
			echo "No such a directory,please input again!"
		fi
	done
	echo -e "\033[32munpackage complete.\033[0m"
}

if [[ -e $aim_file ]]; then
	if [[ ${aim_file##*.} = "gz" ]]; then
		echo "This is a gz file."
		gzunpackage
	elif [[ ${aim_file##*.} = "bz2" ]]; then
		echo "This is a bz2 file."
		bz2unpackage
	else
		echo "We will package it to a gz file."
		package
	fi
else
	echo "No such a file or directer."
fi




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值