将函数输出保存到参数中命名的变量中 [英] Saving function output into a variable named in an argument
问题描述
#!/ bin / bash
getFolder(){
local __myResultFolder = $ 1
本地文件夹
for d in * /;做
$ folder = $ d
done
__myResultFolder = $ folder
return $ folder
}
getFolder FOLDER
echo使用文件夹:$ FOLDER
然后我将这个简单的脚本保存为folder_test.sh并将它放在只有一个文件夹的文件夹中,将所有者更改为我,并给予正确的权限。但是,当我运行脚本时,我不断收到错误...
./ folder_test.sh:8 ./folder_test。 sh:= test_folder /:找不到
我已经尝试将 $文件夹= $ d
部分在不同类型的报价单,但没有任何作用。我试过 $ folder ='$ d'
, $ folder =`$ d`
code> $ folder =$ d但没有一个工作。驾驶我疯狂,任何帮助将不胜感激。谢谢。
罪魁祸首是
$ folder = $ d
这是将文件夹名称与一个 =
签名之前,尝试以该名称扩展它,即字面上将名称 = test_folder /
视为可执行文件在shell下运行,但没有找到该名称的文件。将其更改为
folder = $ d
另外, bash
函数的返回值只限于整数类型,您不能向调用函数发送字符串。如果您想在 $ folder
为空的调用函数发送一个非零返回码,您可以添加一行
[pre>
如果[-z$ folder];然后返回1;否则返回0; fi
(或)如果要从函数返回字符串值,请不要使用 return
,只需执行 echo
的名称,并使用命令替换功能名称,即
getFolder(){
local __myResultFolder = $ 1
本地文件夹
for d in * /;做
文件夹= $ d
完成
__myResultFolder = $文件夹
回显$文件夹
}
folderName = $(getFolder FOLDER )
echo$ folderName
I have an interesting problem that I can't seem to find the answer for. I am creating a simple app that will help my dev department auto launch docker containers with NginX and config files. My problem is, for some reason I can't get the bash script to store the name of a folder, while scanning the directory. Here is an extremely simple example of what I am talking about....
#!/bin/bash
getFolder() {
local __myResultFolder=$1
local folder
for d in */ ; do
$folder=$d
done
__myResultFolder=$folder
return $folder
}
getFolder FOLDER
echo "Using folder: $FOLDER"
I then save that simple script as folder_test.sh and put it in a folder where there is only one folder, change owner to me, and give it correct permissions. However, when I run the script I keep getting the error...
./folder_test.sh: 8 ./folder_test.sh: =test_folder/: not found
I have tried putting the $folder=$d
part in different types of quotes, but nothing works. I have tried $folder="'"$d"'"
, $folder=`$d`
, $folder="$d"
but none of it works. Driving me insane, any help would be greatly appreciated. Thank you.
The culprit is the line
$folder=$d
which is treating the folder names to stored with a =
sign before and tried to expand it in that name i.e. literally treats the name =test_folder/
as an executable to be run under shell but does not find a file of that name. Change it to
folder=$d
Also, bash
functions' return value is only restricted to integer types and you cannot send a string to the calling function. If you wanted to send a non-zero return code to the calling function on $folder
being empty you could add a line
if [ -z "$folder" ]; then return 1; else return 0; fi
(or) if you want to return a string value from the function, do not use return
, just do echo
of the name and use command-substitution with the function name, i.e.
getFolder() {
local __myResultFolder=$1
local folder
for d in */ ; do
folder=$d
done
__myResultFolder=$folder
echo "$folder"
}
folderName=$(getFolder FOLDER)
echo "$folderName"
这篇关于将函数输出保存到参数中命名的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!