Solved

Problem with variable

Posted on 2000-02-16
10
162 Views
Last Modified: 2010-03-05
Knowing the following:

$tFile = "greeting"
$Particular = "invitation"
$FileToUse = "dat.csn"

Why then does the following line not work correctly?

$NewFilename = "cgi_user_dir/${tFile}${Particular}${FileToUse}";

The result makes NewFilename = "cgi_user_dir//invitation/dat.csn"

What is happening to the "tFile" variable??

Does any of this make sense to anybody?
0
Comment
Question by:microboard
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2529363
Do you have semicolons at the end of these:-

$tFile = "greeting"
$Particular = "invitation"
$FileToUse = "dat.csn"
Must have them.

The example does not seem to be correct - where do the slashes come from?

Perhaps you really did :-
$NewFilename = "cgi_user_dir/$tFile/$Particular/$FileToUse";

With the changes described, it should work.  It works for me

0
 
LVL 84

Expert Comment

by:ozo
ID: 2529710
perl -de 1

Enter h or `h h' for help.

main::(-e:1):   1
  DB<1> $tFile = "greeting"

  DB<2> $Particular = "invitation"

  DB<3> $FileToUse = "dat.csn"

  DB<4> x $NewFilename = "cgi_user_dir/${tFile}${Particular}${FileToUse}";
0  'cgi_user_dir/greetinginvitationdat.csn'
  DB<5>  
0
 

Author Comment

by:microboard
ID: 2530148
I can run the script on my own computer and it does fine but when running it on the web server I am having the trouble.

Thanks guys but I guess my method is ok; it must be something else messing things up.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2531469
can you provide the forum with the complete script?
0
 

Author Comment

by:microboard
ID: 2532527
What I'm trying to do is check whether a file already exists on my server and if so, then I simply make a new filename. What I don't understnad is that it was working fine then suddenly it's not working anymore.


##--------BEGIN SCRIPT-----------------

#just know that $upname is a filename that includes the directory: "directory/sub/file.csn"

#set filename
$filename = $upname;

#Here I am separating the directory name
#from the filename
$DirPosCheck = rindex($upname, "\/");
$DirCheck = substr($upname,0,                     $DirPosCheck);

#this gives me the filename alone
$FileCheck= substr($upname,$DirPosCheck + 1);

# check directory existence - this works fine
if (!(-e "${FULLPATH}cgi_user_dir/${DirCheck}"))        {
      print "Invalid Directory Names -\n";
      print "*${DirCheck}*\n";
      exit;
       }

# find free file name
#What I'm doing here is simply separating the
#known file extension from the filename - it works
$NewFilename = $FileCheck;
$NewFilename =~ s/.que.csn//i;
$extension = ".que.csn";
$nCount = 0;

#this is the trouble area
while (-e      "$/cgi_user_dir/${DirCheck}/${FileCheck}") {

     ++$nCount;
     $FileCheck =                      "${NewFilename}${nCount}${extension}";
   };             

For some reason the $FileCheck part of the string is not being registered by the script so that even if a file exists, it is not being found. Now, I can remove the $FileCheck part and then it returns true because it will register the $DirCheck value. But if I put the $FileCheck value back in, then it skips the entire statement as if it couldnt' find the file. The file I'm testing does exist so don't think I'm getting the value wrong. I can manually type in the filename and it works fine, but if I leave it up to the variable, then it doesn't seem to register. Is there any way this could be a server problem? Is there some known incident where a server's perl script executable was to blame? The think I don't understand is that the above script was working just fine. Now, it gives problems.

Perhaps someone could simply show me how you would check for the existence of a file on your server and then adjust the name of the file so that you do not overwrite the existing one. Hope someone can help
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:maneshr
ID: 2532960
if i am not wrong you are first trying to seperate the directory and the filename and then creating a new file with a running number.

if so you can use this. i have cut down the program size a lot without removing any validation checks

===file_ext.pl

#!/usr/local/bin/perl

$upname="/tmp/UserAccounts.pm";

#set filename
$filename = $upname;

$filename=~ /(.*)\/(.*)$/;
$dir=$1;
$FileCheck=$2;

# check directory existence - this works fine
if (!(-e "$dir")){
   print "Invalid Directory Names -\n";
   print "*$dir*\n";
   exit;
}

# find free file name
#What I'm doing here is simply separating the
#known file extension from the filename - it works
$NewFilename = $FileCheck;
$extension = ".pm";
$NewFilename =~ s/$extension//i;

$nCount = 0;


#this is the trouble area
while(){    ##Infinite loop. Be Careful!!
   $FileCheck = $NewFilename.$nCount.$extension;
   if (-e "$dir/$FileCheck"){
      ++$nCount;
   }else{
      last;
   }
}

print $FileCheck,"\n";
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2533633
microboard,

Surely the line
   while (-e      "$/cgi_user_dir/${DirCheck}/${FileCheck}") {
in your script should be
   while (-e      "${FULLPATH}cgi_user_dir/${DirCheck}/${FileCheck}") {

0
 

Author Comment

by:microboard
ID: 2533949
maneshr, I like your code although this line $filename=~ /(.*)\/(.*)$/;  appears to only parse a specific number of times. But doesn't matter, your code is very good. So, send an answer instead of a comment.
But just to let you know, I figured out what was wrong with the script. And it turns out no one but me could have solved it because it was being created within the calling application. The application was not sending the proper POST. It was leaving off a vital part that I was using when parsing the values.

0
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2534995
glad that you could figure out the problem.
0
 

Author Comment

by:microboard
ID: 2539277
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now