3

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 –
5
  • $@ the arguments $0 the first argument ERR is the return value from the execution Commented May 2, 2012 at 14:12
  • Doubts? Commented May 2, 2012 at 14:18
  • -- $@ passes all the arguments to php linux.die.net/man/1/php Commented May 2, 2012 at 14:19
  • now I understand your 4th question .. try something like "`php $1 -- $@`` Commented May 2, 2012 at 14:21
  • cant seem to be able to get those quotations right.. arrgh Commented May 2, 2012 at 14:24

2 Answers 2

1

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 -- $@
Sign up to request clarification or add additional context in comments.

Comments

0
$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.

3 Comments

i want to run "sh ss.sh data.php" . this run run data.php i if run "ss ss.sh data2.php" it should run data2.php
will the last line holds true in your case i.e exec $0 $@
You should not call the exec-function because it will execute your program a second time. This could cause a recursive loop.

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.