Not able to assign a value to variable

Hi Experts,

I am facing some problem while developing the script.My input config.csv file contains the three columns namely pathname,filename,filetype.Based on the file type i have to use ftp command that is if filetype=csv then do ftp.
The input file is

cat config.csv
pathname,filename,filetype
/home/fir/dir,filename1,csv
/home/fir1/dir,filename2,csv
/home/fir4/dir,filename3,csv
/home/fir/dir6,filename4,dat

To achieve this i have developed the following script:

#!/bin/ksh
head -5 config.csv | while read line
do
Type1=`echo $line | awk -F"," '{print $3}'`
Type=`echo $Type1 | tr [A-Z] [a-z]`
if [ $Type == 'csv' ]
 then
   DataFileName=`echo $line | awk -F"," '{print $2}'`
   DataPath=`echo $line| awk -F"," '{print $1}'`
   echo $DataPath
   slash=`echo '\'`
   filelocation=`echo $DataPath$slash$DataFileName`
   echo do sftp -n
fi
done

Now the problem is that the value of DataPath variable is coming as

echo $DataPath
homefirdir

Some how the slashes are getting removed.:confused:
How can we achieve the variable value as '/home/fir/dir ' and pass it to ftp command?

Thanks in Advance!

try qouting the $line in all lines

DataPath=`echo "$line"| awk -F"," '{print $1}'`

For me your code works just fine...:slight_smile:
try the solution given above, if you still have a problem debug using set -x.

Sorry, actually the input file data is :o :o

$ cat config.csv
pathname,filename,filetype
\home\fir\dir,filename1,csv
\home\fir1\dir,filename2,csv
\home\fir4\dir,filename3,csv
\home\fir\dir6,filename4,dat

Actually i tried to send this pathname to file and again cat the file,but this command is also not working... :frowning:

.....
echo $line > 45.txt
.....
DataPath=`cat 45.txt | awk -F"," '{print $1}'

did you try qouting the line variable?

"$line"

Thanks! for your reply..
Yes! I have tried the above mentioned command,but still it is not working :frowning:

hmm.. can you just use backlash? forward slashes complicate things

Nice thought :slight_smile:
but actually the external vendor puts this file into our server,we cannot make any changes to this file.. :rolleyes:
Actually I am thinking if i can create the copy of the source file(config.csv) and then use sed command(replacing the slashes) ...but then will sftp command will work properly?? :confused:
I am not sure if this approach is proper or not :eek:
Anyone have better idea ... :slight_smile:

hehe, just replace the backslashes for the meantime

-bash-3.2$ sed -e 's|\\|/|g' config.csv
pathname,filename,filetype
/home/fir/dir,filename1,csv
/home/fir1/dir,filename2,csv
/home/fir4/dir,filename3,csv
/home/fir/dir6,filename4,dat
-bash-3.2$

DataPath=` .... `
or write it
DataPath=$( .... ) - easier to see and read (and maybe understand = take value of subprocess).

i try that one also, it makes $line value to "homefirdir,filename1,csv"

Double the \ character from the input file and use the print -R command instead of echo :

$ cat config.csv
pathname,filename,filetype
\home\fir\dir,filename1,csv
\home\fir1\dir,filename2,csv
\home\fir4\dir,filename3,csv
\home\fir\dir6,filename4,dat
$ cat amey.ksh
#!/bin/ksh
head -5 config.csv | sed 's+\\+&&+g' | while read line
do
Type1=`echo $line | awk -F"," '{print $3}'`
Type=`echo $Type1 | tr [A-Z] [a-z]`
if [ $Type = 'csv' ]
 then
   DataFileName=`echo $line | awk -F"," '{print $2}'`
   DataPath=`print -R "$line" | awk -F"," '{print $1}'`
   print -R $DataPath
   slash=`echo '\'`
   filelocation=`echo $DataPath$slash$DataFileName`
   echo do sftp -n
fi
done
$ amey.ksh
\home\fir\dir
do sftp -n
\home\fir1\dir
do sftp -n
\home\fir4\dir
do sftp -n
$ 

Your script can be rewritten in the following form :

typeset -l Type
head -5 config.csv | sed 's+\\+&&+g' |
while IFS=, read DataPath DataFileName Type
do
if [ "$Type" = 'csv' ]
 then
   FileLocation="$DataPath\\$DataFileName"
   print -R  $DataPath
   print do sftp -n
fi
done

jean-Pierre.

#!/bin/ksh
oifs="$IFS"
cat config.csv | while read -r line
do
        IFS=","
        fields=($line)
        IFS="$oifs"
        Type=${fields[2]}
        Filename=${fields[1]}
        Path=${fields[0]}
        [ "$Type" != "csv" ] && continue
        print -R "$Path - $Filename"
done

Thanks All!!
:wink:
Both the scripts work really very well... :smiley:

Again Many Thanks!!! :smiley: :smiley: