2

I would like to make my own program but I have no idea how.. for example I want to make 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 to compile all this files into one bash program?

3
  • Can you explain in more detail what you mean by "compile all this files into one bash program"? Commented May 1, 2010 at 12:34
  • I want to make one bash command, like 'cd, cal & so on..'. In this case 'hello' command. not simple to add an alias in '.bashrc'.. Commented May 1, 2010 at 12:45
  • 1
    Make a directory called "~/bin". Edit your ~/.bashrc file so that your PATH includes ~/bin. Copy your script into ~/bin, renaming it to "hi" (without .sh). After you restart your session or source ~/.bashrc from the command line, the new PATH will take effect. Then you can type "hi User" at the command line et voilà. Commented May 1, 2010 at 13:03

4 Answers 4

1

You don't. If you want it in one script then you put it in one script in the first place.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't think we understand the question, because you could just call hi_to.sh like this:

./hi_to.sh user

And it would run like you want, getting rid of the first sh script.

1 Comment

I've made one more complicated script with php&bash and here I've tried to make a simple example for explaining what I want..
0
  1. Make sure the shebang line points to the correct php executable
  2. You don't have to call the script hi.php, just call it hi
  3. Make your script file executable ( e.g. via chmod u+x path/to/hi or chmod a+rx path/to/hi, see http://en.wikipedia.org/wiki/Chmod)
  4. Make sure the file is within the search PATH for the user/accounts that are supposed to use your script (without typing the absolute path)

Comments

0

The only way I could see this 'combined' is by using a here-doc, which basically causes the first script to generate the second, 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

In that example, $1 will expand to the first argument. I have escaped the other variables (that are related only to PHP), which PHP will expand when it runs. $$ in a shell script just expands to the PID of the script, the actual temporary file is going to be something like /tmp/1234.php. mktemp(1) is a much safer way to make a temporary file name that is more resistant to link attacks and collisions.

It also saves the exit status of PHP in retval , which is then returned when the script exits.

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

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

This is kind of an icky demonstration for how to use bash to write other scripts, but at least demonstrates that its possible. Its the only way I could think of to 'combine' (as you indicated) the two scripts that you posted.

3 Comments

You could do that without a temporary file: (@ indicates a newline since we can't format comments) php -q /dev/stdin <<EOF@<?php echo "hello\n"; ?>@EOF
@Dennis, if you want to edit my answer to add that, feel free :) I'm relatively sure (at this point) the OP will want to see the results of the here-doc in a file for debugging, especially due to the need to escape variables that php, not bash should expand.
No, I was just offering a way to have things more "compiled" as it were. I'm not advocating either approach. Doing things such as this is just asking for trouble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.