多功能精准删除并解压缩实验
[root@localhost ~]# vim tar
#!/bin/bash
read -t 30 -p "Please enter the absolute path of the compressed file : " lj
if [ -e "$lj" ]
then
echo $lj >> where
echo -e "\e[1;32m \ta --- tar -zxvf\n\tb --- gunzip\n\tc --- unzip\n\td --- bunzip2 \e[0m"
read -t 30 -p "please input a-d : " jyfs
for j in $( cat where )
do
cd $j
done
case $jyfs in
"a")
touch file /dev/null
ls *.tar.gz > file
for j in $( cat file )
do
tar -zxvf $j & > /dev/null
done
rm -rf file /dev/null
;;
"b")
touch file1 /dev/null1
ls *.gz > file1
for k in $( cat file1 )
do
gunzip $k & > /dev/null1
done
rm -rf file1 /dev/null1
;;
"c")
touch file2 /dev/null2
ls *.zip > file2
for l in $( cat file2 )
do
unzip $l & > /dev/null2
done
rm -rf file2 /dev/null2
;;
"d")
touch file3 /dev/null3
ls *.bz2 > file3
for m in $( cat file3 )
do
bunzip2 -k $m & > /dev/null3
done
rm -rf file3 /dev/null3
;;
*)
echo "No compressed files,Or there is no such option"
esac
fi
[root@localhost ~]# chmod 755 tar
[root@localhost ~]# ls
ag dohave jjcc newtest shell1 years 视频 下载
anaconda-ks.cfg if1 lass.tar.gz old tar 公共 图片 音乐
and initial-setup-ks.cfg mm sh2006 where 模板 文档 桌面
[root@localhost ~]# ./tar
Please enter the absolute path of the compressed file : /root
#压缩文件所在的绝对路径
a --- tar -zxvf
b --- gunzip
c --- unzip
d --- bunzip2
please input a-d : a #请输入a-d
./tar: 第 10 行:cd: b: 没有那个文件或目录
[root@localhost ~]# ./tar:行19: 无法从 /dev/null 重定向标准输入: 没有那个文件或目录
lass
[root@localhost ~]# ls
ag if1 lass.tar.gz sh2006 years 图片 桌面
anaconda-ks.cfg initial-setup-ks.cfg mm shell1 公共 文档
and jjcc newtest tar 模板 下载
dohave lass old where 视频 音乐
#!/bin/bash
# 批量解压缩文件
ls *.tar.gz > ls.log
ls *.tgz >> ls.log
for item in $(cat ls.log); do
# echo "${item}"
tar -zxf $item & > ./null # 不显示任何信息
done
rm -rf ./ls.log
#!/bin/bash
# 累加
sum=0
for ((i = 0; i <= 100; i++)); do
# echo "${i}"
sum=$(($sum + $i))
done
echo $sum
ls *.tar.gz > ls.log 覆盖到log文件
ls *.tgz >> ls.log 追加到log文件

#!/bin/bash
#先切换目录到准备要解压的文件夹下
cd /root/test
#把.tar.gz格式要解压的文件输出重定向写入ls.log
ls *.tar.gz > ls.log
#读取ls.log中要解压的文件进行循环解压
for i in $( cat ls.log )
do
#解压出来的过程不显示,并把解压过程信息输出重定向写入null
tar -xzf $i & > /dev/null
done
#删除临时文件
rm -rf ls.log
for循环解压多个文件名的文件
解压缩多个变量名的压缩包
for循环语法
> 输出覆盖 >>输出追加
for循环
语法1:
for 变量 in 值1 值2 值3 …
do
程序
done
示例1
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
示例2
##!/bin/bash
cd /root/test
ls *.tar.gz > ls.log
for i in $( cat ls.log )
do
tar -czf $i &>/dev/null
#把执行过程中所有输出信息丢到回收站,不显示在屏幕上
done
rm -rf ls.log
#!/bin/bash
cd /root/test
ls *.tar.gz > ls.log(>覆盖)
ls *.tgz >> ls.log(>>追加)
for i in $(cat ls.log)
do
tar -zxvf $i &> /dev/null
done
rm -rf ls.log
批量解压缩脚本
ls .*tar > ls.log
#把以tar结尾的文件都放到日志文件ls.log中
&>/dev/null
#把解压的详情信息放到回收站
rm -rf /lamp/ls.log
#删除临时文件ls.log防止以后干扰
for语法1
#!/bin/bash
cd /root/test
ls *.tar.gz > ls.log(>覆盖)
ls *.tgz >> ls.log(>>追加)
for i in $(cat ls.log)
do
tar -zxvf $i &> /dev/null
done
rm -rf ls.log
批量解压缩文件脚本
tar -zxf $i
for循环