On Fri, 2002-08-30 at 09:25, Mario Lombardo wrote:
> Ok, now I'm very confused. My output looks exactly like yours below,
> yet I get better results with insmod. How does that make any sense?
>
> What's "value of $0?" An "echo $0" returns "-bash."
Whenever a program runs in Unix, the command line that started it is
passed as an "argv" list of parameters to the main() function. The first
parameter is always the command that started the currently running
program, and the rest of the parameters are arguments that you included
on the command line. You can actually change argv[0] and the command
name will change in the process table ("ps" will show whatever you set
in argv[0]).
In sh/bash/csh/ksh/..., these command line parameters are mapped to $0
through $9. So, if you write a script like this:
$ cat > myscript.sh <<EOF
#!/bin/bash
echo $0
EOF
$ chmod 755 myscript.sh
$ ./myscript.sh
This will print "./myscript.sh", as that is the name of the command that
started this script. Note the included path before the script name.
But...
$ ln -s myscript.sh foo.sh
$ ./foo.sh
Will print "./foo.sh", even though the REAL script is named myscript.sh
When the command was run, $0 (or argv[0]) is set to the full command
that was used to start the script.
You can also use $1...$9 (or $*) to represent any command line options:
$ echo 'echo argument1: $1' >> myscript.sh
$ echo 'echo arguments: $*' >> myscript.sh
$ ./foo.sh one two three four
This will print:
./foo.sh
argument1: one
arguments: one two three four
If you have more than 9 arguments, you must use the "shift" shell
builtin to shift the arguments to the left by one (the first one in the
list will "drop off"):
$ echo 'shift ; echo arguments -1: $*' >> myscript.sh
$ ./foo.sh one two three four
Which will print
./foo.sh
argument1: one
arguments: one two three four
arguments -1: two three four
That's a short mini-howto on shell argument processing. Enjoy.
- Ian C. Blenke <icblenke@nks.net> <ian@blenke.com>
http://ian.blenke.com
This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 17:26:16 EDT