Solved

quick script (important)

Posted on 2000-02-20
6
164 Views
Last Modified: 2013-12-25
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
Comment
Question by:wormboy__6
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
monas earned 100 total points
ID: 2540398
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
 

Author Comment

by:wormboy__6
ID: 2582625
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
 
LVL 3

Expert Comment

by:monas
ID: 2582642
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Expert Comment

by:ozo
ID: 2582643
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
 

Author Comment

by:wormboy__6
ID: 2584227
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
 

Author Comment

by:wormboy__6
ID: 2585816
I have figured it out thanks to your help, cheers.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

17 Experts available now in Live!

Get 1:1 Help Now