[comp.lang.c] UNIX command line arguments

chin@ankh.ftl.fl.us (Albert Chin) (06/07/90)

First off, I thank those of you who responded to my question regarding
"char **x". I haven't had time to look at the responses yet but am greatly
appreciated. Now another question.

Under UNIX, you can specify multiple filenames with similar characteristics
with the "*" argument and also the "[..]". For instance, to remove all
files of the type "file.1, file.2, file.3" you would type "rm file.*".
Also, you could "mv file.* myfile.*". I woule like to know how the argv
arguments get interpreted when this happens. I can understand that under
"rm file.*" all files matching that parameter would be translated on the
command line as if "rm file.1 file.2 ..." had been typed. But what about
the case of "mv file.* myfile.*". Would **argv then contain "mv file.1
myfile.1 file.2 myfile.2 file.3 myfile.3". If so, then I understand, but
if now, then how else does it work.

albert chin ... mthvax!mamia!albert

cpcahil@virtech.uucp (Conor P. Cahill) (06/08/90)

In article <352@ankh.ftl.fl.us> chin@ankh.ftl.fl.us (Albert Chin) writes:
>Under UNIX, you can specify multiple filenames with similar characteristics
>with the "*" argument and also the "[..]". For instance, to remove all
>files of the type "file.1, file.2, file.3" you would type "rm file.*".
>Also, you could "mv file.* myfile.*". I woule like to know how the argv

First off, you can't do "mv file.* myfile.*" if you think this will move
all of the "file.*" files to "myfile.*" files.  mv only as two modes of
operation: mv file.orig file.new or mv filelist directory.

The expansion of the shell filename wilcards is performed by the shell
itself as the command line is processed by the shell before the indicated
program is executed.  This processing used to be performed by a "glob" 
function (hence the message "glob [confirm]" from the PWB and version 6
rm command when a wildcard was specified).

>arguments get interpreted when this happens. I can understand that under
>"rm file.*" all files matching that parameter would be translated on the
>command line as if "rm file.1 file.2 ..." had been typed. But what about
>the case of "mv file.* myfile.*". Would **argv then contain "mv file.1
>myfile.1 file.2 myfile.2 file.3 myfile.3". If so, then I understand, but
>if now, then how else does it work.

The interpretation is always in place in sorted order.  So if your 
directory contained the files:

	file.1
	file.2
	file.3
	file.4
	myfile.8
	myfile.9
	myfile.10

and you executed "echo file.* myfile.*"
the argv array for echo would look like:

	argv[1] = "file.1"
	argv[2] = "file.1"
	argv[3] = "file.1"
	argv[4] = "file.1"
	argv[5] = "myfile.10"
	argv[6] = "myfile.8"
	argv[7] = "myfile.9"

Note that I start with subscript 1, since argv[0] would be "echo".


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

dankg@tornado.Berkeley.EDU (Dan KoGai) (06/08/90)

In article <352@ankh.ftl.fl.us> chin@ankh.ftl.fl.us (Albert Chin) writes:

>Under UNIX, you can specify multiple filenames with similar characteristics
>with the "*" argument and also the "[..]". For instance, to remove all
>files of the type "file.1, file.2, file.3" you would type "rm file.*".
>Also, you could "mv file.* myfile.*". I woule like to know how the argv
>arguments get interpreted when this happens. I can understand that under
>"rm file.*" all files matching that parameter would be translated on the
>command line as if "rm file.1 file.2 ..." had been typed. But what about
>the case of "mv file.* myfile.*". Would **argv then contain "mv file.1
>myfile.1 file.2 myfile.2 file.3 myfile.3". If so, then I understand, but
>if now, then how else does it work.

	Simple:  It's shell's job to expand * and other character (called
globbing).  On csh you can turn off this feature by "set noglob" and
characters such as '*' are treated leterally (this is necessary when using
tset, et al).
	So your argv will receive literal names of filenames, not a single
