1

I am trying to read multiple directory locations from a text file using a bash script and pass as argument to another python script. The directory name contains space which shows

test.py: error: unrecognized arguments: 1B/PHASE/PHASE_90

I am using following bash script:

filename='filter_directory.txt'
line_counter=1
while read line; do
# reading each line
    echo "Processing Directorie: $line_counter : $line"
    python3 test.py -i $line
    line_counter=$((line_counter+1))
    echo "Finish Directorie: $line_counter!!!"
    done < $filename

test.py is:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--input", "-i", type=str, default='./input', help='Calculation LUMA image folder')
arg = parser.parse_args()
input_path = arg.input
print(input_path)

filter_directory.txt contains:

version_of_study/Study 1B/PHASE/PHASE_90
version_of_study/Study 1B/PHASE/PHASE_91
version_of_study/Study 1B/PHASE/PHASE_92
version_of_study/Study 1B/PHASE/PHASE_93

Could you help me to import the directory name from text file to python by bash script?

0

1 Answer 1

2

You should just quote $line like python3 test.py -i "$line" to group the argument. You can use tools like shellcheck to identify issues like these in shell scripts. This article is a great introduction to the different ways to correct use/avoid variable expansion and word splitting.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the shellcheck link. Genuinely needed a link to some form of shell debugging tool!! I find bug fixing bash like pulling teeth sometimes!!! I tend to resort to lots of echo statements and things like that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.