Click to See Complete Forum and Search --> : Easy script question (for someone ;-)


Blackcat_UK
10-16-2002, 03:55 PM
I've just come across this in a book and wondered what the $1 $2 $3 ...etc do (I know about the rest of the script). Thanks in advance...

g++ -Wall -W $1 $2 $3 $4 $5 $6 $7 $8 $9
./a.out

ferreter
10-16-2002, 04:06 PM
If I'm not mistaken they are the command line arguments for the script.

hlrguy
10-16-2002, 04:09 PM
They represent the command line inputs. For example, if I execute
a script called

multiply_number

and executed it with

multiply_number 45 67

then inside the scripts, I could access $1 (45) and multply it with $2 (67)

Create a script that contains the line

echo $1
echo $2

Then execute the above (chmod 755 <scripname>)
<scriptname> Hello World

hlrguy

Blackcat_UK
10-16-2002, 04:32 PM
That explains it, but I'm still not sure of the reason for their being included in the script. The script is made executable (according to the book) as follows:

$ chmod a+x cg

and is used as below (assuming welcome.cpp is an uncompiled C++ file)

$ cg welcome.cpp

This compiles and runs the C++ prog. (providing no errors!) without having to type in the whole line every time (I'm sure you guys already know this, but hey what the heck).
So, do these numbers allow the compiler to be run using more options that are hard coded into the script (example follows)?

$ cg -I/include <iostream.h> welcome.cpp

Thanks again...

bwkaz
10-16-2002, 06:04 PM
Yes, that's the reason they're there. They will let you pass up to 9 extra arguments to g++ through the script.

Actually, instead of putting $1 $2 $3 etc., etc., it would be better to make that line read like so:

gcc -Wall -W "$@"

That way, all arguments get put in there (even if there are more than 9 of them), and each one is quoted individually.

If you were to use the cg script, but have one of the arguments with spaces in it and quotes around it, that script won't preserve the quoting.

In other words, if you ran cg "my filename.cpp", the actual g++ command line that the script runs will look like g++ -Wall -W my filename.cpp, and this is bad, since "my" isn't a valid option for g++, and "filename.cpp" probably doesn't exist. Using "$@" solves both problems.

Blackcat_UK
10-17-2002, 02:51 AM
Question answered! Thanks guys :-)