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>
|