PHP & bash; Linux; Compile my own function

I would like to make my own program, but I have no idea how ... for example, I want to create a typical Hello $ user program.

So..

├── hi
│   ├── hi.sh
│   ├── hi_to.sh

      


hi.sh

#!/bin/bash
~/hi/hi_to.sh $1 

      


hi_to.sh

#!/usr/bin/php
<?php
    echo "\nHellO ".$argv[1]."\n";
?>

      


Run it in terminal:

me:~/hi  
 ./hi.sh User

HellO User

      


and my question is, how do I compile all these files into one bash program?

+2


a source to share


4 answers


No. If you want it in one script, then you put it in one script in the first place.



+1


a source


I don't think we understand this question because you can simply call hi_to.sh like this:

./hi_to.sh user

      



And it will work as you want by getting rid of the first sh script.

0


a source


  • Make sure the shebang line points to the correct php executable
  • You don't need to call script hi.php, just call it hi

  • Create an executable script (e.g. via chmod u+x path/to/hi

    or chmod a+rx path/to/hi

    , see http://en.wikipedia.org/wiki/Chmod )
  • Make sure the file is in the PATH search for the users / accounts your script should be using (without entering an absolute path)
0


a source


The only way I could see this "combined" is to use here-doc, which basically makes the first script generate the second and then execute it:

#!/bin/sh

cat << EOF > /tmp/$$.php
<?php
    \$string="$1";
    echo "\nHellO ". \$string ."\n";
?>
EOF

/usr/bin/php -q /tmp/$$.php
retval=$?

rm /tmp/$$.php

exit $retval

      

This example $1

will expand to the first argument. I have avoided other variables (which are only PHP related) that PHP will expand when it runs. $$

in a shell script just expands to the PID script, the actual temp file would be something like /tmp/1234.php

. mktemp(1)

is a much safer way to create a temporary filename that is more resistant to attacks and link conflicts.

It also stores the PHP exit status in retval

, which is then returned after the script exits.

The resulting file will look like this (if the first argument to the shell script is foo

):

<?php
   $string="foo";
   echo "\nHello " . $string . "\n";
?>

      

This is kind of a bad demonstration of how to use bash to write other scripts, but at least it demonstrates that it is possible. This is the only way I could think of to "merge" (as you pointed out) the two scripts you posted.

0


a source







All Articles