如何确定脚本本身中的Bash脚本文件的名称?
就像如果我的脚本在文件runme.sh中,那么我如何让它显示“您正在运行runme.sh”消息而不硬编码?
如何确定脚本本身中的Bash脚本文件的名称?
就像如果我的脚本在文件runme.sh中,那么我如何让它显示“您正在运行runme.sh”消息而不硬编码?
me=`basename "$0"`
对于读取symlink1,这通常不是你想要的(你通常不想以这种方式迷惑用户),尝试:
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
在我看来,这会产生令人困惑的输出。“我跑了foo.sh,但它说我跑了bar.sh!?”一定是虫子!”此外,使用不同名称的符号链接的目的之一是根据其名称提供不同的功能(例如某些平台上的gzip和gunzip)。
1也就是说,为了解析符号链接,当用户执行foo.sh时,它实际上是一个到bar.sh的符号链接,你希望使用解析的名称bar.sh而不是foo.sh。
如果脚本名中有空格,更可靠的方法是使用"$0"或"$(basename "$0")"-或MacOS: "$(basename \"$0\")"。这可以防止名称以任何方式被破坏或解释。一般来说,在shell中始终使用双引号作为变量名是一种良好的实践。
为了回答Chris Conway的问题,在Linux上(至少)你会这样做:
echo $(basename $(readlink -nf $0))
Readlink输出符号链接的值。如果不是符号链接,则打印文件名。-n告诉它不打印换行符。-f告诉它完全跟随链接(如果一个符号链接是到另一个链接的链接,它也会解析那个链接)。
这些答案对于它们所陈述的情况是正确的,但如果您使用'source'关键字从另一个脚本运行脚本(以便它在同一个shell中运行),仍然存在一个问题。在本例中,您将获得调用脚本的$0。在这种情况下,我认为不可能获得脚本本身的名称。
这是一个边缘情况,不应该太当真。如果你直接从另一个脚本运行脚本(没有'source'),使用$0可以工作。
使用bash >= 3,以下工作:
$ ./s
0 is: ./s
BASH_SOURCE is: ./s
$ . ./s
0 is: bash
BASH_SOURCE is: ./s
$ cat s
#!/bin/bash
printf '$0 is: %s\n$BASH_SOURCE is: %s\n' "$0" "$BASH_SOURCE"
# ------------- SCRIPT ------------- #
#!/bin/bash
echo
echo "# arguments called with ----> ${@} "
echo "# \$1 ----------------------> $1 "
echo "# \$2 ----------------------> $2 "
echo "# path to me ---------------> ${0} "
echo "# parent path --------------> ${0%/*} "
echo "# my name ------------------> ${0##*/} "
echo
exit
# ------------- CALLED ------------- #
# Notice on the next line, the first argument is called within double,
# and single quotes, since it contains two words
$ /misc/shell_scripts/check_root/show_parms.sh "'hello there'" "'william'"
# ------------- RESULTS ------------- #
# arguments called with ---> 'hello there' 'william'
# $1 ----------------------> 'hello there'
# $2 ----------------------> 'william'
# path to me --------------> /misc/shell_scripts/check_root/show_parms.sh
# parent path -------------> /misc/shell_scripts/check_root
# my name -----------------> show_parms.sh
# ------------- END ------------- #
回复:上面Tanktalus(接受)的答案,一个稍微干净一点的方法是使用:
me=$(readlink --canonicalize --no-newline $0)
如果你的脚本来自另一个bash脚本,你可以使用:
me=$(readlink --canonicalize --no-newline $BASH_SOURCE)
我同意,如果您的目标是向用户提供反馈,那么取消对符号链接的引用可能会令人困惑,但在某些情况下,您确实需要获得脚本或其他文件的规范名称,在我看来,这是最好的方法。
DIRECTORY=$(cd `dirname $0` && pwd)
我从另一个堆栈溢出问题得到了上面,Bash脚本可以告诉它存储在什么目录中吗?,但我认为它对这个话题也很有用。
this="$(dirname "$(realpath "$BASH_SOURCE")")"
这将解析符号链接(realpath会这样做),处理空格(双引号会这样做),并将找到当前脚本名称,即使是在来源(. ./myscript)或被其他脚本调用时($BASH_SOURCE会这样做)。在所有这些之后,最好将其保存在环境变量中以便重用或方便复制到其他地方(this=)…
我发现这一行总是有效的,无论文件是源文件还是作为脚本运行。
echo "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
如果你想遵循符号链接,在上面得到的路径上使用readlink,递归或非递归。
单行程序工作的原因可以通过使用BASH_SOURCE环境变量及其关联的FUNCNAME来解释。
BASH_SOURCE An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}. FUNCNAME An array variable containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function. The bottom-most element (the one with the highest index) is "main". This variable exists only when a shell function is executing. Assignments to FUNCNAME have no effect and return an error status. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset. This variable can be used with BASH_LINENO and BASH_SOURCE. Each element of FUNCNAME has corresponding elements in BASH_LINENO and BASH_SOURCE to describe the call stack. For instance, ${FUNCNAME[$i]} was called from the file ${BASH_SOURCE[$i+1]} at line number ${BASH_LINENO[$i]}. The caller builtin displays the current call stack using this information.
[来源:Bash手册]
感谢Bill Hernandez提供的信息。我增加了一些我正在采用的偏好。
#!/bin/bash
function Usage(){
echo " Usage: show_parameters [ arg1 ][ arg2 ]"
}
[[ ${#2} -eq 0 ]] && Usage || {
echo
echo "# arguments called with ----> ${@} "
echo "# \$1 -----------------------> $1 "
echo "# \$2 -----------------------> $2 "
echo "# path to me ---------------> ${0} " | sed "s/$USER/\$USER/g"
echo "# parent path --------------> ${0%/*} " | sed "s/$USER/\$USER/g"
echo "# my name ------------------> ${0##*/} "
echo
}
干杯
像这样的东西?
export LC_ALL=en_US.UTF-8
#!/bin/bash
#!/bin/sh
#----------------------------------------------------------------------
start_trash(){
ver="htrash.sh v0.0.4"
$TRASH_DIR # url to trash $MY_USER
$TRASH_SIZE # Show Trash Folder Size
echo "Would you like to empty Trash [y/n]?"
read ans
if [ $ans = y -o $ans = Y -o $ans = yes -o $ans = Yes -o $ans = YES ]
then
echo "'yes'"
cd $TRASH_DIR && $EMPTY_TRASH
fi
if [ $ans = n -o $ans = N -o $ans = no -o $ans = No -o $ans = NO ]
then
echo "'no'"
fi
return $TRUE
}
#-----------------------------------------------------------------------
start_help(){
echo "HELP COMMANDS-----------------------------"
echo "htest www open a homepage "
echo "htest trash empty trash "
return $TRUE
} #end Help
#-----------------------------------------------#
homepage=""
return $TRUE
} #end cpdebtemp
# -Case start
# if no command line arg given
# set val to Unknown
if [ -z $1 ]
then
val="*** Unknown ***"
elif [ -n $1 ]
then
# otherwise make first arg as val
val=$1
fi
# use case statement to make decision for rental
case $val in
"trash") start_trash ;;
"help") start_help ;;
"www") firefox $homepage ;;
*) echo "Sorry, I can not get a $val for you!";;
esac
# Case stop
如果你调用shell脚本喜欢
/home/mike/runme.sh
$0是全名
/home/mike/runme.sh
Basename $0将获得基本文件名
runme.sh
你需要把这个基本的名字放到一个变量里,比如
filename=$(basename $0)
并添加额外的文本
echo "You are running $filename"
你的脚本就像
/home/mike/runme.sh
#!/bin/bash
filename=$(basename $0)
echo "You are running $filename"
echo "$(basename "`test -L ${BASH_SOURCE[0]} \
&& readlink ${BASH_SOURCE[0]} \
|| echo ${BASH_SOURCE[0]}`")"
在bash中,您可以使用$0获取脚本文件名。通常$1,$2等用于访问CLI参数。类似地,$0是访问触发脚本的名称(脚本文件名)。
#!/bin/bash
echo "You are running $0"
...
...
如果你使用/path/to/script.sh这样的路径调用脚本,那么$0也会给出带有路径的文件名。在这种情况下,需要使用$(basename $0)来获取脚本文件名。
由于一些注释询问没有扩展名的文件名,这里有一个示例如何完成:
FileName=${0##*/}
FileNameWithoutExtension=${FileName%.*}
享受吧!
以下是我从Dimitre Radoulov的回答中得到的灵感(顺便说一下,我给他的回答投了赞)。
script="$BASH_SOURCE"
[ -z "$BASH_SOURCE" ] && script="$0"
echo "Called $script with $# argument(s)"
不管你如何调用你的脚本
. path/to/script.sh
or
./path/to/script.sh
简短,清晰,简单,在my_script.sh中
#!/bin/bash
running_file_name=$(basename "$0")
echo "You are running '$running_file_name' file."
把:
./my_script.sh
You are running 'my_script.sh' file.
这对./self.sh, ~/self.sh, source self.sh, source ~/self.sh很有效:
#!/usr/bin/env bash
self=$(readlink -f "${BASH_SOURCE[0]}")
basename=$(basename "$self")
echo "$self"
echo "$basename"
演职员:我综合了多个答案才得到这个答案。
$0将给出您正在运行的脚本的名称。创建一个脚本文件并添加以下代码
#!/bin/bash
echo "Name of the file is $0"
然后像这样从终端运行
./file_name.sh
在所有情况下获取脚本或源脚本的“真实路径”:
fullname=$(readlink $0) # Take care of symbolic links
dirname=${fullname%/*} # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}
下面是要生成的bash脚本(在新创建的“workdir”子目录中,在当前目录的“mytest”中),该bash脚本将派生另一个脚本,该脚本将调用bash定义的函数....测试了许多方法来启动它们:
#!/bin/bash
##############################################################
ret=0
fullname=$(readlink $0) # Take care of symbolic links
dirname=${fullname%/*} # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}
fullname_withoutextension=${fullname%.*}
mkdir -p workdir
cat <<'EOD' > workdir/_script_.sh
#!/bin/bash
##############################################################
ret=0
fullname=$(readlink $0) # Take care of symbolic links
dirname=${fullname%/*} # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}
fullname_withoutextension=${fullname%.*}
echo
echo "# ------------- RESULTS ------------- #"
echo "# path to me (\$0)-----------> ${0} "
echo "# arguments called with ----> ${@} "
echo "# \$1 -----------------------> $1 "
echo "# \$2 -----------------------> $2 "
echo "# path to me (\$fullname)----> ${fullname} "
echo "# parent path(\${0%/*})------> ${0%/*} "
echo "# parent path(\$dirname)-----> ${dirname} "
echo "# my name ----\${0##*/}------> ${0##*/} "
echo "# my source -\${BASH_SOURCE}-> ${BASH_SOURCE} "
echo "# parent path(from BASH_SOURCE) -> $(dirname $BASH_SOURCE)"
echo "# my function name -\${FUNCNAME[0]}------> ${FUNCNAME[0]}"
echo "# my source or script real path (realpath)------------------> $realpath"
echo
[ "$realpath" = "workdir" ] || ret=1
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "*********** ERROR **********************************"
[ $ret = 0 ] || echo "*******************************************************"
show_params () {
echo
echo "# --- RESULTS FROM show_params() ---- #"
echo "# path to me (\$0)-----------> ${0} "
echo "# arguments called with ----> ${@} "
echo "# \$1 -----------------------> $1 "
echo "# \$2 -----------------------> $2 "
echo "# path to me (\$fullname)----> ${fullname} "
echo "# parent path(\${0%/*})------> ${0%/*} "
echo "# parent path(\$dirname)-----> ${dirname} "
echo "# my name ----\${0##*/}------> ${0##*/} "
echo "# my source -\${BASH_SOURCE}-> ${BASH_SOURCE} "
echo "# parent path(from BASH_SOURCE) -> $(dirname $BASH_SOURCE)"
echo "# my function name -\${FUNCNAME[0]}------> ${FUNCNAME[0]}"
echo "# my source or script real path (realpath)------------------> $realpath"
echo
[ "$realpath" = "workdir" ] || ret=1
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "*********** ERROR **********************************"
[ $ret = 0 ] || echo "*******************************************************"
}
show_params "$@"
EOD
cat workdir/_script_.sh > workdir/_side_by_side_script_sourced.inc
cat <<'EOD' >> workdir/_script_.sh
echo "# . $realpath/_side_by_side_script_sourced.inc 'hello there' 'william'"
. $realpath/_side_by_side_script_sourced.inc 'hello there' 'william'
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "*********** ERROR **********************************"
[ $ret = 0 ] || echo "*******************************************************"
EOD
chmod +x workdir/_script_.sh
[ -L _mytest_ ] && rm _mytest_
ln -s workdir/_script_.sh _mytest_
# ------------- CALLED ------------- #
called_by () {
echo '=========================================================================='
echo " Called by : " "$@"
echo '=========================================================================='
eval "$@"
}
called_by bash _mytest_
called_by ./_mytest_
called_by bash workdir/_script_.sh
called_by workdir/_script_.sh
called_by . workdir/_script_.sh
# ------------- RESULTS ------------- #
echo
echo
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "*********** ERROR **********************************"
[ $ret = 0 ] || echo "*******************************************************"
echo
[ $ret = 0 ] && echo ".... location of scripts (\$realpath) should always be equal to $realpath, for all test cases at date".
echo
# ------------- END ------------- #