2016-06-07 72 views
4

我想创建一个名为传递给该函数的参数的var。有可能吗?然后过滤它的内容,以便在读取数据前后删除可能的空格和单引号。不工作示例:Bash。使用参数名称创建var并清除它的值

function test() { 
    read $1 
    [[ ${1} =~ ^\'?(.*)(\'.*)?$ ]] && 1="${BASH_REMATCH[1]}" 
} 

test "testingvar" 
#At this point, the user sent some data to the read statement 
echo $testingvar 

对于在read语句中收到的数据,我们可以收到一些不同的字符串。让我们来看看这3个例子:

/path/file 
'/path/file' 
/path/file' <-notice a space after the quote 

在所有例子中,正则表达式必须清理并让/无可能引号和空格路径/文件....和我说,都在一个称为的帕拉姆VAR功能。是梦还是可以在bash中完成?提前致谢。

+0

[壳牌参数扩展(https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion)和'$ {!名}'可能是什么你在追求。请注意,在'read'中,通常你会指定没有'$'的名字(例如'read -r value'),因为你想读入变量,而不是读入它的当前值。 –

+0

'test'对于shell函数来说是一个可怕的名字......'test'已经是大多数现代shell中的内置shell。 – twalberg

+0

是的,你是对的。但在我的真实剧本中,它有另一个名字。谢谢你的提示。 – OscarAkaElvis

回答

2

下面是做这件事:

fun(){ 
    read -r "$1" 
    declare -n var="$1" #use a nameref -- see `help declare` 
    var=${var//[\' ]/} #clean the contents with string substitution 
} 
fun testingvar <<<"/path/file"; echo "$testingvar" 
fun testingvar <<<"'/path/file'"; echo "$testingvar" 
fun testingvar <<<" /path/ file'"; echo "$testingvar" 

此输出:

/path/file 
/path/file 
/path/file 

即,所有的投入得到了清理,并投入经$1传递其命名变量。


Namerefs:

基本上,namerefs像自动取消引用指针,除了它们指向的变量,而不是地址。它们既可以用作l值也可以用作r值,并且它们在创建后始终是自动引用的。

您可以使用namerefs要解决的事实,你不能分配给变量变量,即你不能做的:

foo=bar 

然后

$foo=42 #illegal 

到分配42至bar,但你可以做:

declare -n foo=bar 
foo=42 #now bar is 42 

编辑: 如果希望只在开头和结尾删除所有单引号和空格,但是,你可以使用extglob

fun(){ 
    local settings="$(shopt -p extglob)" #save extglob settings 
    shopt -s extglob #set extglob 
    read -r "$1" 
    declare -n var="$1" #use a nameref -- see `help declare` 
    var=${var##+([ \'])}; var=${var%%+([ \'])} 
    eval "$settings" #restore extglob settings 
} 
fun testingvar <<<"/path/file"; echo "$testingvar" 
fun testingvar <<<"'/path/file'"; echo "$testingvar" 
fun testingvar <<<" /pa'th/ f'ile'"; echo "$testingvar" 

编辑2 - nameref少用EVAL版本:

fun(){ 
    local settings="$(shopt -p extglob)" #save extglob settings 
    shopt -s extglob #set extglob 

    local var 
    read -r var; var=${var##+([ \'])}; var=${var%%+([ \'])} 
    eval "$1=\$var" #avoids interpolating the var value for eval to avoid code injections via stdin 

    eval "$settings" #restore extglob settings 
} 
fun testingvar <<<"/path/file"; echo "$testingvar" 
fun testingvar <<<"'/path/file'"; echo "$testingvar" 
fun testingvar <<<" /pa'th/ f'ile'"; echo "$testingvar" 
+0

非常好!但替代对我来说是无效的。它清除了空格......我只需要在数据前后清理空格。该路径可能包含空格,不应删除。 – OscarAkaElvis

+0

@OscarAkaElvis我编辑了答案。 – PSkocik

+1

可以注意到,namerefs('declare -n')仅在'bash' 4.3+中可用。 –

相关问题