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