Quantcast
Channel: Execute python file from shell script based on the cat/awk output - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by Kusalananda for Execute python file from shell script based on the cat/awk output

$
0
0

The issue is not due to any single quotes (I'm assuming you're referring to the single quotes displayed in the error output; these are just Python's way of displaying the error) but to the fact that you rely on the shell to split $text into separate words on spaces. At the same time, you have removed the space character from the IFS variable before your loop, which means that the shell won't do that splitting correctly.

The correct solution is not to get the shell to split $text on spaces, as that would still also invoke filename globbing on the generated words. Since $text contains $1, $2 and $3, these values would also be split on spaces etc. when $text is split.

Instead of relying on the shell to split the $text variable correctly, read the two words on each line of samp.txt in a while loop:

while read -r script config; do
    out=$( python "$script" "$config" -c "$1" -s "$2" -t "$3" )
done <samp.txt

If you want to use a separate variable for your arguments, and if you're using e.g. bash or another shell that has named arrays:

args=( -c "$1" -s "$2" -t "$3" )
while read -r script config; do
    out=$( python "$script" "$config" "${args[@]}" )
done <samp.txt

Using an array for the arguments instead of a text string allows the argument to be properly delimited within the array. The expansion of "${args[@]}" would be the separate elements of that array with each element properly quoted. This means that e.g. $1, $2 and $3 could contain spaces etc. without any issues.

In a POSIX (/bin/sh) script, assuming you can safely discard the positional parameters from $4 onwards:

set -- -c "$1" -s "$2" -t "$3"
while read -r script config; do
    out=$( python "$script" "$config" "$@" )
done <samp.txt

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>