简体   繁体   中英

Running php via a unix script

I have the above shell script .

#!/bin/bash

# a shell script that keeps looping until an exit code is given

nice php -q -f ./data.php -- $@
ERR=$?

exec $0 $@

I have a few doubts

  1. What is $0 and what is $@
  2. what is ERR=$?
  3. what does -- $@ in 5th line do
  4. I wanted to know if can i pass data.php as a parameter. so that i have only i shell script for all kind of execution . Say, i want to run "sh ss.sh data1.php" then this should run data1.php, if run "ss ss.sh data2.php" it should run data2.php –

1) $0 is the name of the executable (the script in your case, eg: if your script is called start_me then $0 is start_me )

2) ERR=$? gets the return code of nice php -q -f ./data.php -- $@

3) -- $@ does two things, first of all it tell the php command that all following parameter shall be passed to data.php and $@ passes all given parameter to the script to ./data.php (eg. ./your_script_name foo bar will translate to nice php -q -f ./data.php -- foo bar )

4) short answer yes, but you have to change the script to

 YOUR_FILE=$1
 shift #this removes the first argument from $@
 nice php -q -f ./$YOUR_FILE -- $@
$0 

is the name of the script.

$@ 

are the arguments given to the script

ERR=$? 

catches the status code of the previous command

php_command="php -q -f $1"
shift
nice $php_command -- $@

You take the first parameter for the f-flag, then you shift it off the parameter list and pass the rest after the double dashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM