查看系统shell:
[root@localhost rc.d]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
查看系统默认shell:
[root@localhost rc.d]# echo $SHELL
/bin/bash
[root@localhost rc.d]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
查看系统的bash版本:
[root@localhost rc.d]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost rc.d]# bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
查看bash是否安全
(两年老版本的bash被暴露出存在较为严重的安全信息,凭借漏洞,攻击者可能会接管计算机的整个系统。)
[root@localhost rc.d]# env x='(){:;};echo be careful' bash -c "echo this is a test"
this is a test
————>如果返回be careful,则需要进行升级系统
升级bash版本
[root@localhost rc.d]yum update bash
[root@localhost rc.d]rpm -qa bash
2、shell脚本的建立与执行
当shell脚本运行时,它首先查找系统环境变量ENV,该变量制定了环境变量(加载顺序通常是/etc/profile、/etc/profile.d、/.bash_profile、/.bashrc、/etc/bashrc、/sysconfig等),在加载了以上环境变量文件后,shell就开始执行shell脚本中的内容
加载(su - username):/etc/profile、/etc/profile.d、/.bash_profile、/.bashrc、/etc/bashrc、/sysconfig
不加载(su username):/etc/profile.d、/.bash_profile、/.bashrc、/etc/bashrc、/sysconfig直接登陆,不加载用户名
3、shell脚本注释
一行代码使用“#”进行注释
如果是多行代码都需要进行注释,则使用
:<<BLOCK
。。。
BLOCK
4、shell脚本规范
-
开头指定脚本解释器
#!/bin/bash或者#!/bin/sh
-
以 . sh结尾
变量类型:
变量可以分为环境变量和普通变量
环境变量(全局变量):可以在会创建他们的shell以及派生出来的任意子进程shell中使用,又可分为:自定义环境变量和bash内的环境变量
普通变量(局部变量):只能在创建他们的shell函数或shell脚本中使用
环境变量的名字均采用大写形式
变量的导出:
set:输出所有变量
env:输出全局变量
declare:输出所有变量、函数、和已经到处的变量
全局变量用export修饰
把变量加载为全局变量
. spname
source spname
执行文件,进程结束后,变量失效
. /spname
sp的绝对路径/
引号的区别:
[root@localhost ~]# a=192.168.1.1
[root@localhost ~]# a=192.168.1.1-$a
[root@localhost ~]# b='192.168.1.1-$a'
[root@localhost ~]# c="192.168.1.1-$a"
[root@localhost ~]# echo $a
192.168.1.1-192.168.1.1
[root@localhost ~]# echo $b
192.168.1.1-$a
[root@localhost ~]# echo $c
192.168.1.1-192.168.1.1-192.168.1.1
-f 绝对路径:判断文件是否存在,如果存在为真,不存在为假
-d:判断目录
-m:不分文件或者目录
shell脚本之特殊变量:
-
$0:获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,那么就包括脚本路径。
-
{10},接的参数以空格隔开
-
$#:获取当前执行的shell脚本后接的参数的总个数
-
@相同,如果给$ *加上双引号,例如:
- “$ *”,则表示将所有参数是为单个字符串,相当于“$1 $2 $3”
-
*相同,如果给$@加上双引号,例如:
- “$@”,则表示将所有的参数视为不同的独立字符串,相当于“$1”"$2""$3",这是将多参数传递给其他程序的最佳方式,因为他会保留所有的内嵌在每个参数里的任何空白。
当“*”都加双引号时,两者是有区别的,都不加双引号时,两者无区别。
[root@localhost ~]# set -- this "is a" test
[root@localhost ~]# echo $#
3
[root@localhost ~]# echo $1
this
[root@localhost ~]# echo $2
is a
[root@localhost ~]# echo $3
test
[root@localhost ~]# echo $*
this is a test
[root@localhost ~]# echo $@
this is a test
[root@localhost ~]# for i in "$*";do echo $i;done
this is a test
[root@localhost ~]# for i in "$@";do echo $i;done
this
is a
test
[root@localhost ~]# shift----->将位置参数左移一位,shift 3表示原来的$4变成了现在的$1.丢弃原来的$1.
[root@localhost ~]# echo $#
2
[root@localhost ~]# echo $1
is a
[root@localhost ~]# echo $2
test
[root@localhost ~]# echo $3
-
$?:获取执行上一条指令的执行状态返回值
- 判断命令、脚本、函数等程序是否执行成功 。
- 若在脚本中调用执行“exit 数字”,则会否会这个数字给“$?”变量。
- 如果是在函数里,则通过“return 数字”把这个数字以函数返回值的形式传给“$?”。
-
$$:获取当前执行的shell脚本的进程号(PID)
-
$!:获取上一个在后台工作的进程的进程号(PID)
-
$ _ : 获取在此之前执行的命令或脚本的最后一个参数
shell内置变量
echo:
- -n:不换行输出内容
- -e:解析转义字符(见下面的字符)
- \n:换行
- \r:回车
- \t:制表符
- \b:退格
- \v:纵向制表符
exec:该命令在不创建新的子进程前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程(也就是最初的设立了)也就终止了
来源:CSDN
作者:梁欣伟
链接:https://blog.csdn.net/qq_42508901/article/details/104109544