vendredi 22 avril 2016

Remove part of a string from a variable in bash containing a forward slash

I have an array of strings I want to print for a bash script that tests my C programs. The arrays contents are like this:

PASSED TESTS: (4)

✓ tests/test_sample_one.in
✓ tests/test_sample_two.in
✓ tests/test_sample_three.in
✓ tests/test_sample_four.in

I however want to print it out like this:

PASSED TESTS: (4)

✓ test_sample_one
✓ test_sample_two
✓ test_sample_three
✓ test_sample_four

Here is the part of the code I've written for printing out the results. I think this is all that is relevant but I can post further code upon request.

I have successfully used the line i=${i%.*} to remove the .in on the end but the line i=${i#*/ } doesn't seem to work at removing the characters before the /.

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

Aucun commentaire:

Enregistrer un commentaire