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
如果我将相同的命令放在函数中并从命令行调用该函数,则看不到任何输出。stdout 去哪儿了?为什么?
#
# 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
答案1
您的示例中没有stdout
:将 shell 函数定义包装在脚本文件中,然后调用该脚本不会执行该函数。您需要来源脚本,以便该函数在当前 shell 中可用
. Sourced/getFileSystemInfo
getFileSystemInfo $(pwd) fstype,source
或者修改脚本,使其调用该函数并将其自己的参数传递给它,方法是添加如下行
getFileSystemInfo "$@"
现在,您只是调用一个不执行任何操作的脚本,其中的位置参数会被忽略。