johnlemon
johnlemon

Reputation: 21489

How can I get php result and use it in bash?

I have a php file that outputs 0 or 1. How can I use this in bash as a condition ?

Upvotes: 2

Views: 1385

Answers (2)

Daniel Beck
Daniel Beck

Reputation: 6513

#!/bin/bash
result=$( php script.php )
if [ "$result" = "0" ] ; then
    # do stuff
else 
    # do other stuff
fi

Not sure on how PHP is called (parameters etc.)

Upvotes: 5

M'vy
M'vy

Reputation: 5774

That should work provided your php script writes to stdout and output only 0 on the first line.

exec 6<&0 #Saves stdin file descriptor to #6 fd.
exec < `php <your_script>`

read a

if 
    [ $a -eq 0 ]
then
    What you want to do
else 
    The other solution
fi

exec 0<&6 6<&- #Restores stdin and clear fd #6

Upvotes: 1

Related Questions