命令没有bash函数调用发现 [英] Command not found on bash function call
问题描述
您好我是新来bash脚本编程,但不明白为什么我得到的未找到命令的错误,当我试图给一个局部变量参数20120920 5.这个函数调用的结果
Hi i'm new to bash scripting but cannot understand why i get the command not found error when i try to assign to a local variable the result of this function call with parameters 20120920 5.
#!/bin/bash
function nDaysAgo () #date # daysago
{
date -d "${1} - ${2} days" +%Y%m%d;
}
这样的脚本名称是ndaysago,我是第一个调用的脚本。 ndaysago然后像这样分配值:
so the script name is ndaysago, i'm first invoking the script with . ndaysago and then assigning the value like this:
newdate= nDaysAgo 20120910 5
它打印:20120905:找不到命令
it prints: 20120905: command not found
含义的日期执行制作,但随后试图使用输出作为命令,那不是我所希望的。
Meaning that the date execution is made but then tries to use the output as command, thats not what i would expect.
我也尝试过像这样的函数内分配新的值给一个变量:
i have also tried assigning the new value to a variable within the function like so:
#!/bin/bash
function nDaysAgo () #date # daysago
{
var=$(date -d "${1} - ${2} days" +%Y%m%d)
}
但仍然一无所获,MMMMMM
but still nothing, mmmmmm
推荐答案
空格不在身边允许为变量赋值时, =
。要调用你应该使用这就是所谓的 $(...)语法功能=http://tldp.org/LDP/abs/html/ commandsub.html相对=nofollow>命令替换。
Spaces are not allowed around the =
when assigning a variable. To invoke a function you should use the $(...)
syntax which is called command substitution.
更改为:
newdate=$(nDaysAgo 20120910 5)
这篇关于命令没有bash函数调用发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!