qfedu-linux-advanced-level/day1/homework/h8.sh

25 lines
592 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 编写一个Shell脚本接受一个目录名作为参数将目录中所有文件打包成xx.tar.bz2到它的父级目录然后再删除该目录及其所有子目录和文件。
if [ $# -ne 1 ]; then
echo "请提供目录参数"
exit 1
fi
dir=$1
# 获取父级目录
father_dir=`dirname $dir`
# 获取目录名
dir_name=`basename $dir`
# 打包目录中所有文件
tar -jcvf $father_dir/$dir_name.tar.bz2 $dir
# 判断是否打包成功
if [ $? -eq 0 ];then
# 删除目录
rm -rf $dir
echo "已删除目录 $dir"
else
echo "打包失败"
fi