相当于 Bash 中的 __FILE__ 和 __LINE__

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3055755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:13:22  来源:igfitidea点击:

Equivalent of __FILE__ and __LINE__ in Bash

basherror-handlingenvironment-variables

提问by user368507

Is there any variable in bash that contains the name of the .sh file executed? The line number would be great too.

bash 中是否有任何变量包含执行的 .sh 文件的名称?行号也会很棒。

I want to use it in error messages such as:

我想在错误消息中使用它,例如:

echo "ERROR: [$FILE:L$LINE] $somefile not found"

采纳答案by ezpz

#!/bin/bash

echo $LINENO
echo `basename 
#!/bin/bash

MY_NAME=`basename 
$ cat xx
#!/bin/bash

_ERR_HDR_FMT="%.23s %s[%s]: "
_ERR_MSG_FMT="${_ERR_HDR_FMT}%s\n"

error_msg() {
  printf "$_ERR_MSG_FMT" $(date +%F.%T.%N) ${BASH_SOURCE[1]##*/} ${BASH_LINENO[0]} "${@}"
}

error_msg "here"


error_msg "and here"
` function ouch { echo "Fail @ [${MY_NAME}:]" exit 1 } ouch $LINENO
`

$LINENOfor the current line number $0for the current file. I used basenameto ensure you only get the file name and not the path.

$LINENO$0当前文件的当前行号 。我曾经basename确保你只得到文件名而不是路径。

UPDATE:

更新:

2010-06-16.15:33:13.069 xx[11]: here
2010-06-16.15:33:13.073 xx[14]: and here

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.

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

回答by Kevin Little

I find the "BASH_SOURCE" and "BASH_LINENO" built-in arrays very useful:

我发现“BASH_SOURCE”和“BASH_LINENO”内置数组非常有用:

echo $LINENO
echo $(basename 
#!/bin/bash
debug() {
  echo "${BASH_SOURCE[1]##*/}:${FUNCNAME[1]}[${BASH_LINENO[0]}]" > /dev/tty
}
debug
)

Invoking xx yields

调用 xx 收益

script:main[5]    

回答by Loxley

You just need to

你只需要

# Say the file, line number and optional message for debugging
# Inspired by bash's `caller` builtin
# Thanks to https://unix.stackexchange.com/a/453153/143394
function yelp () {
  # shellcheck disable=SC2154  # undeclared zsh variables in bash
  if [[ $BASH_VERSION ]]; then
    local file=${BASH_SOURCE[1]##*/} func=${FUNCNAME[1]} line=${BASH_LINENO[0]}
  else  # zsh
    emulate -L zsh  # because we may be sourced by zsh `emulate bash -c`
    # $funcfiletrace has format:  file:line
    local file=${funcfiletrace[1]%:*} line=${funcfiletrace[1]##*:}
    local func=${funcstack[2]}
    [[ $func =~ / ]] && func=source  # $func may be filename. Use bash behaviour
  fi
  echo "${file##*/}:$func:$line $*" > /dev/tty
}

回答by Tom Hale

Here's how to do it in a reusable function. if the following is in a file named script:

以下是如何在可重用函数中执行此操作。如果以下内容在名为 的文件中script

##代码##

This produces the output:

这会产生输出:

##代码##

Which indicates the line on which debugwas called.

这表示debug被调用的行。



The following will print out the filename, function, line and an optional message.

以下将打印出文件名、函数、行和可选消息。

Also works in zshfor extra goodness.

也适用于zsh额外的好处。

##代码##