shell warn 警告输出格式

shell warn 警告输出格式

1. die

在linux shell中执行命令后加上die命令,执行过程中如果出错会报出相应的原因与行号。如cat /usr/home/test.log || die $?,如果文件不存在,则会报出相应的错误。

  • test1.sh
#!/bin/bash

warn () {
    
    
  echo "$@" >&2
}


die () {
    
    
  status="$1"
  shift
  warn "$@"
  exit "$status"
}

cat /usr/home/test.log || die $?
[root@dockermake ~]# bash test1.sh 
cat: /usr/home/test.log: No such file or directory

猜你喜欢

转载自blog.csdn.net/xixihahalelehehe/article/details/125325240