passing arguments to external script

Hi!

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

I sincerely doubt that. echo doesn't put quotes around things like that.

What's the actual contents of the file_paths variable?

Incorrect in what fashion?

my script is:

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

Don't use quotes:

/my_python_script $file_paths

Franklin52,

i tried that with same undesired result

What happens when you run the script manually with those names?

There may be other reasons than filenames that your script is failing.

Also, what are the exact contents of the file_paths variable? Not made up, not paraphrased.

my bash script currently end like this:

/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf "$all_PDFs"

and it this will output:

"/my_files/1.pdf" "/my_files/2.pdf"

if i add two lines as per below:

sleep 1 # to create a different output file
/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf "/my_files/1.pdf" "/my_files/2.pdf"

will produce the expected result

---------- Post updated at 12:36 PM ---------- Previous update was at 11:38 AM ----------

echo $all_PDFs
"/my_files/1.pdf" "/my_files/2.pdf"

/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf $all_PDFs

this command produces an empty PDF (incorrect)

/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf "/my_files/1.pdf" "/my_files/2.pdf"

this command produces a correct result, the only difference is replacement of $all_PDFs with content... what am i doing wrong?

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.

Regards,
Alister

1 Like

and how can i pass the correct paths if i have weird characters?

all_PDFS="$(echo $all_PDFS | tr -d '"')"

Depending on your shell you may be able to accomplish this with less expensive string operations as well.

If your shell's extremely old you may have to use backticks instead of $( )

Corona688,

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.

Why not simply do the following instead?

source_folder=/my_files
/Scripts/Combine_PDFs_python.py -o /TEST`date +%H-%M-%S`.pdf "$source_folder"/*

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".

Regards,
Alister

1 Like

alister,

that's the difference between an amateur like me and a pro like you :slight_smile: you need only 2 lines to complete a script

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.

1 Like

sorry for the confusion