[root@som ~]# cat /etc/shells #查看shell解释器
[root@som ~]# usermod -s /bin/tcsh test #使用usermod指令修改用户解释器
[root@som ~]# grep test /etc/passwd #从/etc/passwd过滤test用户信息
[root@som ~]# ls #数据有颜色区分
[root@som ~]# ls > a.txt
[root@som ~]# cat a.txt
[root@som ~]# ls >> a.txt
[root@som ~]# cat a.txt
[root@som ~]# ls xxyyzz.txt
ls: 无法访问xxyyzz.txt: 没有那个文件或目录
[root@som ~]# ls xxyyzz.txt > b.txt #失败,> 收集正确信息
ls: 无法访问xxyyzz.txt: 没有那个文件或目录
[root@som ~]# ls xxyyzz.txt 2> b.txt #正确,2> 收集错误信息
[root@som ~]# cat b.txt
ls: 无法访问xxyyzz.txt: 没有那个文件或目录
[root@som ~]# ls a.txt xxyzz.txt > b.txt
[root@som ~]# ls a.txt xxyzz.txt &> b.txt #收集所有信息
[root@som ~]# cat b.txt
ls: 无法访问xxyzz.txt: 没有那个文件或目录
a. txt
[root@som ~]# ifconfig | head -2 #查看ip信息前两行
什么是shell脚本
[root@som ~]# mkdir -p /root/shell/day0{1..2}
[root@som ~]# vim /root/shell/day01/first.sh
echo "Hello World"
[root@som ~]# chmod +x /root/shell/day01/first.sh #添加执行权限
[root@som ~]# /root/shell/day01/first.sh #绝对路径形式执行
Hello World
[root@som ~]# cd /root/shell/day01/ #切换目录
[root@som day01]# ./first.sh #相对路径执行
Hello World
优化刚刚的first.sh脚本
[root@som day01]# vim /root/shell/day01/first.sh
#!/bin/bash #指定解释器
#This a test program for shell. #这是一个测试性的程序
echo "Hello World"
[root@som day01]# ./first.sh #执行脚本
执行脚本的多种方式
脚本在执行的时候要有执行(x)权限,否则会报错
[root@som day01]# chmod -x first.sh
[root@som day01]# ./first.sh #报错
-bash: ./first.sh: 权限不够
[root@som day01]# /root/shell/day01/first.sh #报错
-bash: /root/shell/day01/first.sh: 权限不够
不需要文件有可执行权限,指定解释器执行脚本
[root@som day01]# sh first.sh #指定sh来执行first.sh
[root@som day01]# bash first.sh #指定bash解释器执行first.sh
[root@som day01]# vim tmp.sh #编写tmp.sh
#!/bin/bash
exit
[root@som day01]# sh tmp.sh #指定运行脚本,没有关闭终端
[root@som day01]# vim tmp.sh #编写tmp.sh
#!/bin/bash
exit
[root@som day01]# source tmp.sh #执行tmp.sh,会关闭终端
总结:
环境变量(变量名通常大写,有操作系统维护)
位置变量(bash内置变量,存储脚本执行时的参数)
预定义变量(bash内置变量,可以调用但是不能赋值或修改)
自定义变量(用户自主设置)
定义变量
可以是数字,字母,下划线
变量名不能使用特殊符号,会报错
不能以数字开头
查看变量
定义变量
[root@som ~]# a=11
[root@som ~]# echo $a #调用变量,查看变量的值
[root@som ~]# a=33 #变量名已经存在,再次赋值,里面的内容会被覆盖
[root@som ~]# echo $a #调用变量,查看变量的值
[root@som ~]# a)=11 #变量包含特殊符号,所以定义失败
-bash: 未预期的符号 `)' 附近有语法错误
[root@som ~]# 3a=33 #变量数字开头,所以定义失败
bash: 3a=33: 未找到命令...
[root@som ~]# a_0=11 #没有违规,所以成功
[root@som ~]# _a=11 #没有违规,所以成功
[root@som ~]# _0=11 #没有违规,所以成功
[root@som ~]# x=CentOS
[root@som ~]# echo $x #成功
[root@som ~]# echo ${x} #成功
若想要显示CentOS7.9
[root@som ~]# echo $x7.9 #失败,会显示.9,此时是把$x7看成一个变量名
加上{}可以成功
[root@som ~]# echo ${x}7.9 #输出CentOS7.9
[root@som ~]# echo ${x}7.6 #输出CentOS7.6
取消变量
[root@som ~]# unset x #取消变量
[root@som ~]# echo $x
[root@som ~]# echo $PATH #命令搜索的路径变量
[root@som ~]# echo $PWD #返回当前工作目录
/root
[root@som ~]# echo $USER #显示当前登录的用户
root
[root@som ~]# echo $UID #显示当前用户的uid
0
[root@som ~]# echo $HOME #显示当前用户的家目录
/root
[root@som ~]# echo $SHELL #显示当前的SHELL
/bin/bash
[root@som ~]# vim /root/shell/day01/vars.sh
#!/bin/bash
echo $1
echo $2
echo $3
[root@som ~]# chmod +x /root/shell/day01/vars.sh
[root@som ~]# /root/shell/day01/vars.sh aa bb cc #执行脚本,传递参数
aa
bb
cc
[root@som ~]# ls /etc/hosts #执行命令成功
/etc/hosts
[root@som ~]# echo $? #返回值为0,正确
0
[root@som ~]# ls /xxxxxyyyy #执行命令错误
ls: 无法访问/xxxxxyyyy: 没有那个文件或目录
[root@som ~]# echo $? #返回值为非0,失败
2
其他几个预定义变量的测试
[root@som7 ~]# chmod +x /root/shell/day01/pre.sh
[root@som7 ~]# /root/shell/day01/pre.sh a b c d
/root/shell/day01/pre.sh
46608
4
a b c d
[root@som ~]# touch a b c #创建了三个文件
[root@som ~]# touch "a b c" #创建1一个文件
[root@som ~]# ls -l
[root@som ~]# rm -rf a b c #删除三个文件
[root@som ~]# rm -rf "a b c" #删除一个文件
[root@som ~]# hi="world"
[root@som ~]# echo "$hi" #成功
world
[root@som ~]# echo '$hi' #失败,当成一个字符串
$hi
当没有特殊符号时,单引号和双引号的含义是一样的
[root@som ~]# touch "a b c"
[root@som ~]# touch 'c d e'
练习单引号和双引号的区别
[root@som ~]# echo "$USER id is $UID" #调用变量
root id is 0
[root@som ~]# echo '$USER id is $UID' #不调用变量
$USER id is $UID
[root@som ~]# grep root /etc/passwd
[root@som ~]# test=`grep root /etc/passwd` #定义变量,内容为命令输出结果
[root@som ~]# echo $test
[root@som ~]# test2=$(grep root /etc/passwd) #定义变量,内容为命令输出结果
[root@som ~]# echo $test2
read基本用法
[root@som ~]# read iname #定义变量iname
123 #从键盘输入123作为iname的值
[root@som ~]# echo $iname #输出变量iname
123
虽然可以赋值。但是屏幕上没有任何提示信息,在未来写脚本的时候不太方便,可以加上-p选项,给出提示
[root@som ~]# read -p "请输入用户名:" iname #定义变量
请输入用户名:tom
[root@som ~]# echo $iname #输出变量
tom
创建一个脚本,通过read定义变量创建用户,更改密码
[root@som ~]# vim /root/shell/day01/read.sh
#!/bin/bash
read -p "请输入用户名:" name
read -p "请输入密码:" pass
useradd $name
echo "$pass" | passwd --stdin $name
[root@som ~]# chmod +x /root/shell/day01/read.sh
[root@som ~]# /root/shell/day01/read.sh
请输入用户名:user2
请输入密码:a
更改用户 user2 的密码 。
passwd:所有的身份验证令牌已经成功更新。
但是此时密码是名为显示的,不安全,可以使用-s参数,不显示终端输入的信息
[root@som ~]# vim /root/shell/day01/read.sh
read -p "请输入用户名:" name
read -s -p "请输入密码:" pass
useradd $name
echo "$pass" | passwd --stdin $name
[root@som ~]# /root/shell/day01/read.sh
请输入用户名:user3
请输入密码:
更改用户 user3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@som ~]# read -t 3 iname #3秒不输入直接退出
[root@som ~]# [ $USER == root ] #环境变量USER的值是root吗
[root@som ~]# echo $?
0
[root@som ~]# [ $USER == tom ] #环境变量USER的值是tom吗
[root@som ~]# echo $?
1
[root@som ~]# [ $USER != tom ] #环境变量USER的值不是tom
[root@som ~]# echo $?
0
格式:[ 整数值1 操作符 整数值2 ]
参与比较的必须是整数(可以调用变量),比较非整数值时会出错:
四则运算:+ - * /
求模取余:%
[root@som ~]# echo $[1+1] #计算1+1
[root@som ~]# echo $[10-2] #计算10-2
[root@som ~]# echo $[2*2] #计算2*2
[root@som ~]# echo $[6/3] #计算6/3
[root@som ~]# echo $[10%3] #取10/3的余数
[root@som ~]# a=10
[root@som ~]# b=20
[root@som ~]# echo $[a+b] #计算变量a+变量b
小于
[root@som ~]# [ 3 -lt 8 ]
[root@som ~]# echo $?
0
大于
[root@som ~]# [ 3 -gt 2 ]
[root@som ~]# echo $?
0
等于
[root@som ~]# [ 3 -eq 3 ]
[root@som ~]# echo $?
0
## 文件状态的测试
```shell
1. -e 判断对象是否存在(不管是目录还是文件),存在则结果为真
[root@som ~]# [ -e /etc ]
[root@som ~]# echo $ 返回0表示存在
2. -d 判断对象是否为目录(存在且是目录),是则为真
[root@som ~]# [ -d /etc/hosts ]
[root@som ~]# echo $ 返回0表示存在
3. -f 判断对象是否为文件(存在且是文件)是则为真
[root@som ~]# [ -f /etc/hosts ]
[root@som ~]# echo $? 返回0表示存在
4. -r 判断对象是否可读,是则为真
5. -w 判断对象是否可写,是则为真
6. -x 判断对象是否具有可执行权限,是则为