Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > [SOLVED] Variable Corruption

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 2 Weeks Ago   #1 (permalink)
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.
eccapt is offline  


Old 1 Week Ago   #2 (permalink)
Just Joined!
 
mehorter's Avatar
 
Join Date: May 2006
Posts: 12
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.
mehorter is offline  
Old 1 Week Ago   #3 (permalink)
Just Joined!
 
mehorter's Avatar
 
Join Date: May 2006
Posts: 12
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
mehorter is offline  
Old 1 Week Ago   #4 (permalink)
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.
eccapt is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:06 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2