三、shell编程

文章介绍了Shell编程的基础知识,包括使用vim编辑器,cat命令查看文件内容,shell脚本的执行顺序,bash执行脚本的方法。详细讲解了变量的使用,if语句的结构和比较操作,以及for和while循环的用法。此外,还提到了位置变量的概念和重定向的运用。
摘要由CSDN通过智能技术生成

三、shell编程

3.1 shell编程是什么?

  1. shell编程时使用Linux命令来进行写程序的语言

脚本语言:写Linux里执行命令的脚本

脚本就是一个文件,里面有很多Linux命令+if +for+case+while等流控语言

  1. 脚本的好处:

可以批量的完成很多操作(如批量新建文件夹),提升效率,减少失误

3.2 vim

[root@localhost ~]# yum  install   vim   -y  安装vim软件

1.vim feirx.txt

(1)按i进入输入模insert)

(2)输入内容

(3)按esc键,离开输入模式,进入命令模式

  • yy :复制当前行
  • 3yy:从当前行开始往下复制3行,包括当前行
  • p:粘贴
  • dd:删除当前行
  • 2dd:从当前行开始往下删除2行
  • u:撤销
  • CTRL+R:重做

(4)末行模式:

  • 输入“wq” 退出并保存(write quit)
  • :q:退出 --》没有修改文件
  • :q!:强制退出,不保存

2.cat——查看文件里的内容

[root@localhost shell]# cat feirx.txt
linux
changsha
123456
查看系统版本
[root@xiaoweipf ~]# cat /etc/centos-release  查看centos系统的版本
CentOS Linux release 7.9.2009 (Core)

xw@xw-virtual-machine:~$ cat /etc/issue  在Ubuntu里查看系统的版本
Ubuntu 20.04.3 LTS \n \l

3.3 shell脚本执行顺序:

从上而下,一条一条的执行,如果某条命令执行出错,后面的命令继续执行!

[root@localhost init.d]# mkdir  /shell
[root@localhost init.d]# cd /shell/
[root@localhost shell]# ls
[root@localhost shell]# vim  first.sh

[root@localhost shell]# cat first.sh 
#!/bin/bash

echo "开始学习shell脚本了"
mkdir  -p /xiaowei/hengyang/leiyang
cd /xiaowei/hengyang/leiyang

#使用for循环批量新建文件和文件夹
for  i  in  {1..20}
do
	touch liuna$i
	mkdir caojb$i
done

#使用while循环批量新建文件和文件夹
i=1
while ((i<=20)) 
do
	touch  renxiaojing$i
	mkdir zhouxin$i
	((i++))
done

#if判断一个文件夹是否存在,如果不存在就新建
if  [ -d /sanchuang ];then
	echo  "/sanchuang 文件夹存在"
else
	mkdir  /sanchuang && echo "新建/sanchaung文件夹成功"
fi

[root@localhost shell]# [ -d /var ]  判断文件夹/var是否存在   directory

3.4 bash——执行脚本

root@localhost shell]# bash  first.sh 

[root@localhost shell]# cd /xiaowei/hengyang/leiyang/
[root@localhost leiyang]# ls
caojb1   caojb17  caojb6   liuna13  liuna20  renxiaojing1   renxiaojing17  renxiaojing6  zhouxin13  zhouxin20
caojb10  caojb18  caojb7   liuna14  liuna3   renxiaojing10  renxiaojing18  renxiaojing7  zhouxin14  zhouxin3
caojb11  caojb19  caojb8   liuna15  liuna4   renxiaojing11  renxiaojing19  renxiaojing8  zhouxin15  zhouxin4
caojb12  caojb2   caojb9   liuna16  liuna5   renxiaojing12  renxiaojing2   renxiaojing9  zhouxin16  zhouxin5
caojb13  caojb20  liuna1   liuna17  liuna6   renxiaojing13  renxiaojing20  zhouxin1      zhouxin17  zhouxin6
caojb14  caojb3   liuna10  liuna18  liuna7   renxiaojing14  renxiaojing3   zhouxin10     zhouxin18  zhouxin7
caojb15  caojb4   liuna11  liuna19  liuna8   renxiaojing15  renxiaojing4   zhouxin11     zhouxin19  zhouxin8
caojb16  caojb5   liuna12  liuna2   liuna9   renxiaojing16  renxiaojing5   zhouxin12     zhouxin2   zhouxin9

3.5 变量的使用

1.变量——variable

/var --》因为这个文件夹里的内容是可以变化的,例如:大小

2.实现的功能:

  • 传递信息
  • 临时存放数据的一个容器

3.变量的定义:

  • “=”左右不能有空格
[root@localhost shell]# sg="xiaowei"
[root@localhost shell]# bsg="caojb"
[root@localhost shell]# ssg = "xiaoniu"
-bash: ssg: 未找到命令
[root@localhost shell]# num1=10
[root@localhost shell]# num2=20

4.引用变量

  • $接变量名
