Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从bash中包含正斜杠的变量中删除字符串的一部分_Bash_Testing_Split_Printf_Echo - Fatal编程技术网

从bash中包含正斜杠的变量中删除字符串的一部分

从bash中包含正斜杠的变量中删除字符串的一部分,bash,testing,split,printf,echo,Bash,Testing,Split,Printf,Echo,我想为测试我的C程序的bash脚本打印一个字符串数组。数组内容如下所示: PASSED TESTS: (4) ✓ tests/test_sample_one.in ✓ tests/test_sample_two.in ✓ tests/test_sample_three.in ✓ tests/test_sample_four.in PASSED TESTS: (4) ✓ test_sample_one ✓ test_sample_two ✓ test_sample_three ✓ test_

我想为测试我的C程序的bash脚本打印一个字符串数组。数组内容如下所示:

PASSED TESTS: (4)

✓ tests/test_sample_one.in
✓ tests/test_sample_two.in
✓ tests/test_sample_three.in
✓ tests/test_sample_four.in
PASSED TESTS: (4)

✓ test_sample_one
✓ test_sample_two
✓ test_sample_three
✓ test_sample_four
但是,我想这样打印出来:

PASSED TESTS: (4)

✓ tests/test_sample_one.in
✓ tests/test_sample_two.in
✓ tests/test_sample_three.in
✓ tests/test_sample_four.in
PASSED TESTS: (4)

✓ test_sample_one
✓ test_sample_two
✓ test_sample_three
✓ test_sample_four
这是我为打印结果而编写的代码的一部分。我认为这是所有相关的,但我可以根据要求发布更多的代码

我已经成功地使用了
I=${I%.*}
行删除了
.in
结尾的
,但是
I=${I}*/}
行似乎无法删除
/
前面的字符

printf "PASSED TESTS: ($passed_tests)\n\n"
for i in "${passed_tests_arr[@]}"
do
    printf "✓ "
    i=${i%.*}
    i=${i#*/ }
    echo "$i"
done

代码是正确的,除了第6行的脚本中有一个空格

printf "PASSED TESTS: ($passed_tests)\n\n"
for i in "${passed_tests_arr[@]}"
do
    printf "✓ "
    i=${i%.*}
    i=${i#*/ }
    echo "$i"
done

没有空白,代码为:

printf "PASSED TESTS: ($passed_tests)\n\n"
for i in "${passed_tests_arr[@]}"
do
    printf "✓ "
    i=${i%.*}
    i=${i#*/}
    echo "$i"
done
然后,此代码可以打印正确的输出:

PASSED TESTS: (4)

✓ test_sample_one
✓ test_sample_two
✓ test_sample_three
✓ test_sample_four

您还有一个额外的空间。
您可以使用

printf "PASSED TESTS: ($passed_tests)\n\n"
for i in "${passed_tests_arr[@]}"
do
    # Not needed: printf "✓ "
    i=${i%.*}
    # Not needed: i=${i#*/ }
    echo "${i/ *\// }"
done

你的脚本中有空白,删除空白将执行工作{$i**/} @ SigMalHA。对于这个问题,对叠加流采取什么适当的措施。如果语法正确,那么最好删除这个问题,或者在回答问题时给出一些建议,说明为什么它会起作用✓ 被印在这上面?我以为✓ 是您输入行的一部分。否则,简单的替换似乎更好。最后一行是否可以替换为
echo“✓ ${i/*\/}“
这是可能的,但我会选择echo”✓ ${i#*/}”