zoukankan      html  css  js  c++  java
  • 四、Shell脚本

    什么是shell脚本

    我们把原来linux命令或语句放在一个文件中,然后通过这个程序文件去执行时,就被称为shell脚本;

    举例

    vim test.sh

     内容如下

    #!/bin/bash
    #This is shell.
    ehco "hello world"
    mkdir /tmp/test
    touch /tmp/test/a.txt

     添加执行权限

    chmod +x test.sh

    脚本执行顺序

    1、先查找系统环境变量ENV(该变量指定了环境变量文件,加载顺序默认为/etc/profile,~/.bash_profile、~/bashrc、/etc/bashrc)

    2、执行Shell脚本(从上至下,从左至右)

    执行脚本时父Shell(也就是当前Shell)会向系统内核请求启动一个新的进程,在该进程中执行脚本命令。

     shell脚本的执行通常有以下几种方式

    1、/root/test.sh(绝对路径)或者 ./test.sh(需要添加执行权限chmod +x test.sh)

    2、bash test.sh 或 sh test.sh  (不需要添加执行权限,会开启新的进程执行脚本,推荐使用)

    3、source test.sh 或 . test.sh(不需要添加执行权限,在当前父shell进程中运行,可以将自身脚本中的变量和函数返回到父shell中使用)

    4、sh < test.sh 或者 cat test.sh |sh(bash)

    例3:

    这种方式会在父Shell进程中运行,可将shell.sh自身脚本中的变量值或函数的返回值返回到父Shell中使用

    [root@tz yum.repos.d]# source shell.sh
    [root@tz yum.repos.d]# . shell.sh
    

    举例

    使用sh命令执行脚本,输出变量userdir为空

    [root@tz yum.repos.d]# echo 'userdir=`pwd`' >pwd.sh
    [root@tz yum.repos.d]# sh pwd.sh
    [root@tz yum.repos.d]# echo $userdir
    
    

    使用source命令执行脚本,输出变量userdir不为空,说明值存在当前父shell中

    [root@tz yum.repos.d]# source pwd.sh
    [root@tz yum.repos.d]# echo $userdir
    /etc/yum.repos.d
    

    如果脚本中有引用或执行其他脚本内容或配置文件时,最好使用.或source执行脚本文件

    命令的替换

    Shell脚本可以从命令输出中提取信息并将其赋给变量。

    可通过一下两种方式

    • 反引号`
    • $()格式

    在命令中调用date命令输出值

    命令替换:将命令的执行结果作为echo的输出值

    [root@tzPC ~]# echo `date`
    Wed Jul 15 10:14:21 CST 2020
    [root@tzPC ~]# echo $(date)
    Wed Jul 15 10:23:32 CST 2020
    [root@tzPC ~]# echo `date +"%Y-%m-%d"`
    2020-07-15

    常见案例

    [root@tzPC Script]# cat test5
    #/bin/bash
    today=$(date +%y%m%d)
    ls -al /usr/bin >log.$today

    注意命令替换会创建一个子Shell来运行对应的命令

    [root@tzPC Script]# echo $BASH_SUBSHELL
    0
    [root@tzPC Script]# echo `echo $BASH_SUBSHELL`
    1

    $BASH_SUBSHELL参数输出为1表示有子shell

    [root@tzPC Script]# `echo $BASH_SUBSHELL >subshell`
    [root@tzPC Script]# cat subshell 
    1

    这里有个疑问,父Shell创建普通变量test值为1,命令替换开启子Shell是如何读到父Shell的普通变量test的?

    [root@tzPC Script]# test=1
    [root@tzPC Script]# `echo $test > test`
    [root@tzPC Script]# cat test
    1

    命令的嵌套使用

    [root@tzPC ~]# var6=$(tar zcvf root.tar.gz $(find /root/ -name *.cfg))
    tar: Removing leading `/' from member names
    [root@tzPC ~]# ls
    2018-05-12_21:10:44  anaconda-ks.cfg  root.tar.gz

     当本机时间不准确,如晚于文件的mtime时间,解压文件会发生如下报错,解压是成功了的

    [root@tzPC ~]# date -s "2012-03-03 21:25:00"
    Sat Mar  3 21:25:00 CST 2012
    [root@tzPC ~]# tar zxvf root.tar.gz -C /home/tz/
    root/anaconda-ks.cfg
    tar: root/anaconda-ks.cfg: time stamp 2020-02-04 13:00:19 is 250011303.162389384 s in the future

     输出重定向

    将命令的输出发送到一个文件。

    [root@tzPC Script]# who >test1
    [root@tzPC Script]# cat test1
    root     tty1         2020-08-10 16:20

    >>为追加

    输入重定向

    将文件的内容重定向到命令。

    [root@tzPC Script]# wc < test1
      5  24 259

    wc命令会对数据中的文本计数,默认输出3个值

    行数、词数、字节数

    内联输入重定向

    必须指定一个文本标记来划分输入数据的开始和结尾

    [root@tzPC Script]# cat << EOF
    > 123
    > 321
    > EOF
    123
    321

    管道

    将一个命令的输出作为另一个命令的输入

    [root@tzPC Script]# rpm -qa | sort |more
    abrt-2.1.11-55.el7.centos.x86_64
    abrt-addon-ccpp-2.1.11-55.el7.centos.x86_64
    abrt-addon-kerneloops-2.1.11-55.el7.centos.x86_64
    abrt-addon-pstoreoops-2.1.11-55.el7.centos.x86_64
    abrt-addon-python-2.1.11-55.el7.centos.x86_64
    abrt-addon-vmcore-2.1.11-55.el7.centos.x86_64
    ...
    今天的学习是为了以后的工作更加的轻松!
  • 相关阅读:
    计算在线人数
    微软MSMQ消息件研究(一)
    jQuery循序渐进2
    单点登陆的ASP.NET应用程序设计[zt]
    利用SQL2005的缓存依赖
    .Net 操作MSMQ
    GridView中数据格式化
    TcpListener/TcpClient/UdpClient 的区别及联系
    微软消息件MSMQ研究DEMO(二)
    Nhibernate(1)
  • 原文地址:https://www.cnblogs.com/tz90/p/13441407.html
Copyright © 2011-2022 走看看