'*'.  And this also apply for directories and variable substitution:
if the shell (here I use csh) recieves "~/foo" and your $HOME is
/usr/account/chin, your argv will receive /usr/account/chin/foo.
	I'm not sure if it applies to other CLI OS but I think so.
(DOS, OS/2, OS-9, you name it).  The following code is a simple program
to print what exactly your argv got.

/* start code */

main(argc, argv)
int	argc;
char	**argv;
{
	int i;
	printf("argc == %d\n", argc);
	for(i=0; i < argc; i++){
		printf("argv[%d] == %s\n", i, argv[i])
	}
}

/* end code */

----------------
____  __  __    + Dan The "Pnews -h~/.rnhead < .article" Man
    ||__||__|   + E-mail:	dankg@ocf.berkeley.edu
____| ______ 	+ Voice:	+1 415-549-6111
|     |__|__|	+ USnail:	1730 Laloma Berkeley, CA 94709 U.S.A
|___  |__|__|	+	
    |____|____	+ "What's the biggest U.S. export to Japan?" 	
  \_|    |      + "Bullshit.  It makes the best fertilizer for their rice"

martin@mwtech.UUCP (Martin Weitzel) (06/09/90)

In article <1990Jun8.023449.4273@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
[about wild card expansion by the shell]
>
>The interpretation is always in place in sorted order.

Very true. But be aware of the upcomming NLS (Native Language Support)
stuff. Suddenly some other collating sequence may apply. (Have you
ever looked hard for some file that didn't exist though you just
created it - may be "ls" displayed it where you didn't expect :-).)
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

emm@iczer-1.UUCP (Edward M. Markowski) (06/09/90)

In article <352@ankh.ftl.fl.us> chin@ankh.ftl.fl.us (Albert Chin) writes:
>
>the case of "mv file.* myfile.*". Would **argv then contain "mv file.1
>myfile.1 file.2 myfile.2 file.3 myfile.3". If so, then I understand, but
>if now, then how else does it work.
>

You have it right except for one point. When the shell expands the * or []
or whatever else you put on the line it alphabitizes(sp?) them.

You have to rember that some commands need the parameters in a specific
order. mv will work with many parameters as long as the last one it the
name of a directory.


-- 
Edward M. Markowski -- iczer-1 Administrator

                              ...the garage is flooded from the sprinkler.
                              It also left a man's decapitated body,
VOICE : (201) 478-6052        lying on the floor next to his own severed head.
UUCP : ..!uunet!iczer-1!emm   A head which at this time has no name.

dmt@PacBell.COM (Dave Turner) (06/12/90)

In article <319@iczer-1.UUCP> emm@iczer-1.UUCP (Edward M. Markowski) writes:
:In article <352@ankh.ftl.fl.us> chin@ankh.ftl.fl.us (Albert Chin) writes:
:>the case of "mv file.* myfile.*". Would **argv then contain "mv file.1
:>myfile.1 file.2 myfile.2 file.3 myfile.3". If so, then I understand, but
:>if now, then how else does it work.
:You have it right except for one point. When the shell expands the * or []
:or whatever else you put on the line it alphabitizes(sp?) them.
:You have to rember that some commands need the parameters in a specific
:order. mv will work with many parameters as long as the last one it the
:name of a directory.

The bourne and korn shells sort each expansion individually which retains
the position of those arguments which defy expansion.

The man(1) page for [b]sh(1) in my System V R2.1 manual states about
File Name Generation:

.... The *word* is replaced with alphabetically sorted file names that
match the pattern. If no file name is found that matches the pattern,
the word is left unchanged.

The command:

	echo [e-j]* . [a-d]* .

produced the expected, desired results: all file names beginning with
letters e through j, a dot, all file names beginning with letters a
through d, and a final dot.


-- 
Dave Turner	415/823-2001	{att,bellcore,sun,ames,decwrl}!pacbell!dmt