OT: shell scripting problem

Alexander Dalloz ad+lists at uni-x.org
Wed May 18 01:30:51 UTC 2005


Am Mi, den 18.05.2005 schrieb linux.whiz at gmail.com um 0:55:

> My brain is fried.  I know there is a simple answer to this but I'm
> drawing a blank.  I want to run a script against the contents of a
> text file.  The text file is just my users' first name, middle initial
> and last name like this:
> 
> John A Smith
> Mary P James
> Sally R Jones
> Fred Q Davis
> 
> What I want to do is for each user in this file, run a script.  I
> tried to do this:
> 
> for i in `cat textfile`; do
>   myscript.sh $i
> done
> 
> I expect this to run like this:
> 
> myscript.sh John A Smith
> myscript.sh Mary P James
> myscript.sh Sally R Jones
> myscript.sh Fred Q Davis

> LW

Does your "myscript.sh" expect 3 arguments or one single consisting of
the full person's name?

a) script runs i.e. "John A Smith" as 1 argument:
$ cat names.lst | while read line; do ./script.sh "${line}"; done
-->
1 argument:
john a smith
1 argument:
mary p james
1 argument:
sally r jones
1 argument:
fred q davis


b) script runs i.e. "John A Smith" as 3 arguments:
$ cat names.lst | while read line; do ./script.sh ${line}; done
-->
3 arguments:
JOHN A SMITH
3 arguments:
MARY P JAMES
3 arguments:
SALLY R JONES
3 arguments:
FRED Q DAVIS

The "script.sh" in my example is:

$ cat script.sh
#!/bin/bash
 
if [ "$2" != "" ]; then
  echo "3 arguments: "
  echo -n $1 | tr [:lower:] [:upper:]
  echo -n " "
  echo -n $2 | tr [:lower:] [:upper:]
  echo -n " "
  echo    $3 | tr [:lower:] [:upper:]
else
  echo "1 argument: "
  echo $1 | tr [:upper:] [:lower:]
fi

And names.lst contains:

$ cat names.lst
John A Smith
Mary P James
Sally R Jones
Fred Q Davis

Alexander Dalloz


-- 
Alexander Dalloz | Enger, Germany | GPG http://pgp.mit.edu 0xB366A773
legal statement: http://www.uni-x.org/legal.html
Fedora Core 2 GNU/Linux on Athlon with kernel 2.6.11-1.14_FC2smp 
Serendipity 03:21:36 up 4 days, 2:53, load average: 0.08, 0.11, 0.09 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://lists.fedoraproject.org/pipermail/users/attachments/20050518/04860380/attachment-0002.bin 


More information about the users mailing list