Shell 脚本专家指南 学习笔记1


Shell脚本的错误检测

set - 打开某个选项,例如set -x

set + 关闭某个选项,例如set +x

这样就能精确控制调试某一段的代码,在不需要调试的地方关闭选项。

1、如果脚本文件中加入了命令set -x ,那么在set命令之后执行的每一条命令以及加在命令行中的任何参数(包括变量和变量的值)都会显示出来。每一行之前都会加上加号(+),提示它是跟踪输出的标识。在子Shell中执行的Shell跟踪命令会加两个加号,即“++”。

2、set -v 选项打开冗余模式,能打印出调试语句。

3、根据调试层次控制输出,用于多层次调试:在脚本开始部分设置一个调试变量。脚本运行时测试该变量,然后根据变量的值决定是显示还是禁止调试指令。

例如:

#!/bin/bash

debug=3
test $debug -gt 1 && echo "Debug is bigger than 1."
test $debug -gt 2 && echo "Debug is bigger than 2."
……

/etc/shadow中用户账号信息的第三个字段保存的是该账户的口令最后一次更改的时间,记录形式从1970年1月1日到该时间的天数。UNIX和Linux都是从1970年1月1日开始计算时间的。

[root@localhost ~]# tail -3 /etc/shadow
tomcat:!!:15261::::::
tom:$6$y6Glr2jNfeFeokEP$Fiov9oRVKKOT9tHhL4ZrgS6WMarI4QuFBWyEOlivFbOPxezJN5PltO5xTlhz4Qxa3B0cuUHghDVJey5cXUv590:15261:0:99999:7:::
jack:$6$WngWxvov$EgAcspKEwnllJ1mpCiCjt.O5wgewhdEFP6Prkx2g.Z6z9LusXyZeFUZe5V3kzzWvhvIH9Bg5K6NGZEicQfHiw1:15287:0:99999:7:::
date +%s 直接返回纪元(1970年1月1日)以来的秒数。
[root@localhost ~]# date +%s
1321600930

[root@localhost ~]# help set
set: set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
    Set or unset values of shell options and positional parameters.


    Change the value of shell attributes and positional parameters, or
    display the names and values of shell variables.


    Options:
      -a  Mark variables which are modified or created for export.
      -b  Notify of job termination immediately.
      -e  Exit immediately if a command exits with a non-zero status.
      -f  Disable file name generation (globbing).
      -h  Remember the location of commands as they are looked up.
      -k  All assignment arguments are placed in the environment for a
          command, not just those that precede the command name.
      -m  Job control is enabled.
      -n  Read commands but do not execute them.
      -o option-name
          Set the variable corresponding to option-name:
              allexport    same as -a
              braceexpand  same as -B
              emacs        use an emacs-style line editing interface
              errexit      same as -e
              errtrace     same as -E
              functrace    same as -T
              hashall      same as -h
              histexpand   same as -H
              history      enable command history
              ignoreeof    the shell will not exit upon reading EOF
              interactive-comments
                           allow comments to appear in interactive commands
              keyword      same as -k
              monitor      same as -m
              noclobber    same as -C
              noexec       same as -n
              noglob       same as -f
              nolog        currently accepted but ignored
              notify       same as -b
              nounset      same as -u
              onecmd       same as -t
              physical     same as -P
              pipefail     the return value of a pipeline is the status of
                           the last command to exit with a non-zero status,
                           or zero if no command exited with a non-zero status
              posix        change the behavior of bash where the default
                           operation differs from the Posix standard to
                           match the standard
              privileged   same as -p
              verbose      same as -v
              vi           use a vi-style line editing interface
              xtrace       same as -x
      -p  Turned on whenever the real and effective user ids do not match.
          Disables processing of the $ENV file and importing of shell
          functions.  Turning this option off causes the effective uid and
          gid to be set to the real uid and gid.
      -t  Exit after reading and executing one command.
      -u  Treat unset variables as an error when substituting.
      -v  Print shell input lines as they are read.
      -x  Print commands and their arguments as they are executed.
      -B  the shell will perform brace expansion
      -C  If set, disallow existing regular files to be overwritten
          by redirection of output.
      -E  If set, the ERR trap is inherited by shell functions.
      -H  Enable ! style history substitution.  This flag is on
          by default when the shell is interactive.
      -P  If set, do not follow symbolic links when executing commands
          such as cd which change the current directory.
      -T  If set, the DEBUG trap is inherited by shell functions.
      -   Assign any remaining arguments to the positional parameters.
          The -x and -v options are turned off.

-a  &&

-o  ||

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值