Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a function in javascript using UNIX shell script? 2

Status
Not open for further replies.

NKnina

Programmer
Oct 3, 2001
7
US
I have this generateReport() function in report.js (javascript file). I need to create a shell script that calls and activate this function- 'generateReport()' in order to produce the report. Can anyone pls show the way to write this shell script?
Thanks for your help..

Regards,
NINA
 
Huh? That is quite a strange request. Javascript is generally executed in a browser window, or at least in some sort of GUI client window. The only non-gui usage of Javascript at the moment is for server-side web scripting, but that is a completely different thing.

Please tell me a little more about what you are trying to do. What operating system are you using? Where did this script come from? Was it originally intended to be executed from a web browser?
 
Strange? Well, that's why i need ur help to do this...

Actually, i'm doing a cronjob task that will have certain reports to be generated and sent to a list of email recipients. Usually, the reports are generated from the web browser,a form with options to select and a button(onClick will activate generateReport() function) to generate the report. But this time, i need to the report to be auto-generated daily (the cronjob).

The cronjob executes 2 shell scripts - 1)to generate report 2)email reports to user
The second part is done but i need help to do the first part. To generate the report, function generateReport(typeReport) must be activated, without going to the browser. That's why i'm asking for a shell script that could call the javascript function. Note that the javascript function is in select.js file. All this are done in UNIX.
 
I doubt that there is a direct way to call Javascript from a Unix shell script. I mean, I have heard about some attempts to create a Javascript interpreter with Perl, but it's not very commonly used.

Most likely, if we looked at the web page that hs this Javascript code, we would find that in the end, there is a URL querystring call involved. The best bet is to emulate in BASH or Perl what the script does (the syntax shouldn't be too difficult), and then make a GET request to the end URL query string.

Please post the code to the Javascript, and we will take a look ;-).
 
This is the function that i want to activate using shell script (this is a portion of the .js file..there are other functions..not related). This code is in select.js file

...
......
function generateReport(typeReport){

Months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

today = new Date();
month = Months[today.getMonth()];
nday = today.getDate();
year = today.getFullYear();
sday = today.getDate()-6;

if (sday < 1) {
if ((month == 'Mar')||(month == 'May')||(month == 'Jul')||(month == 'Oct')||(month == 'Dec')){
stday = 30 + sday;
}else{
stday = 31 + sday;
}
smonth = Months[today.getMonth()-1];

}else{
stday = sday;
smonth = month;
}

startPeriod = smonth+&quot; &quot;+stday+&quot; &quot;+year;
endPeriod = month+&quot; &quot;+nday+&quot; &quot;+year;

cgiCall = '/servlet/MReport?option=EReport&type=Excel';
cgiCall += '&trend=Daily';
cgiCall += '&startPeriod=' + toCgiFormat(startPeriod);
cgiCall += '&endPeriod=' + toCgiFormat(endPeriod);
cgiCall += '&saveToFile=true';
cgiCall += '&name=Summ';

openExcel(cgiCall,this.window.name);

}

....
...
 
Some further ideas ... I doubt you can call the javascript function directly from a shell script. What if you create an HTML page which calls the function as it loads.

You could then use the shell script to either

1) Start a browser on Unix, open the page causing the report to be generated, and then close the browser.

2) Get hold of some sort of HTML parser/executor for Unix, and run the HTML page throught that.

As I say, just some &quot;laymans&quot; ideas :)

Greg.
 
OK, now. Sorry, I have been unable to respond to this until today, because I had a couple of crazy deadlines. Have you solved this problem on your own yet?

If not, I will make the following notes:

As you can see final call this function makes is to call another JS function, which apparently opens a browser window at http://[hostname]/servlet/MReport, with certain parameters passed on the URL. We could paraphrase it as:
Code:
http://[hostname]/servlet/MReport?
option=EReport&
type=Excel&
trend=Daily&
startPeriod=[start_period]&
endPeriod=[end_period]&
saveToFile=true&name=Summ

The variables in brackets are the only areas we don't know yet. The first one is &quot;hostname&quot;, but that's no problem, since you know that. The second two parameters are the unknown area to deal with: the starting period and the ending period of this report. Unfortunately, the Javascript you gave me was not enough to determine how those values are to be formatted. There are two things you can do:

1. Show me the code for the Javascript function called toCgiFormat().

2. Just do a sample report, and when the browser opens that window, right click, choose Properties, or Info, and look for the string containing the full URL that the browser is calling. Copy that string and paste it here.

Or better yet, do both.

Once we know exactly how that URL query string is to be formatted, it's a very simple matter to handle this in Perl or Bash.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top