Reputation: 5490
I have a python script p.py
which does exit("ABC")
for some files. I would like to write a Ubuntu shell to copy the files which make the script exit("ABC")
into a folder:
#!/bin/bash
FILES=*.txt
TOOL=p.py
TAREGT=../TARGET/
for f in $FILES
do
if [ $(python $TOOL $f) = "ABC" ]
then
echo "$f"
cp $f $TARGET
fi
done
but the condition check if [ $(python $TOOL $f) = "ABC" ]
does not seem to work, it says ./filter.sh: line 13: [: =: unary operator expected
. Could anyone tell me what is wrong?
Upvotes: 0
Views: 239
Reputation: 70213
The parameter to exit()
is what the Python script returns (success / error). (Documentation of Python's exit()
. Note how exit( "ABC" )
doesn't return "ABC"
, but prints that to stderr
and returns 1
.)
The return code is what ends up in the $?
variable of the calling shell, or what you would test for like this:
# Successful if return code zero, failure otherwise.
# (This is somewhat bass-ackwards when compared to C/C++/Java "if".)
if python $TOOL $f
then
...
fi
The $(...)
construct is replaced with the output of the called script / executable, which is a different thing altogether.
And if you're comparing strings, you have to quote them
if [ "$(python $TOOL $f)" = "ABC" ]
or use bash's improved test [[
:
if [[ $(python $TOOL $f) = "ABC" ]]
Upvotes: 1