#####shell的基础知识########

###shell###
1.什么是shell?

  • Linux系统中的shell是操作系统的外壳(用于区别内核),也是操作系统的一个软件,“指为使用者提供操作界面”的软件(命令解释器),为用户提供使用操作系统之间的接口(用户需求或命令通过shell甄别后将输入的命令给内核通信,让内核控制调动相应的程序对需求进行响应)。
  • 它是命令语言、命令解释程序和程序设计语言的通称;
  • shell是解释型语言(解释型语言还有python\perl),它拥有自己内建的shell命令集也能被系统中其他应用程序所调用。作为命令语言,他交互式解释和执行用户输入的命令或自动的解释和执行用户预先设定好的一串命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。
  • 文字操作系统与外部最主要的接口叫shell.shell是操作系统最外面的一层,管理用户与操作系统之间的交互;
    2.什么是shell脚本?
    脚本是一种解释型语言,shell脚本当命令或者程序不再用命令行执行,而是通过过可执行文件来实现命令的执行,那么可执行文件被称为shell脚本。
    shell读取存放在文件中的命令并且执行它们,读到文件结尾终止后不进行交互,这种通过文件执行命令的方式叫非互交模式。
    shell脚本优点:
    用shell脚本保存执行动作
    用脚本判定命令的执行条件
    用脚本实现动作的批量执行
    3、系统默认的shell
    bash=GNU Bourne-Again Shell
    echo $SHELL $显示系统正在使用的shell
    直接在命令行中执行 echo $0,打印的是当前的shell名
[root@localhost ~]# echo $SHELL
/bin/bash   #系统正在使用的shell
[root@localhost ~]# echo $0
-bash    #当前的shell名

###shell脚本的编写规范####

  • 脚本文件名称 :一般以.sh结尾,表明文件类型
  • 第一行:#!/bin/bash 脚本使用的解释器,通常用幻数“#!”指定
  • 有用户判断,否则任何用户都可以执行这个脚本
  • 有流程控制,否则只是简单的命令进行顺序操作没有成功与否的判断
  • 注释:可以在命令后,也可以自成一行
  • 编写脚本的基本信息(作者、联系方式、版本、时间、描述…)

注:脚本的格式不是自动生成的,为了方便标准化脚本格式可以编辑vim的配置文件/etc/vimrc
方法一:
1.设定快捷键: map (映射) ##在文件的末尾定义快捷键的调用,打开新建的文件后按设定的快捷键即可生成

"map <F7> ms:call WEST0S()<cr>'s   ##快捷键 + 调用的函数
function WESTOS()     ##自己命名的函数
        call append(0,"#######################################")
        call append(1,"#Author:   Tom                           #")  #作者
        call append(2,"#Create_Date:  ".strftime("%Y-%m-%d  %H:%M:%S")."   #")   #建立时间
        call append(3,"#Version:  7.3                          #") #版本
        call append(4,"#Mail:   tom@westos.com                             #")  #联系方式
        call append(5,"#Description:                         #")  #描述u
        call append(6,"#                                     #")
        call append(7,"#                                     #")
        call append(8,"#######################################")
        call append(9,"")  #空格
        call append(10,"#!/bin/bash")  ##脚本使用的解释器
endfunction

2.文件属性方式自动执行:
自动根据所编写的文件类型生成

`autocmd BufNewFile *.sh exec “:call WESTOS()” ##自动建立以.sh结尾的新创建文件文件

function WESTOS()
call append(0,"#######################################")
call append(1,"#Author: #")
call append(2,"#Create_Date: “.strftime(”%Y-%m-%d %H:%M:%S")." #")
call append(3,"#Version: #")
call append(4,"#Mail: #")
call append(5,"#Description: #")
call append(6,"# #")
call append(7,"# #")
call append(8,"#######################################")
call append(9,"")
call append(10,"#!/bin/bash")
endfunction
`在这里插入图片描述
####脚本的执行方式 #####
sh script.sh
source scripy.sh
. script.sh
./scripy.sh ##该方法执行脚本必须有脚本的执行权限
编写脚本看四种执行方式产生的进程

