[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Doubt on shellscripts



On 12:42 08 Dec 2003, B, Prasada Rao (Cognizant) <BPrasada chn cognizant com> wrote:
| 1.  In any scripting language (bash, ksh, ..etc)   # indicates the comment right. But in the first line for any script we need to start some thing like #!/bin/bash   or 
| #!/bin/ksh.
| Here I think # is not a comment. But how the system will identify The # in the first line is not a comment and in other lines is a comment?

Yes and no.

As far as the shell is concerned, it _is_ a comment. In fact, that's
why a # is used there.

What's actually happening is as follows:

	When you issue a command, the shell locates an executable file
	from your $PATH with that name to run.

	It forks and then calls the exec() system call to execute the file.

	If the file is a valid executable the OS will load it and run it.

	For binaries this means mapping the file into memory and running it.

	For non-binaries that start with the two bytes "#" and then "!" the
	OS reads the remainder of that line as a program with which to
	interpret the file, and issues that program, passing the file as the first
	argument.

So if you run a command:

	foo a b c

and foo is a "#!/bin/sh" script in, say, /bin, then the kernel will run the
command:

	/bin/sh /bin/foo a b c

This invokes the shell (/bin/sh) and the shell then opens the file "/bin/foo"
to look for stuff to do.

In this way you may specify an arbitrary interpreter to execute a script.
Example:

	http://www.cskk.ezoshosting.com/cs/scripts/unhdr

That's a sed script instead of a shell script. The same mechanism is used.

| 2. What is $? is

The exit status from the last command. 0 means success, non-zero means failure.

| 3. What $1 returns ? Will it return the second variable value? 

The first command line argument. For the example above it would be "a".

Cheers,
-- 
Cameron Simpson <cs zip com au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Academics get paid for being clever, not for being right. - Donald Norman




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]