• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

quick script (important)

Can someone please write me a script which will do the following:

Divide a variable $x by $y, if this value is smaller than $a divided by $b then return $Yes to the browser and if it is bigger then return the string "No" to the browser.

This will be run on a unix server and can this script please include code to have it be able to run only once at a time (i cant get flock() to work) and for it to return the string "Running" if it is already being run.

Thanks
0
wormboy__6
Asked:
wormboy__6
  • 3
  • 2
1 Solution
 
monasCommented:
use CGI;

$q = new CGI;

$x = $q->param('x');
$y = $q->param('y');
$a = $q->param('a');
$b = $q->param('b');

print $q->header;
$lock_dir = '/path/to/lock/directory/';

if (mkdir $lock_dir, 0600){
  # direcotry created - we can proceed
  eval {$d = ($x/$y) - ($a/$b);}
  if ($d <0){print "Yes";}
  else {print "No";}
  rmdir $lock_dir;

} else {
  # directory not created
  print "Running"
  ### you may add directory age checking here...
}
0
 
wormboy__6Author Commented:
ok, i have edited the code to this:

#!/usr/bin/perl
$Count = 10;
$CurrentTime = 100;
$Limit = 200;
$TotalTime = 500;
$lock_dir = 'Testings';
$d = mkdir $lock_dir, 0777;
if ($d = 1) {
  # direcotry created - we can proceed
  eval '$FirstRatio = ($Count/$CurrentTime)';
  eval '$MainRatio = ($Limit/$TotalTime)';
if ($FirstRatio < $MainRatio){print "Yes";}
  else {print "No";}
  rmdir $lock_dir;

} else {
  # directory not created
print "Running";
  ### you may add directory age checking here...
}

My problem however is that when i put this script onto a webserver (a free one with scripting) it does not work. I can not get it to create a directory. Can u change this to make it work? Is there permission problems or what?

Do i need that stuff at the start of your script? The $q and param stuff?

Also, if i create a directory on my computer named the same name as the locking directory, the script deletes the directory. Should it do this? I am using win98.

Thanks
0
 
monasCommented:
re directory: if this directory is present - this shows that script is running. And every time you try to run script it first trys to create if it succeeds - it removes this dir when finished. Otherwise I can't say is this script already running.

In principle this "lock directory" shoul be missing when you start your script!

As for $q and param stuff. If that's not CGI script - you don't need them.  

If this IS cgi scipt and  you can live with hardcoded values - you don't need param stuff. Althoug I would recomend you very much to keep "$q= new CGI" and "print $q->header". These two lines will ensure that your script will produce correct headers required by CGI protocol.

As for not creating directory... I don't have computer with w98+perl - I can't check. But the only idea would be - try to write full path like "D:\\TEMP\\Locking" where you have directory creating permitions for sure.

Good Look!
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
ozoCommented:
perl -Mdiagnostics -w
Found = in conditional, should be == at - line 8 (#1)
   
    (W) You said
   
        if ($foo = 123)
   
    when you meant
   
        if ($foo == 123)
   
    (or something like that).
0
 
wormboy__6Author Commented:
The script i wrote works on win98 with perl. How come it does not work on unix?

What do i need to change to make it work? (i think i may have the make directory line incorrect, please check)
0
 
wormboy__6Author Commented:
I have figured it out thanks to your help, cheers.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now