shell脚本基础知识技能讲解

本文介绍Shell作为Linux用户操作系统的桥梁,不仅是一种命令语言,也是一种程序设计语言。文章详细讲解了Shell的种类,如Bourne Shell、Bash、C Shell等,并提供了Shell脚本示例,包括脚本声明、调试技巧、执行路径查看、后台运行和脚本安全性等方面的知识。

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

shell简介

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

Linux 的

Shell 种类众多,常见的有:

Bourne Shell(/usr/bin/sh或/bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
Shell for Root(/sbin/sh)

示例

shell脚本声明自动形成

etc/vimrc
###
map <F9> ms:call WESTOS()<cr>'s
autocmd BufNewFile *.sh,*.script exec ": call WESTOS()"

function WESTOS()
         call append(0,"##########################")
         call append(1,"# Author                 #")
         call append(2,"# Create_Time:           #")
         call append(3,"# Version:               #")
         call append(4,"# Mail:                  #")
         call append(5,"# Description            #")
         call append(6,"#                        #")
         call append(7,"#                        #")
         call append(8,"##########################")
         call append(9,"#!/bin/bash")
endfunction
###
若想显示当前时间
call append(2,"# Create_Time:  ".strftime("%Y-%m-%d").("          #")

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

脚本调试

[root@des ~]# sh -x /tmp/test.sh 
###查看矫正,脚本调试
[root@des mnt]# vim /tmp/test.sh
#!/bin/bash                                 
##指定执行shell解释器,且只能在第一行,否则会被认作注销
ping 172.25.254.70 && echo yes || echo no		     ##执行的相应命令
###
[root@des mnt]# sh -x /tmp/test.sh
+ ping 172.25.254.70                                          ##执行内容
PING 172.25.254.70 (172.25.254.70) 56(84) bytes of data.
64 bytes from 172.25.254.70: icmp_seq=1 ttl=64 time=0.190 ms
64 bytes from 172.25.254.70: icmp_seq=2 ttl=64 time=0.193 ms
^C
--- 172.25.254.70 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.190/0.191/0.193/0.013 ms
+ echo yes						      ##执行成功,命令前                                                                有+号
yes							      ##输出执行结果

在这里插入图片描述

查看脚本执行路径

[root@des mnt]# vim test.sh 
watch -n 1 date
[root@des mnt]# sh test.sh 
###打入后台(ctrl+z)
[root@des mnt]# ps f
###查看

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
还原

[root@des mnt]# fg(ctrl+c)
###查看后台程序,并结束
[root@des mnt]# ps f
###查看是否成功

在这里插入图片描述

执行脚本

sh 直接执行脚本,指定环境(开启新的shell)
./test.sh(=当前绝对路径) 绝对路径执行,)当前环境生效(当前shell)
source( . ) 需要传递变量或函数时使用

实验

[root@des mnt]# /mnt/test.sh
-bash: /mnt/test.sh: Permission denied
[root@des mnt]# chmod +x /mnt/test.sh 
[root@des mnt]# /mnt/test.sh
###自动执行

在这里插入图片描述
还原

   [root@des mnt]# ps f
   [root@des mnt]# fg(ctrl+c)

在这里插入图片描述

[root@des mnt]# vim test.sh 
echo $a
[root@des mnt]# sh test.sh
[root@des mnt]# source test.sh  	###只有source显示传递了变量
[root@des mnt]# . test.s

在这里插入图片描述
在这里插入图片描述

[root@des mnt]# vim log_clean.sh 
###
#!/bin/bash
cd /var/log
> messages			###清空日至信息
echo "logs cleaned up..."       ###指定输出信息logs cleaned up...

在这里插入图片描述
在这里插入图片描述

[root@des mnt]# su - student
[student@des mnt]$ sh log_clean.sh  ###有报错,但仍然可以执行
[student@des mnt]$ ls -l /mnt/
###脚本安全性低

在这里插入图片描述
在这里插入图片描述

输入具有暂时性

[root@des mnt]# a=1
[root@des mnt]# vim test.sh 
echo $a
[root@des mnt]# sh test.sh 
###无输出
[root@des mnt]# echo $a
1
###输出设定a的值,a的值存在
######
说明可显示a的值的环境与执行sh环境不同
sh会打开新的shell,所以无法检测到a的值

在这里插入图片描述

[root@des mnt]# export a=1
[root@des mnt]# sh test.sh 
1
###a的值可显示,export命令为分享型命令,执行sh时a的值可显示

在这里插入图片描述

[root@des mnt]# exit
logout
Connection to 172.25.254.101 closed.
[kiosk@foundation1 ~]$ ssh root@172.25.254.101
root@172.25.254.101's password: 
Last login: Fri Jun  7 02:00:50 2019 from 172.25.254.1
[root@des ~]# cd /mnt/
[root@des mnt]# sh test.sh 
###a的值不显示,之前资源被回收

在这里插入图片描述

历史记录的清除

[root@des mnt]# cd -
/root
[root@des ~]# ls
[root@des ~]# ls -a
###显示隐藏文件

在这里插入图片描述

[root@des ~]# history
[root@des ~]# history -c
[root@des ~]# history
###历史记录被清除

在这里插入图片描述

[root@des ~]# exit
[kiosk@foundation1 ~]$ ssh root@172.25.254.101
[root@des ~]# history
###历史文件被恢复,这是因为历史记录被缓存在历史文件中

在这里插入图片描述

[root@des ~]# > .bash_history
###清空历史文件
[root@des ~]# exit
[kiosk@foundation1 ~]$ ssh root@172.25.254.101
[root@des ~]# history
###清空之前的历史记录被清除

在这里插入图片描述

退出界面修改

[root@des ~]# vim .bash_logout
# ~/.bash_logout
clear
###在退出时清屏
[root@des ~]# exit
###清屏
[kiosk@foundation1 ~]$ ssh root@172.25.254.101

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@des ~]# vim .bash_logout
# ~/.bash_logout
clear
echo bye
[root@des ~]# exit
###清屏且在最上方由bye显示
[kiosk@foundation1 ~]$ ssh root@172.25.254.101

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值