LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2009, 12:11 AM   #1
20033716
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Java to shell script


Greetings to all.Firstly I am new to the forum and I am new to UNIX/Linux and shell scripting.I need help from experts over here.I did not go much through this forum thanks to my lack of time.I need your help in converting a java file to a shell script file.I have pasted my code below.Its a standalone file.It gets two arguments from the user as input.If it is Value1 it will call Help and if it is Value2 it will call another functionality.Another functionality will load the data from the folder by iterating through each file.Finally after finding the results they are written in a file and the name of the file is finally printed.

<code>


import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

//Apache Struts required
//import org.apache.struts.action.Action;
//import org.apache.struts.action.ActionForm;
//import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionMapping;

import java.io.*;
/*
import java.net.*;
import java.sql.*;
import java.util.*;
//import javax.servlet.*;
*/
//OLD EJB

//import java.io.*;
import java.rmi.*;
//import java.util.*;
//import javax.naming.*;

//import logging.*;



public class standAlone
{

/* String result="failure";

String strQuery_type = "select distinct type from request_type where category= ? ";

String alcategory;
String altype;
String alitem; */
public static final String HELP_INDEX = "DIC.HLP";
public static final String ESCALATE_INDEX = "DIK.HLP";
public static final String FILE_EXTENSION = ".HLP";
public static final int SCRID_SIZE = 3;
public static String HELP_ESC_DATA_PATH;
public static final String file_location="/usr3/test";

// private InitialContext initialContext;
// private SessionContext sessionContext;

static
{
HELP_ESC_DATA_PATH = file_location+ "/data/help/";

//CLog.vLog("HELP-ESC:PATH=" + HELP_ESC_DATA_PATH);
}
// public String sSearchKey = "";
//String sSearchType = "";




public static void main (String args[])
{
String sSearchKey = args[0];
String sSearchValue = args[1].toUpperCase();

//CLog.vLog("hlpEscJava::The search string is as:"+args[0]);
//CLog.vLog("hlpEscJava::The search TYPE is as:"+args[1]);
System.out.println(returnFileName(args[0],args[1].toUpperCase()).toString());
System.exit(0);
}


private static String returnFileName(String sSearchType,String sSearchKey)
{
String sFileName = "0";
try
{

if (sSearchType.equals("Value1")) {
sFileName = "111";
sFileName = getHelpPanel(sSearchKey).toString();

}
else if (sSearchType.equals("Value2")) {
sFileName = "222";
sFileName = getEscalateData(sSearchKey).toString();
}
}
catch(RemoteException ex)
{

}
catch(Exception ex)
{
}
return sFileName;

}

//END OF MAIN

/*public void setSessionContext(SessionContext sessionContext)
{
this.sessionContext = sessionContext;
}*/

public static StringBuffer getHelpPanel(String searchCriteria) throws RemoteException,Exception
{
return (loadData((HELP_ESC_DATA_PATH + HELP_INDEX), searchCriteria));
}

public static StringBuffer getEscalateData(String searchCriteria) throws RemoteException,Exception
{
//CLog.vLog(HELP_ESC_DATA_PATH + ESCALATE_INDEX);
return (loadData((HELP_ESC_DATA_PATH + ESCALATE_INDEX), searchCriteria));
}


private static StringBuffer loadData(String indexFile,
String searchCriteria) throws RemoteException
{
//CLog.vLog("HELP-ESC:loadData(" + indexFile + "," + searchCriteria + ")");

StringBuffer buffer = new StringBuffer();
BufferedReader indexReader = null;
BufferedReader fileReader = null;

String currentLine = null;
String indexRecord = null;
String fileName = null;

int offset = 0;
int minimumOffset = 1024;

try
{
indexReader = new BufferedReader(new FileReader(indexFile));

while ((currentLine = indexReader.readLine()) != null)
{
offset = currentLine.indexOf(searchCriteria, (SCRID_SIZE - 1));

if ((offset > 0) && (offset < minimumOffset))
{
minimumOffset = offset;
indexRecord = currentLine;
}
}
}
catch (Exception exception)
{
//CLog.vLog("HELP-ESC:Exception");
//CLog.vLogErr("HELP-ESC:Exception");
throw new RemoteException(exception.toString());
}
finally
{
if (indexReader != null)
{
try
{
indexReader.close();
}
catch (Exception e)
{
//CLog.vLogErr(e);
}
}
}

if (indexRecord != null && indexRecord.substring(1, 4).trim().length() > 0)
{
try
{
currentLine = null;
fileReader = new BufferedReader(new FileReader(HELP_ESC_DATA_PATH + indexRecord.substring(1, 4)
+ FILE_EXTENSION));

//CLog.vLog("HELP-ESC:reading file(" + (HELP_ESC_DATA_PATH + indexRecord.substring(1, 4) + FILE_EXTENSION) + ")");

while ((currentLine = fileReader.readLine()) != null)
{
if (currentLine.indexOf("***APPEND HERE***") > 0)
{
try
{
buffer.append(appendData(HELP_ESC_DATA_PATH + currentLine.substring(1, 4)
+ FILE_EXTENSION));
}
catch (Exception exception)
{
//CLog.vLog("HELP-ESC:Exception loading append file");
//CLog.vLogErr(exception);
}
}
else
{
buffer.append(currentLine + "\n");
}
}
}
catch (FileNotFoundException exception)
{
//CLog.vLog("HELP-ESC:FileNotFoundException");
//CLog.vLogErr(exception);
return null;
}
catch (Exception exception)
{
//CLog.vLog("HELP-ESC:Exception");
//CLog.vLogErr(exception);
throw new RemoteException(exception.toString());
}
finally
{
if (fileReader != null)
{
try
{
fileReader.close();
}
catch (Exception e)
{
}
}
}
}
else
{
//CLog.vLog("HELP-ESC:returning:Finder Exception");

return null;
}

//CLog.vLog("HELP-ESC:returning\n" + buffer.toString());

return fileWriter(buffer.toString());
}

private static String appendData(String appendFile) throws Exception
{
//CLog.vLog("HELP-ESC:appending file(" + appendFile + ")");

BufferedReader fileReader = null;
StringBuffer buffer = new StringBuffer();

try
{
fileReader = new BufferedReader(new FileReader(appendFile));

String currentLine = null;

while ((currentLine = fileReader.readLine()) != null)
{
buffer.append(currentLine + "\n");
}
}
catch(FileNotFoundException exception)
{
return buffer.toString();
}
finally
{
if (fileReader != null)
{
try
{
fileReader.close();
}
catch (Exception e)
{
//CLog.vLogErr(e);
}
}
}

return (buffer.toString());

}
private static StringBuffer fileWriter(String fileContent)
{
FileOutputStream fos=null;
DataOutputStream dos=null;
File file=null;
try
{
file = new File(HELP_ESC_DATA_PATH + "/temp.txt");
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
dos.writeBytes(fileContent);
dos.close();
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
StringBuffer fileName=new StringBuffer(file.getPath());
return fileName;

}

}
</code>
 
Old 06-02-2009, 12:22 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
For more info on shell scripting e.g. bash shell scripting type
man bash
or
info bash
at command line.
While shell scripting can't support all that java can do.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing shell script from java application maheshkodamati Programming 5 02-24-2008 11:39 AM
running SHell script from java anirudh Programming 2 01-07-2005 07:37 AM
How to use Java in a Shell Script arnneisp Programming 1 07-13-2004 02:18 AM
shell script to compile java czy11421 Programming 4 04-30-2004 09:42 AM
can't launch java app from shell script... LonAllen Linux - Newbie 0 07-07-2003 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration