我有一个字符串数组,我想为测试我的 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
这是我为打印结果而编写的代码部分。我认为这就是所有相关的内容,但我可以根据要求发布更多代码。
我已经成功地使用行 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
关于bash - 从包含正斜杠的 bash 中的变量中删除字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807182/