当前位置: 首页> 文旅> 艺术 > 13.RedHat认证-Linux Shell脚本

13.RedHat认证-Linux Shell脚本

时间:2025/7/9 22:01:50来源:https://blog.csdn.net/CNNUMBER/article/details/139391839 浏览次数:0次

13.RedHat认证-Linux Shell脚本

Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本。

bash:是Linux标准默认的shell。bash由Brian Fox和Chet Ramey共同完成,是BourneAgain Shell的缩写

sh:由Steve Bourne开发,是Bourne Shell的缩写,sh 是Unix 标准默认的shell。

另外还有:ash、 csh、 ksh等

# 查看系统版本
[root@nfs-server ~]# cat /etc/centos-release 
CentOS Stream release 8# 查看内核版本信息
[root@nfs-server ~]# uname -a  # -a 是all的意思
Linux nfs-server 4.18.0-552.1.1.el8.x86_64 #1 SMP Fri Apr 26 18:59:23 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux# 查看当前系统使用的解释器
[root@nfs-server ~]# echo $SHELL
/bin/bash# 查看系统支持的shell解释器有哪些
[root@nfs-server ~]# cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash# 查看文件的大小或者目录的大小(以人类可读的方式)# 查看文件大小
[root@nfs-server ~]# du -h anaconda-ks.cfg 
4.0K    anaconda-ks.cfg#查看目录大小
[root@nfs-server opt]# ls
admin-test
[root@nfs-server opt]# du -h admin-test/ | sort -hr  # -h 人类可读的方式,-r 是从大到小排序

shell脚本初体验

# 建议加上后缀.sh,这样会有高亮显示
vim ./hello.sh
----------------------------
#!/bin/bash  
echo "hello world!!!!"
----------------------------
#!/bin/bash 
# 上面中的 #! 是一种约定标记, 它可以告诉系统这个脚本需要什么样的解释器来执行;# 执行脚本的几种方式
sh hello.sh  # 指定解释器执行该脚本。
bash hello.sh  #指定解释器执行该脚本
#请注意:指定解释器执行该脚本是不需要加上执行权限的。# 如果不指定解释器,那么就得赋予执行权限,然后以绝对路径或者相对路径执行脚本即可
chmod +x hello.sh
./hello.sh # 相对路径执行
/opt/hello.sh # 绝对路径执行
[root@nfs-server opt]# ./hello.sh 
hello world!!

shell变量

自定义变量

[root@nfs-server ~]# var=aaa # 自定义变量
[root@nfs-server ~]# echo $var  # 加上$ 输出变量的值
aaa
shell中,变量只能以字母或_开头,不能以数字开头,可以有特殊字符,但只能是 '_'#shell中一般我们定义变量是大写字母。
#小写字母有可能会和Linux命令冲突问题。
test="hello"
等号两边不能有空格。

特殊变量

#1.列出当前shell的进程ID
[root@nfs-server ~]# echo $$
3137
[root@nfs-server ~]# ps aux | grep bash
root        3137  0.0  0.7 226436  5404 pts/1    Ss   20:41   0:00 -bash
root        3374  0.0  0.1 222016  1112 pts/1    S+   21:01   0:00 grep --color=auto bash#2.$?能列出上次命令执行的状态#$?=0 是执行成功的标志,非0 就是不成功的标志
[root@nfs-server ~]# ls
anaconda-ks.cfg  hello.sh  initial-setup-ks.cfg  
[root@nfs-server ~]# echo $?
0# 3. 特殊变量演示
#!/bin/bash
#$0 表示脚本文件的执行路径,大多数情况会列出脚本的名字
echo "文件名: $0"
#$1表示传给脚本的第一个参数(后面的第二个参数等以此类推)
echo "第一个参数: $1"
#$#表示传给脚本参数的数量
echo "参数数量 $#"[root@nfs-server opt]# ./demo.sh 111
文件名: ./demo.sh
第一个参数: 111
参数数量 1

环境变量

系统环境变量
系统环境变量强烈建议没事不要改,他是保证系统稳定运行的关键。
#uptime列出系统运行时间
[root@nfs-server opt]# uptime 21:56:00 up  2:43,  2 users,  load average: 0.02, 0.02, 0.00# env 列出系统环境变量
[root@nfs-server opt]# env

条件判断

# 1.判断上一条ls命令是否执行成功
#!/bin/bash
ls
if [ "$?" == 0 ];thenecho "command execute success"
elseecho "command execute fail"
fi[root@nfs-server opt]# ./demo1.sh 
admin-test  demo1.sh  demo.sh
command execute success# 2. bash -x 可以列出脚本执行过程, 方便调试代码。
[root@nfs-server opt]# bash -x demo1.sh 
+ ls
+ '[' 0 == 0 ']'
+ echo 'command execute success'
command execute success# 3.判断文件是否存在 -f 就是判断文件是否存在的命令。
[root@nfs-server opt]# cat if.sh 
#!/bin/bash
if [ -f demo.sh ];thenecho "文件存在"
else echo "文件不存在"
fi
root@nfs-server opt]# ./if.sh 
文件存在# 4.一个简单的ping 脚本
[root@nfs-server opt]#  cat ping.sh 
#!/bin/bash
ping -c 1 "$1" &>>/dev/null
if [ "$?" == 0 ];thenecho "$1 ping is success!!!"
elseecho "$1 ping is fail"
fi

shell脚本的连接符

&& ||   逻辑运算符
&& 前后条件同时满足才为true
|| 前后条件满足一个即为true[root@nfs-server ~]# ping -c 1 www.baidu.com &>>/dev/null && echo "baidu ip upping"
baidu ip upping

shell脚本的分支语句

vim case.sh
------------------------------------
echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum # 读入一个值
case $aNum in 
1) echo 'You select 1';;
2) echo 'You select 2';;
3) echo 'You select 3';;
4) echo 'You select 4';;
*) echo 'You do not select a number between 1 to 4';;
esac   # 以esac结尾
--------------------------------------------

shell 脚本的循环

# for循环
#!/bin/bash
for i in {1..254}
doping 10.6.110.$i -c 1 &>>/dev/null && echo "$i is up" || echo "$i is down" &  # & 表示在后台执行。
done
wait
# wait是等循环执行完毕,再退出循环。(后台实际上是并行执行这些ping 操作的)
关键字:13.RedHat认证-Linux Shell脚本

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: