目录
一、脚本练习1(新建)

1.1 需求分析
1. mkdir {1..10}或for
【具体实现】
mkdir feng{1..10}
for i in {1..10}
do
mkdir fengdy$i
done
2. touch liu{1..10}
for i in {1..10}
do
touch liu$i
done
3. vim dir10_file10.sh
4.mkdir -p /feng123
1.2 实现
1.2.1 直接建立
[root@localhost lianxi]# cat dir10_file10.sh
#!/bin/bash
mkdir -p /feng123
cd /feng123
mkdir sanle{1..10}
touch hunan{1..10}
1.2.2 循环
[root@localhost lianxi]# cat dir10_file10_v2.sh
#!/bin/bash
mkdir -p /feng456
for i in {1..10}
do
mkdir /feng456/sanle$i
touch /feng456/hunan$i
done
1.3 检验
[root@localhost lianxi]# ls /
back bin dev home lib media opt root sbin sys usr
bak boot etc lianxi lib64 mnt proc run srv tmp var
[root@localhost lianxi]# bash dir10_file10.sh
[root@localhost lianxi]# ls /
back bin dev feng123 lianxi lib64 mnt proc run srv tmp var
bak boot etc home lib media opt root sbin sys usr
[root@localhost lianxi]# ls /feng123
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost lianxi]# bash dir10_file10_v2.sh
[root@localhost lianxi]# ls /feng456
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
二、脚本练习2(删除)

