shell入门基础

本文详细介绍Linux Shell的基础知识,包括Shell作为用户与内核间接口的作用,如何查看默认Shell,以及Shell脚本的创建与执行方法。通过实例演示了简单脚本的编写,如输出文本、清空日志文件及HTTPD服务的自动安装和启动。同时,讲解了不同执行方式的区别及脚本规范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shell基础知识

  1. shell是linux的外壳,它包在linux内核的外面,为用户和内核之间的交互提供一个接口
  2. 当用户下达指令给操作系统,实际上是把指令告诉给shell,经过shell解释,处理后内核做出相应的动作
  3. 系统的回应和输出信息由shell处理,然后显示在用户屏幕上
  4. 查看系统默认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
  1. 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. 脚本执行方法

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继续使用

  1. shell脚本规范
#!/bin/bash			###指定解释器
#Date:2018-12-23		###日期
#Author:sql			###作者
#Mail:0302san@gmail.com		###邮箱
#Desc:This script is for ...	###脚本描述
#Version: 1.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
  1. 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
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值