Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple script 3

Status
Not open for further replies.

wchuacs

MIS
Oct 12, 2000
21
US
Hi all experts,

I have a script and here is a part of the script, I am still a learner and don't understand this, pls help to explain what this part of script is trying to do ?


if [ $# -ge 2 ]; then
if [ "$2" != "x" ]; then
CONSTR=$2; export CONSTR
MOLE=$1; export MOLE
shift; shift
#echo $$ $0 $MOLE x $*
$0 $MOLE x $* &
exit
fi

#
# Parse the arguments passed
#
shift; shift
fi

Thank you.
 
if [ $# -ge 2 ]; then (If no. of command line parameters is greater or equal to two)
if [ "$2" != "x" ]; then (If the second parameter isn't x)
CONSTR=$2; export CONSTR (Set variable CONSTR to equal the second parameter and export it)
MOLE=$1; export MOLE (Set the variable MOLE to equal the first parameter and export it)
shift; shift (Shift two command line parameters)
#echo $$ $0 $MOLE x $* (Commented out, so no action)
$0 $MOLE x $* & (Run the script $MOLE in the shell defined by $0 using the parameters, x and remaining parameters, in the background)
exit (leave the script)
fi (end of conditional)

#
# Parse the arguments passed
#
shift; shift (Shift two command line parameters)
fi (end of conditional)

Others will probably be able to explain this better (particularly the shift part, something I've never used - man shift for more details), but I hope some of the above is a help anyway.
 
Here, I've added comments.

When you shift, $1 is discarded, $2 becomes $1, $3 becomes $2, etc.

[tt]# If the number of arguments supplied to the script
# is greater than 2
if [ $# -ge 2 ]; then
# If the second argument is not equal to "x"
if [ "$2" != "x" ]; then
# Set the value of CONSTR to the second parameter,
# and MOLE to the first.
CONSTR=$2; export CONSTR
MOLE=$1; export MOLE
# Remove the first two arguments from the
# argument list.
shift; shift
#echo $$ $0 $MOLE x $*
# Run this script ($0) again with "x"
# as the first parameter , and the remaining
# arguments in the argument list.
$0 $MOLE x $* &
exit
fi

#
# Parse the arguments passed
#
# Remove the first two arguments from the argument
# list.
shift; shift
fi[/tt]


Annihilannic.
 
Thanks for the clarification on shift, Anni. I think my understanding of $0 $MOLE x $* & was faulty too (isn't it "run this script with $MOLE, x and the remaining parameters, in the background"?

I'll have to revisit my shell scripting books, methinks! Cheers.
 
Oops, we cross posted there.

Yep, that's right. I didn't notice the & there either!

Annihilannic.
 
Hi,

Thanks for the great and easy to understand inputs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top