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?
1 Answer 1
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.