Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix shell编程-解释$的使用?带函数调用_Unix_Shell - Fatal编程技术网

Unix shell编程-解释$的使用?带函数调用

Unix shell编程-解释$的使用?带函数调用,unix,shell,Unix,Shell,我试图理解一个shell/bash脚本,只是想了解如何在代码中使用$?。 它与函数调用一起使用 功能示例: function showerr { err=$1 if [ $err -ne 0 ]; then echo `date` : "error!" echo "stat : " $2 echo `date` : "stat: " $2 # alert email prog=$0 uname=`whoam

我试图理解一个shell/bash脚本,只是想了解如何在代码中使用
$?
。 它与函数调用一起使用

功能示例:

function showerr {   err=$1
   if [ $err -ne 0 ]; then
      echo `date` : "error!"
      echo "stat  :  " $2
      echo `date` : "stat:  " $2
      # alert email 
      prog=$0
      uname=`whoami`
      echo `date` : Sending email to ${ADDR_TO} 
      mailx -s "Error checking status " $ADDR_TO << EOF
+++++++++++++++++++++
stat   =   $2
util   =   $prog
host   =   $uname
+++++++++++++++++++++
Check $uname for details.
.
EOF
      echo "Exiting program..."
      exit 1
   fi
}

$?
是最后一个命令的退出代码。有关POSIX shell中此类特殊变量的列表,请参阅

如果第一个参数不是
0
,则
showerr
功能会记录错误

因此:

只有当
/some\u super\u script\u可能失败时才会记录某些内容
的退出代码不是0(这通常意味着它失败)

将始终记录

showerr 0 "message"
我什么都不会做

./some_super_script_that_might_fail
showerr $? "SuperScript failed"
showerr 1 "message"
showerr 0 "message"