I have a python script that requires arguments and these arguments are file paths. This script works fine when executed like this:
/my_python_script "file_path1" "file_path2"
(i added quotes as some file names may have weird characters)
the issue happens when i launch my python script from within a bash script. i do get the variable file_paths as "file_path1" "file_path2"
in my bash script i have:
echo "$file_paths"
and this will return:
"file_path1" "file_path2"
but when my bash script contains:
/my_python_script "$file_paths"
it produces an incorrect result
Can anyone please shed some light into what i'm missing
source_folder=/my_files
cd "$source_folder"
if [ "$(ls -A)" ]; then
for PDF_file in *
do
quoted_filename=\"$source_folder/$PDF_file\"
all_PDFs="$all_PDFs $quoted_filename"
done
fi
echo "$all_PDFs"
/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf "$all_PDFs"
this will output:
"/my_files/1.pdf" "/my_files/2.pdf"
my python script (part of the OS) is supposed to join these PDFs but it produces an empty PDF
There is no quote removal done on the results of parameter expansion. The file names seen by the python script in this case include those quotes.
In this example, the shell will remove those quotes after it's done processing the command, so the python script will not see those quotes as part of the filenames.
thank you but that didn't solve the issue with weird names within paths. files with names containing spaces (as an example) will be omitted from being joined, all other ones will be joined properly.
Pathname expansion, which expands the asterisk into the filenames, happens after field splitting and all other subsitutions. This is by design. It is immune to "weird characters".
You can't help unwanted things splitting on spaces when your list is separated by spaces.
Most here could have suggested that if you'd posted your complete script, or at least what your actual intentions are("want to combine all PDF's in this folder", not "want to split this string so I can feed names into a thing"). Without that, we're stuck working with what you've given us.