[root@localhost shell]# mkdir  $sg
[root@localhost shell]# ls
first.sh  xiaowei
  • 若变量没有定义直接使用,默认是空值,不会报错,可以直接使用(可以不定义)

  • {}避免了变量的混淆,给变量划定了边界

[root@localhost shell]# echo $sg1

[root@localhost shell]# echo ${sg}1
xiaowei1

5.变量的命名

(1)、下划线命名法 --》使用非常多

​	user_name="feng"

(2)、驼峰命名法

UserName="feng"
  • 变量的名字里不能出现特殊符号

  • 区分大小写

  • 变量的名字不能以数字开头,包含特殊字符

    [root@localhost shell]# 6sg="gaohui"
    -bash: 6sg=gaohui: 未找到命令
    [root@localhost shell]# sg6="gaohui"
    [root@localhost shell]# sg>="gaohui"
    用法:sg 组 [[-c] 命令]
    

3.6 if语句

1.$?

代表上一条命令的返回值,0代表上一条命令执行成功;非0代表上一条命令执行失败。

[root@localhost shell]# fjdkjfdjkfdj
-bash: fjdkjfdjkfdj: 未找到命令
[root@localhost shell]# echo $?
127 #命令不存在

[root@localhost shell]# ls
first.sh  =gaohui  xiaowei
[root@localhost shell]# echo $?
0

[root@localhost shell]# mkdir
mkdir: 缺少操作数
Try 'mkdir --help' for more information.
[root@localhost shell]# echo $?
1

2.结构

(1)单分支
if  条件 ;then
	命令
fi
#或
if  条件
then
	命令
fi
  • ; 命令连接符号
    命令1 ; 命令2 先执行命令1然后执行命令2 ,在一行里可以执行多条命令,不管前面的命令执行是否成功都会执行后面的命令
[root@localhost shell]# mkdir feng ; echo yes;echo no
mkdir: 无法创建目录"feng": 文件已存在
yes
no
(2)双分支
if  条件 ;then
	命令1
else
	命令2
fi
(3)多分支
if  条件1 ;then
	命令1
elif 条件2;then
	命令2
elif 条件3;then
	命令3
else
	命令4
fi

3.比较

  • [[]] 进行字符串的比较
[root@localhost shell]# cat if.sh 
#!/bin/bash

#接受用户的输入
read  -p "请输入用户名:"  user_name # #读取命令前,有提示消息

if [[ $user_name == "root" ]];then
        echo "你是管理员root"
elif [[  $user_name == "admin" ]];then
        echo "你是管理员admin"
elif [[ $user_name == "sg" ]];then
        echo "你是帅哥sg"
else
        echo "你是一个普通的用户"
fi

[root@localhost shell]# bash if.sh 
请输入用户名:root
你是管理员root
[root@localhost shell]# bash if.sh 
请输入用户名:admin
你是管理员admin
[root@localhost shell]# bash if.sh 
请输入用户名:sc
你是一个普通的用户
  • (()) 进行数值的比较,默认只能进行整数的比较和运算
[root@localhost shell]# a=10
[root@localhost shell]# b=20
[root@localhost shell]# (($b > $a))
[root@localhost shell]# echo $?
0
[root@localhost shell]# (($a > $b))
[root@localhost shell]# echo $?
1
  • &&逻辑与

    ||逻辑或

[root@localhost shell]# (($b >10 && $b<100))
[root@localhost shell]# echo $?
0
[root@localhost shell]# (($b >50 && $b<100))
[root@localhost shell]# echo $?
1
[root@localhost shell]# 
[root@localhost shell]# (($b >=50 || $b<=100))
[root@localhost shell]# echo $?
0

3.7 for循环

1.经典案例

(1)continue :

结束本次循环,不再执行后面的语句,开始下一次循环

#!/bin/bash

for  i  in {1..10}
do
	if (( $i == 6 ));then
		touch wuyangming$i.txt
		continue
	fi
	mkdir -p  /shell/wuyangming$i  
	#-p递归建文件夹,如果文件夹已存在,则不报错
done
[root@localhost shell]# bash for.sh 
[root@localhost shell]# ls
feirx.txt  lianxi.sh  machi3  machi7       wuyangming10  wuyangming5      wuyangming9
first.sh   machi1     machi4  machi8       wuyangming2   wuyangming6.txt  xiaowei
for.sh     machi10    machi5  machi9       wuyangming3   wuyangming7
if.sh      machi2     machi6  wuyangming1  wuyangming4   wuyangming8


  • 从结果可以看出,新建了wuyangming6.txt,但是没有wuyangming6。当i=6时,跳出了for的这次循环,继续执行i=7,8,9,10.
(2)break :
  • 跳出整个循环,执行循环后面的语句
#!/bin/bash

for  i  in {1..10}
do
	if (( $i == 6 ));then
		touch renxiaojing$i.txt
		break
	fi
	mkdir -p  /shell/renxiaojing$i
done

