Re: shell script question:How 2 assign several fields from a file to a variable?

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

 



I'm not sure you have the right logic in your script.  As I understand
it, you want to take each *.doc file and rename it to a filename from
names.txt.  For example; you have a.doc, b.doc, and c.doc. The way your
script is written, a.doc will be renamed (moved) to "Korean car", then
b.doc will be renamed to "Korean.car", and so will c.doc.  Your script
has two 'for' loops. The outer loop runs thru each file of *.doc, and
the inner loop cycles thru all of the entries in names.txt for EACH
*.doc file.  So, for each *.doc file (outer loop) it will completely run
thru the inner loop effective renaming each file to every file name you
have in names.txt eventually ending up with "Korean car" as the last
rename before it moves to the next *.doc file.  The effect of this will
be that every *.doc file will be renamed to "Korean car and the only
file you will have left in the directory will be the last *.doc file and
it will be called "Korean car".  Hmmm.

Anyways, to answer your question...  you need to use double quotes
around your input and output variables and commands.  Here is and example:

for i in "`cat names.txt`"; do
    printf "$i\n"
done

You could also use the 'echo' command instead or 'printf' but 'echo' is
outdated. Use printf.

HTH,
Rick

vick Julius wrote:

> Hello everybody
>
> I want to rename files with the new names from a text file, names.txt:
>
> Brazilian coffee
> Canadian maple
> Korean car
>
>
>
> i want to use the following script
>
> for i in *.doc
> do
>
>    for file in `cat names.txt`
>   do
>    mv  $i   $file....
>
>
>
> I want to renmae all files (in *.doc) with the new names read from
> names.txt file.
>
> The problem is the names in the file names.txt have spaces...
>
>
> Any idea?
>
> Thanks
>
> Vick
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
> -
> : send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-
: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Newbie]     [Audio]     [Hams]     [Kernel Newbies]     [Util Linux NG]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Device Drivers]     [Samba]     [Video 4 Linux]     [Git]     [Fedora Users]

  Powered by Linux