tags:

views:

58

answers:

1

Given the following code I am expecting the variable $NewFile to be /var/ftp/pub/faxes/myfile.txt.pdf
However my system is returning: ".pdf/ftp/pub/faxes/myfile.txt"

$Ext returns: "txt"
$oFileName returns: "/var/ftp/pub/faxes/myfile.txt"

I have tried this a hundred different ways with no luck.

PDF=".pdf"

#extract the file.ext from the script
FileName1=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName2=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName3=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`

FileName=`basename $FileName1`
Ext=`echo $FileName | awk -F . '{print $NF}'`

FileName=`basename $FileName2`
oFileName=/var/ftp/pub/faxes/${FileName}

FileName=`basename $FileName3`
NewFile=/var/ftp/pub/faxes/${FileName}${PDF}

echo $oFileName
echo $NewFile
echo $Ext

Any help is appreciated.

+4  A: 

Comment: if you ever move to a normal Unix-like system instead of Linux, you won't get away with 'head $f -n1'; you must put option (control) arguments before file names, so use 'head -n1 $f'. (If you set environment variable POSIXLY_CORRECT, Linux will behave the same as other systems.)

Comment: you could perfectly well write:

FileName1=`head -n1 $f | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName2=$Filename1
FileName3=$Filename1

It makes more sense than running the same three programs on the same data three times.

Comment: you should learn and use the '$(...)' notation:

FileName1=$(head -n1 $f | awk -F/ '{print $NF}' | sed 's/\"//')

When run on MacOS X, with f=xxxx.input and the file xxxx.input containing one line that says '/some/where/myfile.txt', the script produces:

/var/ftp/pub/faxes/myfile.txt
/var/ftp/pub/faxes/myfile.txt.pdf
txt

The trace output ('bash -x') is:

+ f=xxxx.input
+ PDF=.pdf
++ head -n 1 xxxx.input
++ sed 's/\"//'
++ awk -F/ '{print $NF}'
+ FileName1=myfile.txt
++ head -n 1 xxxx.input
++ awk -F/ '{print $NF}'
++ sed 's/\"//'
+ FileName2=myfile.txt
++ awk -F/ '{print $NF}'
++ head -n 1 xxxx.input
++ sed 's/\"//'
+ FileName3=myfile.txt
++ basename myfile.txt
+ FileName=myfile.txt
++ echo myfile.txt
++ awk -F . '{print $NF}'
+ Ext=txt
++ basename myfile.txt
+ FileName=myfile.txt
+ oFileName=/var/ftp/pub/faxes/myfile.txt
++ basename myfile.txt
+ FileName=myfile.txt
+ NewFile=/var/ftp/pub/faxes/myfile.txt.pdf
+ echo /var/ftp/pub/faxes/myfile.txt
/var/ftp/pub/faxes/myfile.txt
+ echo /var/ftp/pub/faxes/myfile.txt.pdf
/var/ftp/pub/faxes/myfile.txt.pdf
+ echo txt
txt

You will need to show what you're really using, and 'bash -x' may well help you see where your problem is -- I cannot reproduce it.

Jonathan Leffler
Jonathan, thanks for the reply. what other info can I get you to help out here?
ec
+ PDF=.pdf+ FileName1=$'faxtest.txt\r'+ FileName2=$'faxtest.txt\r'+ FileName3=$'faxtest.txt\r'++ basename $'faxtest.txt\r'+ FileName=$'faxtest.txt\r'++ echo $'faxtest.txt\r'++ awk -F . '{print $NF}'+ Ext=$'txt\r'++ basename $'faxtest.txt\r'+ FileName=$'faxtest.txt\r'+ oFileName=$'/var/ftp/pub/faxes/faxtest.txt\r'++ basename $'faxtest.txt\r'+ FileName=$'faxtest.txt\r'+ NewFile=$'/var/ftp/pub/faxes/faxtest.txt\r.pdf'+ echo $'/var/ftp/pub/faxes/faxtest.txt\r'/var/ftp/pub/faxes/faxtest.txt+ echo $'/var/ftp/pub/faxes/faxtest.txt\r.pdf'.pdf/ftp/pub/faxes/faxtest.txt
ec
I think I see my problem, the variable $FileName is being populated with a \r character.
ec
If you imported the file from Windows (most plausible source of carriage returns at the ends of lines), then you should convert to Unix LF only line endings before processing further. One way to do that is via the dos2unix or dtou commands; another way uses vim to edit the file, use ':set fileformat=unix' to change from DOS to Unix formats, and save the file again.
Jonathan Leffler
And you see the '.pdf' at the start because the embedded carriage return moves the cursor back to the beginning of the line after echoing the name, and then emits the '.pdf' over the beginning.
Jonathan Leffler