Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3

    [SOLVED] Variable Corruption

    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.

    Code:
    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.

  2. #2
    Just Joined! mehorter's Avatar
    Join Date
    May 2006
    Posts
    20
    Ther e must be a good reason but whynot write:
    NewFile=/var/ftp/pub/faxes/${FileName}.pdf

    I myself tried to use the $PDF variable and ${PDF} worked for me but so did using just plain .pdf at the end of the line.

    I will watch this thread and stew on a bettter answer for you.

  3. #3
    Just Joined! mehorter's Avatar
    Join Date
    May 2006
    Posts
    20
    A few thoughts:
    If $filename=myfile.txt.
    Code:
    ${filename/txt/pdf}
    would return "myfile.pdf"
    Code:
    ${filename}.pdf
    would return "myfile.txt.pdf"

    A better way still would be to use
    Code:
    ${filename%.txt}.pdf
    .
    The "%" strips off the ".txt' from the end of filename only.

    so if
    FileName=mytxtfile.txt
    using "% " would give you a correct result but $
    Code:
    {filename/txt/pdf}
    would yield "mypdffile.txt".

    Another thought:
    if $oFileName returns: "/var/ftp/pub/faxes/myfile.txt" why not do
    Code:
     NewFile=${oFileName%.txt}.pdf

  4. #4
    Just Joined!
    Join Date
    Nov 2009
    Posts
    3
    thanks for the replies everybody:

    FileName1=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`
    what I didnt realize was that the file in $f contained a char(10) character at the end of the string which I did not see at first, I ran this script with -x and realized that %f was returning with a \r at the the end.

    I changed my script to:
    Code:
    FileName1=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//' | tr -d'\r'`
    All good now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...