Operating System - HP-UX
1829585 Members
1762 Online
109992 Solutions
New Discussion

syntax for varaiable with spaces

 
SOLVED
Go to solution
Michael Allmer
Frequent Advisor

syntax for varaiable with spaces

I have found many threads that talk about spaces in file name and directories. I though that I was following the information in them but I am sptill having issues, and I must have sothing wrong in my syntax.
Script is as follows;
#!/usr/bin/sh
#
COPY_CMD=`rsync -avz`
#
LIST=`cat /home/allmerms/office_move/raid_mdb.txt`
#
DEST="/office"
#
#
for l in $LIST
do
$COPY_CMD "${l}" $DEST
done
17 REPLIES 17
James R. Ferguson
Acclaimed Contributor
Solution

Re: syntax for varaiable with spaces

Hi Mike:

# COPY_CMD=`rsync -avz`

...should be:

# COPY_CMD="rsync -avz"

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

Here is the example of the error that I am seeing;
+ /raid/documents/actuation/eng_pdu/Tony /office
./test_copy.sh[13]: /raid/documents/actuation/eng_pdu/Tony: not found.
+ Li/saved/MoveNewDesktop/work/PNcrossReference.mdb /office
./test_copy.sh[13]: Li/saved/MoveNewDesktop/work/PNcrossReference.mdb: not found.



The full path is;
"/raid/documents/actuation/eng_pdu/Tony Li/saved/work/PNcrossReference.mdb"
Steven Schweda
Honored Contributor

Re: syntax for varaiable with spaces

> [...] but I am sptill having issues [...]

Are those anything like _problems_? And do
we need to guess what they were, or might you
be persuaded to tell us?

> LIST=`cat /home/allmerms/office_move/raid_mdb.txt`

You have a file named "cat"?
James R. Ferguson
Acclaimed Contributor

Re: syntax for varaiable with spaces

Hi (again) Mike:

By the way, the use of backticks is deprecated in favor of '$()' as:

# LIST=`cat /home/allmerms/office_move/raid_mdb.txt`

...is better written:

# LIST=$(cat /home/allmerms/office_move/raid_mdb.txt)

...which, is more _efficiently_ done as:

# LIST=$(< /home/allmerms/office_move/raid_mdb.txt)

...which causes the shell to perform the read of the file without forking a separate process (here, 'cat').

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

Thanks James, you help was what I needed.
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

After changing script to follwo James' suggestions:
#!/usr/bin/sh
#
COPY_CMD="rsync -avz"
#
LIST=$(#
DEST="/office"
#
#
for l in $LIST
do
$COPY_CMD "${l}" $DEST
done

I am still getting the following error form the output:

sent 29 bytes received 20 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be transferred (code 23) at main.c(892) [sender=2.6.8]
+ rsync -avz /raid/documents/actuation/eng_pdu/Tony /office
building file list ... rsync: link_stat "/raid/documents/actuation/eng_pdu/Tony" failed: No such file or directory (2)
done

sent 29 bytes received 20 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be transferred (code 23) at main.c(892) [sender=2.6.8]
+ rsync -avz Li/saved/move/Program /office
building file list ... rsync: link_stat "/home/allmerms/office_move/Li/saved/move/Program" failed: No such file or directory (2)
done


In additions some of the file that where succesful are not what I expected. I wanted the full path created after /office
Exapmle:
"/raid/unigraphics/utils/var/tables/Teamcenter-DB.mdb"
Was created in /office as; /office/Teamcenter-DB.mdb

I was expecting;
"/office/raid/unigraphics/utils/var/tables/Teamcenter-DB.mdb"

I hope that I have explained my expectation correctly.
James R. Ferguson
Acclaimed Contributor

Re: syntax for varaiable with spaces

Hi (again) Mike:

A failed stat() means that the file or directory isn't found (or readable). See if this helps you:

...
for l in ${LIST}
do
[ -f "${l}" ] || { echo "missing file: ${l}"; continue; }
DIR=$(dirname ${l})
echo ${COPY_CMD} "${l}" "${DEST}${DIR}"
done

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

James,

Here is a log of the output from you latest suggested change.
James R. Ferguson
Acclaimed Contributor

Re: syntax for varaiable with spaces

Hi Mike:

> Here is a log of the output from you latest suggested change.

Without seeing what the input file for that run looks like, it is difficult to comment.

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

Sorry, here it is
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

here is the "LIST" file as well.
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

I was running against an old list. Sorry for the confusion. here is the updated list as well as the attached output.

/raid/cad/777-lowerlobe/200-lr/docs/All_Side_Guide_BOMs.mdb
/raid/unigraphics/utils/var/tables/Teamcenter-DB.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/MoveNewDesktop/work/PNcrossReference.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/move/Program Files/Visual Sizer/VisualSizer.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/move/Program Files/Visual Sizer/appdata.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/move/Program Files/Visual Sizer/datatemp.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/move/Program Files/speed/spud/tuts/mag10.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/work/PNcrossReference.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/workold/PNcrossReferenceCustomerPN-1.mdb
/raid/documents/actuation/eng_pdu/Tony_Li/saved/workold/PNcrossReferenceCustomerPN.mdb
James R. Ferguson
Acclaimed Contributor

Re: syntax for varaiable with spaces

Hi (again) Mike:

Oh, you have _spaces_ in your filenames. I should have guessed that from your original post. Seeing actual input avoids ambiguous communication.

#!/usr/bin/sh
COPY_CMD="rsync -avz"
LIST="/home/allmerms/office_move/raid_mdb.txt"
DEST="/office"
while read FILE
do
[ -f "${FILE}" ] || { echo "missing file: '${FILE}'"; continue; }
DIR=$(dirname "${FILE}")
echo ${COPY_CMD} "'${FILE}'" "'${DEST}${DIR}'"
done < ${LIST}
exit 0

This also avoids forking a process to construct a list in memory. Instead we let the shell read each line of the file for us.

I would suggest that you read the 'rsync' manpages to make sure that the 'rsync' syntax gives you what you want.

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

The last par of the directory path is being removed.

using the last iput file I provided, I ran the following.

#!/usr/bin/sh
#
LIST="/home/allmerms/office_move/raid_mdb.txt"
#
#
while read FILE
do
[ -f "${FILE}" ] || { echo "missing file: ${FILE}"; continue; }
DIR=$(dirname ${FILE})
echo "${DIR}"
done < ${LIST}
exit 0

The output file show that it is dropping everything in the path name after the first space.
James R. Ferguson
Acclaimed Contributor

Re: syntax for varaiable with spaces

Hi (again) Mike:

> The output file show that it is dropping everything in the path name after the first space.

Yes, you dropped quotes. Compare:

...
while read FILE
do
#-> [ -f "${FILE}" ] || { echo "missing file: ${FILE}"; continue; }
DIR=$(dirname ${FILE})
echo "<<< ${DIR}"
DIR=$(dirname "${FILE}")
echo ">>> ${DIR}"
done < ${LIST}

Regards!

...JRF...
Michael Allmer
Frequent Advisor

Re: syntax for varaiable with spaces

Thanks for all the assistance.
Fredrik.eriksson
Valued Contributor

Re: syntax for varaiable with spaces

This is closed and all... but why not just use the IFS?

#!/bin/nash
COPY_CMD="rsync -avz"
LIST=$(cat /home/allmerms/office_move/raid_mdb.txt)
DEST="/office"

IFS="
"
for i in $LIST; do
$COPY_CMD "${i}" $DEST
done

Best regards
Fredrik Eriksson