If I run the following command I get the expected output as shown.
jonathan@Aristotle:~/EclipseWorkspaces/AGI/ShellUtilities$ df --output=fstype,source $(pwd)
Type Filesystem
ext4 /dev/sdb1
如果我将相同的命令放在一个函数中并从命令行调用该函数,我看不到任何输出。标准输出去了哪里,为什么?
#
# Copyright © Jonathan Gossage, 2016
#
# getFileSystemInfo
#
function getFileSystemInfo {
# Get an item of information related to the file system that contains a
# specific file or directory.
# $1 is the file or directory to be checked
# $2 is the specific information type to be checked. See df(1) FIELD_LIST
# for full details. As in df you can get more than one type of
# information.
# To access the information returned, capture stdout in your script.
echo "We are in getFileSystemInfo"
df --output=$2 "$1"
return $?
}
jonathan@Aristotle:~/EclipseWorkspaces/AGI/ShellUtilities$ Sourced/getFileSystemInfo $(pwd) fstype,source
您的示例中没有
stdout
:将 shell 函数定义包装在脚本文件中,然后调用脚本不会执行该函数。您需要获取脚本,以便该函数在当前 shell 中可用或修改脚本,使其调用函数并将自己的参数传递给它,方法是添加一行,例如
现在,您只是调用一个什么都不做的脚本,而位置参数被忽略。