[root@localhost mnt]# vim data.sh
[root@localhost mnt]# cat data.sh 
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 03:33:32 #
#Description:                      #
#                                  #
####################################

#!/bin/bash
watch   -n 1 data

1)sh data.sh
bash data.sh
这两种执行shell脚本的方式时完全等效的,规定的脚本执行环境,系统调用的资源和产生的进程一致

[root@localhost ~]# sh /mnt/data.sh   ##Ctrl +z  打入后台

[root@localhost ~]# ps f   ##查看进程
  PID TTY      STAT   TIME COMMAND
 6613 pts/1    Ss     0:00 -bash
 6679 pts/1    T      0:00  \_ sh /mnt/data.sh   #sh 脚本年运行产生的进程
 6680 pts/1    T      0:00  |   \_ watch -n 1 data
 6689 pts/1    R+     0:00  \_ ps f
 2117 pts/0    Ss+    0:00 /bin/bash
  589 tty1     Ss+    0:02 /usr/bin/Xorg :0 -background none -verbose -auth /run/gdm/auth-for-gdm-BR79gb
 6645 pts/1    S      0:00 dbus-launch --autolaunch=946cb0e817ea4adb916183df8c4fc817 --binary-syntax --c
 1277 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600

[root@localhost ~]# bash /mnt/data.sh    #打入后台

[root@localhost ~]# ps f
  PID TTY      STAT   TIME COMMAND
 6613 pts/1    Ss     0:00 -bash
 6679 pts/1    T      0:00  \_ sh /mnt/data.sh
 6680 pts/1    T      0:00  |   \_ watch -n 1 data
 6705 pts/1    T      0:00  \_ bash /mnt/data.sh   #bash 产生的进程
 6706 pts/1    T      0:00  |   \_ watch -n 1 data
 6711 pts/1    R+     0:00  \_ ps f
 2117 pts/0    Ss+    0:00 /bin/bash
  589 tty1     Ss+    0:02 /usr/bin/Xorg :0 -background none -verbose -auth /run/gdm/auth-for-gdm-BR79gb
 6645 pts/1    S      0:00 dbus-launch --autolaunch=946cb0e817ea4adb916183df8c4fc817 --binary-syntax --c
 1277 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600

2)source data.sh

[root@localhost ~]# source /mnt/data.sh    #打入后台

[4]+  Stopped                 watch -n 1 data    ##说明source 运行脚本未产生新的进程,用的当前的进程
[root@localhost ~]# ps f
  PID TTY      STAT   TIME COMMAND
 6613 pts/1    Ss     0:00 -bash
 6679 pts/1    T      0:00  \_ sh /mnt/data.sh
 6680 pts/1    T      0:00  |   \_ watch -n 1 data
 6705 pts/1    T      0:00  \_ bash /mnt/data.sh
 6706 pts/1    T      0:00  |   \_ watch -n 1 data
 6748 pts/1    T      0:00  \_ watch -n 1 data
 6757 pts/1    T      0:00  \_ watch -n 1 data   ##source 运行脚本产生的进程
 6762 pts/1    R+     0:00  \_ ps f
 2117 pts/0    Ss+    0:00 /bin/bash
  589 tty1     Ss+    0:02 /usr/bin/Xorg :0 -background none -verbose -auth /run/gdm/auth-for-gdm-BR79gb
 6645 pts/1    S      0:00 dbus-launch --autolaunch=946cb0e817ea4adb916183df8c4fc817 --binary-syntax --c
 1277 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600

3)./data.sh 需要文件有执行权限
使用文件名称的方式指定调用脚本,有相对路径和绝对路径两种方式;这两种执行shell脚本的方式时完全等效的,规定的脚本执行环境,系统调用的资源和产生的进程一致

[root@localhost mnt]# chmod +x data.sh
[root@localhost mnt]# ./data.sh    ##相对路径


