-
查看全部
-
执行脚本无视报错,可正常运行
多功能精准删除并解压缩实验
[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 视频 音乐
查看全部 -
兄弟连linux一模一样
查看全部 -
懒人脚本
[root@localhost ~]# vim sh2006
#!/bin/bash
echo -e "\e[1;31m Can I hlep you ? \e[0m"
echo -e "\e[1;32m \ta --- systemctl restart network \n\tb --- systemctl stop firewalld\n\tc --- ip a\n\td --- shutdown -h now\n\te --- shoutdown -r now\n\tf --- systemclt restart vsftpd\n\tg --- systemctl restart named\n\th --- systemctl restart nmb\n\ti --- systemctl restart smb\n\tj --- systemctl restart httpd \e[0m"
read -t 30 -p "Please you input a-j : " hp
if [ -n "$hp" ]
then
case $hp in
"a")
systemctl restart network
echo "Restart the network card"
/root/sh2006
;;
"b")
systemctl stop firewalld
echo "Turn off the firewall"
/root/sh2006
;;
"c")
ip a
echo "View network card information"
/root/sh2006
;;
"d")
shutdown -h now
echo "Shut down the virtual machine"
/root/sh2006
;;
"e")
shutdown -r now
echo "Restart the virtual machine"
/root/sh2006
;;
"f")
systemctl restart vsftpd
echo "Restart the ftp"
/root/sh2006
;;
"g")
systemctl restart named
echo "Restart the DNS"
/root/sh2006
;;
"h")
systemctl restart nmb
echo "Restart the nmb"
/root/sh2006
;;
"i")
systemctl restart smb
echo "Restart the nmb"
/root/sh2006
;;
"j")
systemctl restart httpd
echo "Restart the http"
/root/sh2006
;;
"l")
echo "exit"
;;
*)
echo "No such option"
;;
esac
fi
[root@localhost ~]# chmod 755 /root/sh2006 [root@localhost ~]# ./sh2006
查看全部 -
加减乘除
vim jjcc
#!/bin/bash
read -t 30 -p "Please input + and - and * and / : " jjcc
read -t 30 -p "Please input number : " num1
read -t 30 -p "Please input number : " num2
if [ "$jjcc" == + -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 + $num2))
echo "num1+num2=$sum"
elif [ "$jjcc" == - -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 - $num2))
echo "num1-num2=$sum"
elif [ "$jjcc" == "*" -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 * $num2))
echo "num1 x num2=$sum"
elif [ "$jjcc" == / -a -n "$num1" -a -n "$num2" ]
then
sum=$(($num1 / $num2))
echo "num1/num2=$sum"
else
echo "num1 or num2 no number"
fi
查看全部 -
#!/bin/bash
# int=1
# while (($int <= 5)); do
# echo $int
# let "int++"
# done
# echo '按下 <CTRL-D> 退出'
# echo -n '输入你最喜欢的网站名: '
# while read FILM; do
# echo "是的!$FILM 是一个好网站"
# done
# 累加
i=1
sum=0
while [ $i -le 100 ]; do
sum=$(($sum + $i))
i=$(($i + 1))
done
echo $sum
从上面可知 条件可以为数字比较表达式 或者命令 比如read
查看全部 -
#!/bin/bash
# 条件为false继续循环 为true终止循环
i=1
sum=0
until [ $i -gt 100 ]; do
sum=$(($sum + $i))
let i++
done
echo $sum
查看全部 -
#!/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
查看全部 -
#!/bin/bash
# for有多种形式
for item in 1 2 3 4 5; do
echo "value: $item"
done
for item in {"Hello World!",a,bc,1372}; do
echo "${item}"
done
for item in {a..z}; do
echo "${item}"
done
for ((i = 0; i < 10; i++)); do # 数值的计算需要加小括号
echo "${i}"
done
for((i=0;i<n;i++)); do
for((j=0;j<m;j++)); do
echo "${i}, ${j}"
done
done
for col in $(docker images | awk '{ print $1":"$2 }'); do
echo "${col}" | cut -d ':' -f 1
echo "${col}" | cut -d ':' -f 2
done
查看全部 -
ls *.tar.gz > ls.log 覆盖到log文件
ls *.tgz >> ls.log 追加到log文件
查看全部 -
str1 == str2 判断字符串是否相等
str1 != str2 判断字符串是否不相等
查看全部 -
-n 判断字符串是否为空 不为空返回true
-z 判断字符串是否为空 为空返回true
-a and 且 串联多个条件
-o or 或
查看全部 -
#!/bin/bash
#给变量s赋值
s=0
#for循环,变量i作为计数值从1开始,循环100次
for ((i=1;i<=100;i=i+1))
do
#变量s的值由初始值+计数值
s=$(($s+$i))
done
echo "1+2+3+4....+100=$s"
查看全部 -
#!/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
查看全部
举报