Bash:从bash函数返回一个字符串 [英] Bash: Return a string from bash function

查看:299
本文介绍了Bash:从bash函数返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个bash函数来将动态构造的字符串返回给调用者空间. 即

I need a bash function to return a dynamically constructed string to returned to the caller space. i.e,

makeName()
{
    echo "Enter Ext: "
    read ext
    return "$fileName.$1.$ext.log"
}


echo -n "Enter fileName:"
read fileName

name1=makeName "type1"
name2=makfName "type2"

因此,我可以使用相同的基本文件名获得两个不同的文件名. 我尝试这样做,

So I can get two different filenames with same base file name. I tries doing like this,

# echo $(makeName "type1")

但是此代码没有任何原因或错误地被插入到某处.我希望它也可以通过该过程接受某种I/O.那没有发生.

But this code is struck somewhere with out any reason or error. I expect it to accept some kind of I/O with that proc also. That is not happening.

推荐答案

bash使用的return语句用于返回数字值,作为状态代码,由调用函数通过$?进行检索.您不能返回字符串.另请参阅

The return statement used by bash is used to return a numeric value as a status code to be retrieved through $? by the calling function. You can not return a string. See also

  • Returning Values from Bash Functions
  • How to return a string value from a Bash function

您可以使用@konsolebox建议的特殊全局变量,也可以使用echo函数内部的返回值,并在调用函数时使用命令替换:

You can either use a special global variable as proposed by @konsolebox, or echo the return value inside your function, and use command substitution when calling the function:

makeName()
{
    echo "$fileName.$1.log"
}


echo -n "Enter fileName:"
read fileName

name1=$(makeName "type1")
name2=$(makeName "type2")

echo $name1
echo $name2

[UPDATE]

更新后的问题表明,您打算读取makeName函数中的另一个值,而该函数还打算为用户提供echo某些提示.因此,在这种情况下,命令替换方法将不起作用-您需要使用全局变量,例如

The updated question shows that you intend to read yet another value inside the makeName function, while the function also intends to echo some prompt for the user. So the command substitution approach will not work in that case - you need to use a global variable, like

makeName() {
    echo -n "Enter Ext: "
    read ext

    __="$fileName.$1.$ext.log"
}


echo -n "Enter fileName:"
read fileName

makeName "type1" ; name1=${__}
makeName "type2" ; name2=${__}

echo $name1
echo $name2

$ ./sample.sh 
Enter fileName:filename
Enter Ext: ext1
Enter Ext: ext2
filename.type1.ext1.log
filename.type2.ext2.log

更好的做法是,为获得更简洁的代码并避免在函数内部使用全局变量,可以使用

Yet better, for cleaner code and to avoid using global variables inside your function, you could use the approach described at Returning Values from Bash Functions and pass the name of the return variable as parameter, and ideally also pass the fileName as parameter:

makeName() {
    local  __type=$1
    local  __fileName=$2
    local  __resultvar=$3
    local ext
    local myresult

    echo -n "Enter Ext: "
    read ext
    myresult="$__fileName.$__type.$ext.log"

    eval $__resultvar="'$myresult'"
}

echo -n "Enter fileName:"
read fileName

makeName "type1" $fileName theResult ; name1=${theResult}
makeName "type2" $fileName theResult ; name2=${theResult}
echo $myresult

echo $name1
echo $name2

旁注:请参见

Side Note: See Why should eval be avoided in Bash, and what should I use instead? for a discussion why eval should be avoided. When using bash version 3.1 or higher, you can use printf instead of eval:

...
printf -v "$__resultvar" '%s' "$myresult"
...

最后,在bash 4.3或更高版本中,我们可以使用declare -n nameref 属性分配给变量,以便该变量实际上是引用另一个变量:

And, finally, with bash 4.3 or higher, we can assign the nameref attribute to a variable using declare -n, so that the variable is effectively a reference to another variable:

...
declare -n myresult=$3

myresult="$__fileName.$__type.$ext.log"
...

这篇关于Bash:从bash函数返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