[root@localhost mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 6839 pts/1    Ss     0:00 -bash
 6884 pts/1    T      0:00  \_ -bash
 6885 pts/1    T      0:00  |   \_ watch -n 1 data
 6890 pts/1    R+     0:00  \_ ps f
 2117 pts/0    Ss+    0:00 /bin/bash
  589 tty1     Ss+    0:02 /usr/bin/Xorg :0 -background none -verbose -auth /run/gdm/auth-for-gdm-BR79gb
 6871 pts/1    S      0:00 dbus-launch --autolaunch=946cb0e817ea4adb916183df8c4fc817 --binary-syntax --c
 1277 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600
[root@localhost mnt]# /mnt/data.sh    #绝对路径


[root@localhost mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 6839 pts/1    Ss     0:00 -bash
 6884 pts/1    T      0:00  \_ -bash
 6885 pts/1    T      0:00  |   \_ watch -n 1 data
 6902 pts/1    T      0:00  \_ -bash
 6903 pts/1    T      0:00  |   \_ watch -n 1 data
 6908 pts/1    R+     0:00  \_ ps f
 2117 pts/0    Ss+    0:00 /bin/bash
  589 tty1     Ss+    0:02 /usr/bin/Xorg :0 -background none -verbose -auth /run/gdm/auth-for-gdm-BR79gb
 6871 pts/1    S      0:00 dbus-launch --autolaunch=946cb0e817ea4adb916183df8c4fc817 --binary-syntax --c
 1277 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud ttyS0 115200 38400 9600

###shell脚本的调试####
sh -x scripts ##适用于所有的shell脚本
vim scripts.sh shell脚本必须有执行权限
#!/bin/bash -x

[root@localhost ~]# cat data.sh    ##编辑一个脚本
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 03:59:16 #
#Description:                      #
#                                  #
####################################

#!/bin/sh
date
cal
cat

运行时脚本会卡住
在这里插入图片描述

[root@localhost ~]# sh -x data.sh 
+ date   ## +表命令
Wed Aug 21 04:09:16 EDT 2019  ##输出
+ cal
     August 2019    
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

+ cat    ##cat命令后没有输出

###脚本示例###
执行ip_show.sh 显示当前主机的ip地址


[root@localhost mnt]# cat ip_show.sh
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 04:17:05 #
#Description:                      #
#                                  #
####################################

#!/bin/bash
ifconfig eth0 | cut -c 13-28 | head -n 2 | tail -n 1
[root@localhost mnt]# sh ip_show.sh
 172.25.254.123 

执行user_show.sh 显示当前主机中能登陆系统的Ip


[root@localhost mnt]# cat user_show.sh
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 04:19:26 #
#Description:                      #
#                                  #
####################################

#!/bin/bash
awk -F : '/bash$/{print $1}'  /etc/passwd
[root@localhost mnt]# sh user_show.sh
root
student
user

执行host_message.sh 显示当前主机的名称,ip以及能登陆系统的用户


[root@localhost mnt]# cat  host_message.sh
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 04:23:05 #
#Description:                      #
#                                  #
####################################

#!/bin/bash
echo "hostname:`hostname`"
echo "host ipaddress: 
              `ifconfig |awk '/inet\>/&&!/127.0.0.1/{print $2}'`"
echo "Login user list:
`awk -F : '/bash$/||/sh$/{print $1}' /etc/passwd| sed 's/^/		/g'`"   ##两个tab距离
[root@localhost mnt]# sh host_message.sh
hostname:localhost
host ipaddress: 
              172.25.254.123
Login user list:
		root
		student
		user

clear_log.sh 执行命令后可以清空日志


[root@localhost mnt]# cat clear_log.sh 
####################################
#Author: Linux                     #
#Version: 7.3                      #
#Mail: Linux@toto.com              #
#Create_Date:  2019-08-21 04:34:08 #
#Description:                      #
#                                  #
####################################

#!/bin/bash
> /var/log/messages
[root@localhost mnt]# head -3 /var/log/messages 
Jul 10 18:18:05 localhost rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="447" x-info="http://www.rsyslog.com"] start
Jul 10 18:18:05 localhost rsyslogd-2307: warning: ~ action is deprecated, consider using the 'stop' statement instead [try http://www.rsyslog.com/e/2307 ]
Jul 10 18:17:49 localhost journal: Runtime journal is using 8.0M (max 92.0M, leaving 138.0M of free 912.5M, current limit 92.0M).

[root@localhost mnt]# sh clear_log.sh 
[root@localhost mnt]# cat /var/log/messages 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值