如何使用包含带有空格linux的字符串的数组来执行命令

时间:2015-05-19 08:49:43

标签: arrays linux bash command-line-arguments

关于bash shell数组,我现在已经陷入了几个小时的问题。

我正在使用一个使用@{int i = 0;} <div class="form-group form-inline col-xs-12"> @foreach (var property in ViewData.ModelMetadata.Properties) { if (property.PropertyName.StartsWith("Z") || property.IsReadOnly) { continue; } <div class="col-xs-6"> @Html.Label(property.DisplayName, new { @for = property.PropertyName, @class = "control-label col-xs-3" }) @Html.TextBox(property.PropertyName, new { @class = "form-control" }) </div> if (++i % 2 == 0) { @:</div><div class="form-group form-inline col-xs-12"> } } </div> 作为分隔符从inputFile创建的字符串数组。

数组的每个索引可能包含由空格分隔的多个单词。每个字符串表示可以与另一个shell脚本的执行一起使用的Options / Arguments的组合。

我尝试使用以下语法的变体将这些字符串提供给我编写的其他shell脚本:

IFS=$'\n'

你会注意到我用来循环数组的一些不同变体(或尝试通过直接从 # Run the testcases, case by case for testcase in `cat $inputFile` ###"${testcases[@]}" #for ((i = 0; i < ${#testcases[@]}; i++)) do #testcase="${testcases[i]}" # Print each case in inputFile echo "${testcase}" # Run the Archive Script, redirect output to /dev/null ./archive.sh "${testcase}" > /dev/null # Respond to $# return value done 的输出中读取一个局部变量来直接响应。

最令人沮丧的是echo命令工作,并用空格打印字符串。但是,当我的脚本尝试使用字符串执行时,第一个单词将作为整体读取,但在第一个空格后,脚本会尝试一次执行一个字符。 :(

请帮忙!这是执行脚本时得到的输出:

cat

这是完整脚本,我错过了一些关键线触发和错误:

$ test_archive.sh lab6_testcase
Testcases Count: 6
Running cases:
-v dirA
./archive.sh: illegal option --  
./archive.sh: illegal option -- d
./archive.sh: illegal option -- i
./archive.sh: illegal option -- r
./archive.sh: illegal option -- A
dirB
-v -r dirA
./archive.sh: illegal option --  
./archive.sh: illegal option -- -
./archive.sh: illegal option -- r
./archive.sh: illegal option --  
./archive.sh: illegal option -- d
./archive.sh: illegal option -- i
./archive.sh: illegal option -- r
./archive.sh: illegal option -- A
-v dirA dirB
./archive.sh: illegal option --  
./archive.sh: illegal option -- d
./archive.sh: illegal option -- i
./archive.sh: illegal option -- r
./archive.sh: illegal option -- A
./archive.sh: illegal option --  
./archive.sh: illegal option -- d
./archive.sh: illegal option -- i
./archive.sh: illegal option -- r
./archive.sh: illegal option -- B
dirc
-a
./archive.sh: illegal option -- a
TEST SUMMARY
SUCCESSFUL CASES:0
USAGE CASES:0
INVALID ARGUMENT CASES:6
-v dirA
dirB
-v -r dirA
-v dirA dirB
dirc
-a

3 个答案:

答案 0 :(得分:1)

问题是由于您的IFS仍然设置为$'\n'所以./archive.sh正在将整行作为单个参数读取并尝试理解这些字母。

将IFS更改回' ',问题应该消失了!

示例:

    # Run the testcases, case by case
    for testcase in "${testcases[@]}"
            do
                    IFS=' '
                    echo "${testcase}"

                    ./archive.sh "${testcase}" > /dev/null

done

答案 1 :(得分:0)

 ./archive.sh "${testcase}"

您使用单个参数调用archive.sh(带有嵌入的空格)。这就是双引号的作用。双引号内的所有内容都是一个单词。

由于参数的第一个字符是破折号,archive.sh将其余参数(包括嵌入空格)作为选项,并抱怨它们不是有效选项。

您想删除引号。 Demo

答案 2 :(得分:0)

经过一番挖掘后,我找到了解决问题的方法。

问题是shell在评估/解除引用变量(或数组值)的内容之前执行命令。

因此,在执行变量/数组的命令之前,必须使用eval强制shell取消引用变量/数组并检索内容。

这是一个工作版本(直接从文件中读取):

for testcase in `cat $inputFile`
        do
                #testcase="${testcases[i]}"
                # Print each case in inputFile
                echo "  ${testcase}"

                # Run the Archive Script, redirect output to /dev/null
                eval ./lab5.sh ${testcase} > /dev/null

... CONTINUE WITH SCRIPT ...