【前期准备】
[root@localhost lianxi]# cd /feng123
[root@localhost feng123]# ls
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost feng123]# touch sc.txt
[root@localhost feng123]# ls
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8 sc.txt
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost feng123]# cd /feng456
[root@localhost feng456]# ls
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost feng456]# touch sc.txt
[root@localhost feng456]# ls
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8 sc.txt
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
2.1 需求分析
1. 删除 rm -rf
2. 删除以sanle和湖南开头的文件或文件夹 rm -rf sanle* hunan*
2.2 具体实现
[root@localhost lianxi]# vim del_file.sh
[root@localhost lianxi]# cat del_file.sh
#!/bin/bash
cd /feng123
rm -rf sanle* hunan*
cd /feng456
rm -rf sanle* hunan*
[root@localhost lianxi]# ls /feng123
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8 sc.txt
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost lianxi]# ls /feng456
hunan1 hunan2 hunan4 hunan6 hunan8 sanle1 sanle2 sanle4 sanle6 sanle8 sc.txt
hunan10 hunan3 hunan5 hunan7 hunan9 sanle10 sanle3 sanle5 sanle7 sanle9
[root@localhost lianxi]# bash del_file.sh
[root@localhost lianxi]# ls /feng123
sc.txt
[root@localhost lianxi]# ls /feng456
sc.txt
三、脚本练习3(接受用户输入)
3.1 需求分析
- 接受用户分析 read 【read -p "请输入数字:" num
- 新建文件夹 mkdir
- 产生序列
[root@localhost lianxi]# seq 4
1
2
3
4
# 产生3到8的一个序列
[root@localhost lianxi]# seq 3 8
3
4
5
6
7
8
[root@localhost lianxi]# aa=$(seq $num)
[root@localhost lianxi]# echo $aa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
3.2 具体实现
[root@localhost lianxi]# vim create_file.sh
[root@localhost lianxi]# cat create_file.sh
#!/bin/bash
#接受用户输入的文件名前缀
read -p "请输入文件名前缀:" f_name
#文件的数量
read -p "请输入文件的数量:" f_num
#文件存放的路径
read -p "请输入文件存放的路径,请使用绝对路径:" f_path
mkdir -p $f_path
cd $f_path
#实现新建文件的功能
for i in $(seq $f_num)
do
mkdir $f_name$i
done
#显示新建的文件
echo "##################################"
ls $f_path
echo "##################################"
[root@localhost lianxi]# bash create_file.sh
请输入文件名前缀:lihua
请输入文件的数量:18
请输入文件存放的路径,请使用绝对路径:/lihua
##################################
lihua1 lihua11 lihua13 lihua15 lihua17 lihua2 lihua4 lihua6 lihua8
lihua10 lihua12 lihua14 lihua16 lihua18 lihua3 lihua5 lihua7 lihua9
##################################
四、补充【$(),判断文件是否存在】
4.1 $()的使用
用来得到命令的执行结果,$(命令的执行结果)
[root@localhost ~]# f_num=$(seq 10)
[root@localhost ~]# echo $f_num
1 2 3 4 5 6 7 8 9 10
统计/etc/passwd 有多少行
[root@localhost ~]# num=$(cat /etc/passwd|wc -l)
[root@localhost ~]# echo $num
34
4.2 [ -f 文件地址 ]
判断文件是否存在
4.2.1 echo $?
表示上一条命令的返回值。0表示执行成功,非0表示执行失败
4.2.2 【示例】
[root@localhost lianxi]# [ -f /lihua ]
[root@localhost lianxi]# echo $?
1
[root@localhost lianxi]# [ -f /etc/passwd ]
[root@localhost lianxi]# echo $?
0
4.3 if语句
语法结构如下
if 命令 ==》当命令执行成功,就执行命令1,执行失败就执行命令2
then
命令1
else
命令2
fi
五、脚本练习4(统计文件行数)
题目:用户输入一个文件,统计有多少行,并输出给用户
[root@localhost lianxi]# vim lines.sh
[root@localhost lianxi]# cat lines.sh
#!/bin/bash
#提醒用户输入文件的文件的绝对路径
read -p "请输入文件的绝对路径:" f_path
#统计文件的行数
num=$(cat $f_path|wc -l)
#输出文件里有多少行
echo "${f_path}文件里有${num}行"
[root@localhost lianxi]# bash lines.sh
请输入文件的绝对路径:/etc/passwd
/etc/passwd文件里有34行
[root@localhost lianxi]# bash lines.sh
请输入文件的绝对路径:/etc/hosts
/etc/hosts文件里有2行
[root@localhost lianxi]# bash lines.sh
【升级版:if语句】
[root@localhost lianxi]# vim lines.sh
[root@localhost lianxi]# bash lines.sh
请输入文件的绝对路径:/lihua
sorry,sir!你的文件不存在,请检查输入的文件路径
[root@localhost lianxi]# bash lines.sh
请输入文件的绝对路径:/etc/hosts
/etc/hosts文件里有2行
[root@localhost lianxi]# cat lines.sh
#!/bin/bash
#提醒用户输入文件的文件的绝对路径
read -p "请输入文件的绝对路径:" f_path
#对文件进行判断,是否存在
if [ -f $f_path ]
then
#统计文件的行数
num=$(cat $f_path|wc -l)
#输出文件里有多少行
echo "${f_path}文件里有${num}行"
else
echo "sorry,sir!你的文件不存在,请检查输入的文件路径"
fi
六、for循环
6.1 语法结构
in 后面接集合:
-
- 一个命令得到的集合
- 一个变量里的集合
- 一个文件夹的路径
for 变量名 in 集合
do
命令
done
变量名到集合里去取内容,先取集合里的第一个内容,然后去执行命令,再回到集合中取第二个内容,再次执行命令,以此类推,直到所有的内容都取出并执行命令时,循环结束。
6.2 示例
[root@localhost lianxi]# vim list_file.sh
[root@localhost lianxi]# cat list_file.sh
#!/bin/bash
#for i in /boot/*
for i in $(ls /boot)
do
echo "$i 在/boot目录下"
done
#定义一个字符串,里面的元素用空格隔开,如此就有了众多元素
sc_singer="李华 晓红 晓燕 冬梅"
for a in $sc_singer
do
echo "有请歌手 $a 演唱:《北京欢迎你》"
done
[root@localhost lianxi]# bash list_file.sh
config-3.10.0-1160.el7.x86_64 在/boot目录下
efi 在/boot目录下
grub 在/boot目录下
grub2 在/boot目录下
initramfs-0-rescue-8774cbc8b8da42ac9f19c4e528d01d72.img 在/boot目录下
initramfs-3.10.0-1160.el7.x86_64.img 在/boot目录下
symvers-3.10.0-1160.el7.x86_64.gz 在/boot目录下
System.map-3.10.0-1160.el7.x86_64 在/boot目录下
vmlinuz-0-rescue-8774cbc8b8da42ac9f19c4e528d01d72 在/boot目录下
vmlinuz-3.10.0-1160.el7.x86_64 在/boot目录下
[root@localhost lianxi]# vim list_file.sh
[root@localhost lianxi]# bash list_file.sh
config-3.10.0-1160.el7.x86_64 在/boot目录下
efi 在/boot目录下
grub 在/boot目录下
grub2 在/boot目录下
initramfs-0-rescue-8774cbc8b8da42ac9f19c4e528d01d72.img 在/boot目录下
initramfs-3.10.0-1160.el7.x86_64.img 在/boot目录下
symvers-3.10.0-1160.el7.x86_64.gz 在/boot目录下
System.map-3.10.0-1160.el7.x86_64 在/boot目录下
vmlinuz-0-rescue-8774cbc8b8da42ac9f19c4e528d01d72 在/boot目录下
vmlinuz-3.10.0-1160.el7.x86_64 在/boot目录下
有请歌手 李华 演唱:《北京欢迎你》
有请歌手 晓红 演唱:《北京欢迎你》
有请歌手 晓燕 演唱:《北京欢迎你》
有请歌手 冬梅 演唱:《北京欢迎你》
6.3 for循环和seq的结合
[root@localhost lianxi]# vim list_file_seq.sh
[root@localhost lianxi]# cat list_file_seq.sh
#!/bin/bash
#for循环和seq的结合
for i in $(seq 5)
do
echo "数字:$i"
done
#read和for、seq的结合使用
read -p "请输入数字:" num
for i in $(seq $num)
do
echo "数字是$i"
mkdir -p /root/lianxi/backup_0315/test$i
done
[root@localhost lianxi]# bash list_file_seq.sh
数字:1
数字:2
数字:3
数字:4
数字:5
请输入数字:4
数字是1
数字是2
数字是3
数字是4
[root@localhost lianxi]# ls /root/lianxi/backup_0315
test1 test2 test3 test4
本文介绍了多个Shell脚本练习,包括新建文件夹、删除文件、接受用户输入创建文件和统计文件行数。通过示例展示了如何使用mkdir、rm、read命令以及for循环和seq命令的结合,还涉及到了文件存在的判断和if语句的运用。这些练习涵盖了基本的文件操作和用户交互,是Shell脚本学习的基础部分。
582

被折叠的 条评论
为什么被折叠?



