编写并使用Shell脚本
1、 编写一个简单的脚本
[root@centos01 ~]# vim first.sh
#!/bin/bash
#This is my first Shell-Script.
cd /boot
echo “当前的目录位于:”
pwd
echo “其中以vml开头的文件包括:”
ls -lh vml*
[root@centos01 ~]# chmod +x first.sh
2、 重定向与管道操作
1) 重定向输出
[root@centos01 ~]# uname -p > kernel.txt
[root@centos01 ~]# uname -r >> kernel.txt
2) 重定向输入
[root@centos01 ~]# vim pass.txt
123456
[root@centos01 ~]# passwd --stdin root < pass.txt
管道操作
提取前
[root@centos01 ~]# grep “/bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 101: …oot@centos01 ~]#̲ grep "/bin/bas…” /etc/passwd | awk -F: ‘{print $1,$7}’
提取前
提取后
3、 定义新的变量
1) 定义新的变量
[root@centos01 ~]# Product=Puthon
[root@centos01 ~]# Version=2.7.13
查看和引用变量值
2) 变量赋值的特殊操作
错误的赋值
正确的赋值
3) 单引号
4)反撇号
4) read命令
4、 特殊shell变量
1) 环境变量
确认脚本位置
查看当前搜索路径
直接执行时找不到命令
将/root添加到搜索路径
直接以文件名运行脚本
2) 位置变量
[root@centos01 ~]# vim aaaa.sh
#!/bin/bash
SUM=expr $1 + $2
echo “$1 + $2 = $SUM”
[root@centos01 ~]# chmod +x aaaa.sh