1752781 Members
6587 Online
108789 Solutions
New Discussion юеВ

Re: script

 
nick massey
New Member

script

hi all, can some one please tell me what this is trying to do? I understand its looking for some thing inside the brackets but what specifically

for i 'cat /foo/users.list' : do
if [[ ! -a $i ]] ; then
mkdir $i
done

what is the "-a" doing too please
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: script

The double brackets "[[ ]]" use the shell's (POSIX and Korn) internal expression evaluation and differ somewhat from the single brackets "[ ]" conditinal which invokes the external test command to evaluate the expression.

To the internal evaluator "-a" says does a file exist? Thus, [[ ! -a ${i} ]] says if file ${i} does not exist then mkdir ${i}(and a file in UNIX is a regular file, a directory, a device node, ...).

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: script

Oh, I should also note (to add to the confusion) that in single-bracket speak, "-a" is a logical AND but in double-bracket speak "&&" is the logical AND. Similarly "-o" (logical OR) becomes "||".

The double-bracket is the the preferred approach these days because it is more efficient in that no external process need be fork()'ed and exec()'ed to do the expression evaluation.
If it ain't broke, I can fix that.
nick massey
New Member

Re: script

thanks very much for the speedy response, I get it..........I think LOL. I guess I need to RTFM more and do some testing. thanks very much :)

nick massey
New Member

Re: script

ok, another question please why does it need a "!"?

it says in the manual that the "!". "inverts the exit status of the command to which it is applied and it also inverts the meaning of the test operator"

I am a bit confused. you can tell me to go back and read more if you like LOL LOL
A. Clay Stephenson
Acclaimed Contributor

Re: script

Consider that /foo/users.list has the entries "Mickey","Minnie", and "Pluto".

Also consider that in the CWD that directory (or file) Minnie already exists.

The directory is only created if no such entry is found so the loop would create directories Mickey and Pluto but would not create Minnie because that directory already existed.

In that sense the correct approach is "if file (directory) does not exist then make it".


I should point out that there is actually a syntax error because you are missing an "fi".

for i 'cat /foo/users.list' : do
if [[ ! -a $i ]] ; then
mkdir $i
done

should be:
for i in 'cat /foo/users.list'
do
if [[ ! -a ${i} ]]
then
mkdir ${i}
fi
done

but because filenames can contain whitespace which would drive your for statement nuts, a better approach is something like this:

typeset i=""
cat /foo/users.list | while read i
do
if [[ ! -a "${i}" ]]
then
mkdir "${i}"
fi
done

and the cat is superfluous as well since you could simply redirect stdin on the read itself but I left the cat in because it is closer to your original. Notice the quotes around "${i}" -- this means that directory "Mickey Mouse" is handled as one filename -- as it should be. Whitespace in UNIX filenames is perfectly legal although many scripts and programs will incorrectly handle it.




If it ain't broke, I can fix that.
nick massey
New Member

Re: script

aaaaaaaaaahhhhhhhh I see, this is very cool.

I am starting to see the light, cool man.

thanks very much.

wicked

cheers