mkdir  /shell/renxj_end
[root@localhost shell]# bash for.sh 
[root@localhost shell]# ls
feirx.txt  machi10  machi7        renxiaojing4      wuyangming2      wuyangming8
first.sh   machi2   machi8        renxiaojing5      wuyangming3      wuyangming9
for.sh     machi3   machi9        renxiaojing6.txt  wuyangming4      xiaowei
if.sh      machi4   renxiaojing1  renxj_end         wuyangming5
lianxi.sh  machi5   renxiaojing2  wuyangming1       wuyangming6.txt
machi1     machi6   renxiaojing3  wuyangming10      wuyangming7

  • 从结果可以看出,新建了renxiaojing{1…5}和renxiaojing6.txt和renxj_end,但是没有renxiaojing{6…10}。说明当i=6时,遇到break跳出了整个循环,继续执行循环外的mkdir /shell/renxj_end

2.双重for循环

#!/bin/bash

for  i  in {1..5}
do
	for a in {1..3}
	do
		echo  "$i $a"
	done
done
[root@www shell]# bash -x for2.sh #-x可以显示代码执行的过程
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
5 1
5 2
5 3

3.8 while循环

1.控制循环次数

[root@localhost shell]# vim while.sh
#!/bin/bash

i=1
while (( $i <= 10))
do
	echo $i
	((i++))
done
[root@localhost shell]# bash while.sh 
1
2
3
4
5
6
7
8
9
10

2.死循环

[root@localhost shell]# vim while2.sh
#!/bin/bash
i=1
#while true
while :
do
        echo $i
        ((i++))
        #暂停1秒
        sleep 1
done
[root@localhost shell]# bash while2.sh 

1
2
3
4
^C
#按ctrl+c退出,这是隔一秒出现一个

练习:

使用for循环编写一个九九乘法表
#!/bin/bash

for (( i=1;i<=9;i++ ))
do
        for(( j=1;j<=$i;j++ ))
        do
                echo -n -e" $i*$j=$((i*j))\t" 
                #-n 表示不换行打印,-e 表示有转译符, \t 表示空格
        done
        echo
done

[root@localhost shell]# vim for3.sh
[root@localhost shell]# bash for3.sh 
 1*1=1	
 2*1=2	 2*2=4	
 3*1=3	 3*2=6	 3*3=9	
 4*1=4	 4*2=8	 4*3=12	 4*4=16	
 5*1=5	 5*2=10	 5*3=15	 5*4=20	 5*5=25	
 6*1=6	 6*2=12	 6*3=18	 6*4=24	 6*5=30	 6*6=36	
 7*1=7	 7*2=14	 7*3=21	 7*4=28	 7*5=35	 7*6=42	 7*7=49	
 8*1=8	 8*2=16	 8*3=24	 8*4=32	 8*5=40	 8*6=48	 8*7=56	 8*8=64	
 9*1=9	 9*2=18	 9*3=27	 9*4=36	 9*5=45	 9*6=54	 9*7=63	 9*8=72	 9*9=81	

3.9 位置变量

  • ——给脚本传递参数(position variable)
[root@yizhen shell]# vim pos_var.sh
[root@yizhen shell]# cat pos_var.sh 
#!/bin/bash

echo "第1个位置变量是$1"
echo "第2个位置变量是$2"
echo "第3个位置变量是$3"
echo "第4个位置变量是$4"
echo "第5个位置变量是$5"
echo "一共有 $# 个位置变量"
echo "所有的位置变量的内容是 $*"
echo "所有的位置变量的内容是 $@"

[root@yizhen shell]# bash pos_var.sh panlinfeng zhengyani machi gaohui xiaowei liquan第1个位置变量是panlinfeng
第2个位置变量是zhengyani
第3个位置变量是machi
第4个位置变量是gaohui
第5个位置变量是xiaowei
一共有 6 个位置变量
所有的位置变量的内容是 panlinfeng zhengyani machi gaohui xiaowei liquan
所有的位置变量的内容是 panlinfeng zhengyani machi gaohui xiaowei liquan

3.10

:%s/sc/username/g
  • %代表所有的行
  • s 替换操作 substitute
  • /sc/username/ 替换模式,前者为old word,后者为new word
  • g 一行里有多个替换 global

3.11 重定向

redirect 将屏幕上的输出重新定向到文件里

——》作用:可以帮我们保存脚本里的变量或者其他的结果内容到文件里

1、标准的正确输出 --》输出到屏幕

  • >:正确的输出重定向,如果后面接的文件存在就覆盖里面的内容,如果不存在就新建文件
  • >>:追加正确的输出重定向,不覆盖,只在文件的末尾追加,若不存在则新建文件

2、标准的错误输出 ——》输出到屏幕

  • 2> :错误的输出重定向,如果后面接的文件存在就覆盖里面的内容,如果不存在就新建文件
  • 2>>:追加错误的输出重定向,不覆盖,只在文件的末尾追加,若不存在则新建文件

3、

  • &>:不管正确的还是错误的都往一个文件里保存,会覆盖原来文件里的内容
    • &>/dev/null:废弃文件
  • &>>:不管正确的还是错误的都往一个文件里保存,不会覆盖原来文件里的内容

4、重定向不影响返回值

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码精灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值