且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相当于Bash中的__FILE__和__LINE__

更新时间:2022-06-13 02:35:54

#!/bin/bash

echo $LINENO
echo `basename $0`

$LINENO为当前行号 当前文件的$0.我使用basename来确保仅获取文件名而不是路径.

$LINENO for the current line number $0 for the current file. I used basename to ensure you only get the file name and not the path.

更新:

#!/bin/bash

MY_NAME=`basename $0`

function ouch {
   echo "Fail @ [${MY_NAME}:${1}]"
   exit 1
}

ouch $LINENO

如果使用函数方法,则必须将该行作为参数传递,否则将获得函数定义的行.

You have to pass the line as a parameter if you use the function approach else you will get the line of the function definition.