If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Shell scripting when verable contains a space

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-03, 07:08
EZEE! EZEE! is offline
Registered User
 
Join Date: Oct 2003
Location: East Coast of South Africa
Posts: 10
Question Shell scripting when verable contains a space

Scenario: I have a file with each line containg filenames. i.e. '{dirname}/Oct Reports'
Problem: The shell reads each line as two verables due to the space.
Script:
#!/bin/sh
for i in `cat file`
do
echo $i
done
Output:
'{dirname}/Oct
Reports'
Tried enclosing filename in single quotes, double quotes, backslash before space, two backslash before space.
please advise if you know where I am going wrong..

Surfon

Last edited by EZEE!; 10-09-03 at 08:19.
Reply With Quote
  #2 (permalink)  
Old 10-09-03, 07:23
EZEE! EZEE! is offline
Registered User
 
Join Date: Oct 2003
Location: East Coast of South Africa
Posts: 10
Lightbulb

Answered my own question by searching this site further and applying a similar task to this one.
Code that worked for me eventually:

cat outfile | while read l
do
echi $l
done

Any other ideas welcome though.
Reply With Quote
  #3 (permalink)  
Old 10-09-03, 09:01
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
You can overcome the space issue by setting IFS to a value other than whitespace.

e.g.

IFS=#; for i in $(cat outfile); do...

Also, in your example, you needn't use 'cat'.

while read l
do
echi $l
done < outfile

HTH
Reply With Quote
  #4 (permalink)  
Old 10-09-03, 12:24
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
@ all in this posting

Thanks for the solution, I have had the same problem last week.
But I donīt find the right words for asking here.

Greetings from Germany
Peter F.
Reply With Quote
  #5 (permalink)  
Old 10-10-03, 07:25
EZEE! EZEE! is offline
Registered User
 
Join Date: Oct 2003
Location: East Coast of South Africa
Posts: 10
Damian Ibbotson, thanks, that is why you are a guru.
For us wannabies..Can you expand a litlle on what IFS=# is declaring...

Muchos Grazias
Reply With Quote
  #6 (permalink)  
Old 10-10-03, 08:47
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by EZEE!
Damian Ibbotson, thanks, that is why you are a guru.
For us wannabies..Can you expand a litlle on what IFS=# is declaring...

Muchos Grazias
IFS (Internal Field Seperator) is an environment variable which is normally a space, a tab and a newline. The shell uses individual characters found in the IFS variable to separate fields (surprisingly enough!). IFS=# is simply setting the value of the variable to a # character.

You can see this in action if you try the following:

IFS=#; set Hello World; echo "$*"

If you only wanted to alter IFS for the cat command, you would do...

for i in $(IFS=# cat outfile); do

PS - I'm really not a 'guru'. I think you can interpret that as meaning 'prolific poster' and not much else!

Last edited by Damian Ibbotson; 10-11-03 at 05:40.
Reply With Quote
  #7 (permalink)  
Old 10-15-03, 07:20
EZEE! EZEE! is offline
Registered User
 
Join Date: Oct 2003
Location: East Coast of South Africa
Posts: 10
Further problems - feedback

Hi, I thought I let you know of further hassles I had.
The echo works fine but if I try to tar copy the file to another volume I have endless problems. and more errors when deleting origional. Here is what didnt work... I tried a combo of them.
#for i in $(IFS=# cat $out)
#IFS=#; for i in $(cat $out)
#IFS=#; for i in $( -name "*.xls" -printf "\"%p\" ");
#for i in $(IFS=# find /usr4/data -mtime +2457 -name "*.xls" -printf "%p ");
#(cd / && tar cf - $(IFS=# find /usr4/data -mtime +2457 -name "*.xls" -printf "\"%p\" "))|(cd $todir; tar xfBp -)
#do
# echo $i
#get dirname
# copy data with tar
#(tar cf - "$i")| (cd $todir; tar xfBp -)
#(IFS=# && tar cf - "$i")| (cd $todir; tar xfBp -)
#remove origional
#IFS=#; rm "$i"
#done

BUT THIS IS WHAT WORKED IN THE END....

#!/bin/bash
todir='/usr4/history/working'
# list of files used with `find /usr4/data -mtime +2457 `
out= "outdile1997"
while read bla
do
echo $bla
(tar cf - "$bla")| (cd $todir; tar xfBp -)
#IFS=# ;rm $bla
done < $out

It Simply amases me how verious loops work in shells and I take my hat off to anyone who truly understands them. for, foreach, while, case ....and,and,and...

I must say a thankyou to Damian Ibbotson. Cheers

Stephan
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On