shell基础知识
- shell是linux的外壳,它包在linux内核的外面,为用户和内核之间的交互提供一个接口
- 当用户下达指令给操作系统,实际上是把指令告诉给shell,经过shell解释,处理后内核做出相应的动作
- 系统的回应和输出信息由shell处理,然后显示在用户屏幕上
- 查看系统默认shell
方法一:
[root@station mnt]# echo $SHELL
/bin/bash
方法二:
[root@station mnt]# cat /etc/passwd | head -1
root:x:0:0:root:/root:/bin/bash
方法三:
[root@station mnt]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- shell脚本的建立
#!/bin/bash ###指定解释器:由哪个程序来执行脚本内容,必须写在第一行
#! :幻数
echo 打印
例1:编写脚本,输出hello world
[root@station mnt]# vim test.sh
[root@station mnt]# cat test.sh
#!/bin/bash
echo "hello world"
[root@station mnt]# sh test.sh
hello world
例2:清空/var/log/messages的日志文件
[root@station mnt]# vim log.sh
#!/bin/bash
cd /var/log
cat /dev/null > messages
echo "Logs has been cleaned up"
####上述脚本缺陷:
1、上述脚本缺少用户判断,任何用户都可执行这个脚本
2、没有控制流程,只是把简单的命令进行顺序操作,没有执行是否成功判断
####对上面脚本加以改进
#!/bin/bash
ROOT_UID=0
LOG_DIR=/var/log
####判断是否是超级用户,如果不是,输出错误提示信息
if [ "$UID" -ne "ROOT_UID" ];then
echo "Error:you should be root to run this script"
exit 1
fi
####判断是否能进入/var/log目录,如果不允许,输出提示信息
cd $LOG_DIR || {
echo "Cannot access to directory"
exit 1
}
####清空日志,输出清空完成提示信息
cat /dev/null > messages && {
echo "logs cleaned up"
exit 0
}
####清空失败,退出
echo "Logs cleaned failed"
exit 1
练习:编写脚本实现httpd自动安装并且开机自动启动
[root@station mnt]# vim test.sh
#!/bin/bash
yum install httpd -y &> /dev/null
systemctl start httpd
systemctl enable httpd &> /dev/null
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
echo "Httpd has been isntalled finished" ###安装完成提示用户信息
- 脚本执行方法
1、sh或者bash方式sh或者bash方式
sh script.sh | bash script.sh ##没有执行权限时
[root@station mnt]# vim test.sh
[root@station mnt]# cat test.sh
#!/bin/bash
echo "hello"
[root@station mnt]# sh test.sh
hello
[root@station mnt]# bash test.sh
hello
2、path/或者./方式 必须给编写的脚本可执行权限
path/script.sh | ./script.sh ##绝对路径,当前目录下sh ./不是在当前shell执行,会打开新shell执行,完成后退出
[root@station mnt]# /mnt/test.sh
-bash: /mnt/test.sh: Permission denied
[root@station mnt]# chmod +x test.sh
[root@station mnt]# /mnt/test.sh
hello
[root@station mnt]# ./test.sh
hello
3、source方式
source script.sh | . script.sh ##使用source或. 来读指定shell文件,并会把其他shell中的变量值执行结果返回父shell继续使用
[root@station mnt]# source test.sh
hello
[root@station mnt]# . test.sh
hello
4、三种方式区别
[root@station mnt]# vim test.sh
[root@station mnt]# cat test.sh
#!/bin/bash
username=`whoami`
[root@station mnt]# sh test.sh
[root@station mnt]# echo $username
[root@station mnt]# /mnt/test.sh
[root@station mnt]# echo $username
[root@station mnt]# source test.sh
[root@station mnt]# echo $username
root
source或. 来读指定shell文件,会把其他shell中的变量值返回父shell继续使用
- shell脚本规范
#!/bin/bash ###指定解释器
#Date:2018-12-23 ###日期
#Author:sql ###作者
#Mail:0302san@gmail.com ###邮箱
#Desc:This script is for ... ###脚本描述
#Version: 1.1 ###版本
- 自动生成脚本声明
[root@station mnt]# vim /etc/vimrc
66 autocmd BufNewFile *.sh exec ":call WESTOS()"
67 func WESTOS()
68 call append(0,"# Author: sql".(" "))
69 call append(1,"# Version: ".(" "))
70 call append(2,"# Mail: ".(" "))
71 call append(3,"# Date: ".strftime("%Y-%m-%d").(" "))
72 call append(4,"# Description: ".(" "))
73 call append(5,"# ".(" "))
74 call append(6," ")
75 call append(7,"#!/bin/bash")
76 endfunc
示例:
[root@station mnt]# vim auto.sh
[root@station mnt]# cat auto.sh
# Author: sql
# Version:
# Mail:
# Date: 2018-12-23
# Description:
#
#!/bin/bash
- shell脚本的执行过程
1、加载系统环境变量
env查看系统环境变量
2、一条一条命令执行,遇到子脚本,先执行子脚本,然后返回父脚本继续执行
[root@station mnt]# env
XDG_SESSION_ID=1
HOSTNAME=station.domain155.example.com
TERM=xterm-256color
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=172.25.254.55 35006 22
SSH_TTY=/dev/pts/0
USER=root
...