com.izforge.izpack.util.os.unix
Class ShellScript

java.lang.Object
  extended by com.izforge.izpack.util.os.unix.ShellScript

public class ShellScript
extends java.lang.Object

A Generator, Wrapper and Executor for Unix ShellScripts

Author:
marc.eppelmann@reddot.de

Field Summary
private static java.lang.String Author
          Author = "marc.eppelmann_at_gmx.de"
private static java.lang.String CommentPre
          internal comment prefix; makes a line as comment:-) :: 'CommentPre = "# "'
private  java.lang.StringBuffer content
          Internal ContentBuffer of this ShellScript
private static java.lang.String currentDateMsg
          "Generated at: " + new Date().toString()
private static java.lang.String explanation
          the explanation header for this generated script
private static java.lang.String Generator
          Generator = "Generator: " + ShellScript.class.getName()
private static java.lang.String H
          H = CommentPre
private static java.lang.String header
          the header of this ShellScript
private  java.lang.String itsLocation
          internal field: where to write via write( itsLocation ) this shellscript.
private static java.lang.String lf
          the linefeed: lf = "\n"
private static java.lang.String lh
          lh = lf + H = "\n#"
private static java.lang.String Revision
          internal Revision = "$Revision$"
private static java.lang.String SCM_ID
          internal SourceCode Management ( currently 'svn') ID :: 'SCM_ID = "$Id$"'
 
Constructor Summary
ShellScript()
          Creates and initializes the ShellScript for running on the bourne shell: "sh".
ShellScript(java.lang.String aShell)
          Creates and initializes the ShellScript for running on the given shell.
 
Method Summary
 void append(char aChar)
          Appends a Char to this ShellScript.
 void append(java.lang.Object anObject)
          Appends an Object or String to this ShellScript.
 void appendln()
          Appends an Object or String to this ShellScript with unix linefeed ("\n").
 void appendln(char aChar)
          Appends a Char Object or String to this ShellScript with unix linefeed ("\n").
 void appendln(java.lang.Object anObject)
          Appends an Object or String to this ShellScript with unix linefeed ("\n").
 java.lang.String exec()
          Execute this ShellScript.
 java.lang.String exec(java.lang.String itsParams)
          Executes thsi ShellScript with the given Params.
static java.lang.String execAndDelete(java.lang.StringBuffer lines, java.lang.String aLocation)
          Executes and removes the script.
static java.lang.String execAndDelete(java.lang.String aShell, java.lang.StringBuffer lines, java.lang.String aLocation, java.lang.String itsParams)
          Executes and removes the script.
static java.lang.String execute(java.lang.StringBuffer lines, java.lang.String aLocation)
          Executes ths given lines in the created default shell (sh) stored on location.
static java.lang.String execute(java.lang.String aShell, java.lang.StringBuffer lines, java.lang.String aLocation, java.lang.String itsParams)
          Execs ths given lines in the creted shell stored on location.
 java.lang.StringBuffer getContent()
          gets the Content of this Script.
 java.lang.String getContentAsString()
          Gets the Content of this Script as String
static void main(java.lang.String[] args)
          Test Main Method Run test with: java -cp .jar com.izforge.izpack.util.os.unix.ShellScript
 java.lang.String toString()
          Dumps the ShellScript Content, and Location.
 void write(java.lang.String aDestination)
          write this to the given Destination FileName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

Author

private static final java.lang.String Author
Author = "marc.eppelmann_at_gmx.de"

See Also:
Constant Field Values

Generator

private static final java.lang.String Generator
Generator = "Generator: " + ShellScript.class.getName()


SCM_ID

private static final java.lang.String SCM_ID
internal SourceCode Management ( currently 'svn') ID :: 'SCM_ID = "$Id$"'

See Also:
Constant Field Values

Revision

private static final java.lang.String Revision
internal Revision = "$Revision$"

See Also:
Constant Field Values

CommentPre

private static final java.lang.String CommentPre
internal comment prefix; makes a line as comment:-) :: 'CommentPre = "# "'

See Also:
Constant Field Values

H

private static final java.lang.String H
H = CommentPre

See Also:
Constant Field Values

lf

private static final java.lang.String lf
the linefeed: lf = "\n"

See Also:
Constant Field Values

lh

private static final java.lang.String lh
lh = lf + H = "\n#"

See Also:
Constant Field Values

explanation

private static final java.lang.String explanation
the explanation header for this generated script

See Also:
Constant Field Values

currentDateMsg

private static java.lang.String currentDateMsg
"Generated at: " + new Date().toString()


header

private static final java.lang.String header
the header of this ShellScript


content

private java.lang.StringBuffer content
Internal ContentBuffer of this ShellScript


itsLocation

private java.lang.String itsLocation
internal field: where to write via write( itsLocation ) this shellscript.

Constructor Detail

ShellScript

public ShellScript(java.lang.String aShell)
Creates and initializes the ShellScript for running on the given shell.

Parameters:
aShell - "sh", "bash", "ksh", "csh" and so an...

ShellScript

public ShellScript()
Creates and initializes the ShellScript for running on the bourne shell: "sh".

Method Detail

append

public void append(java.lang.Object anObject)
Appends an Object or String to this ShellScript.

Parameters:
anObject - the Object to append

append

public void append(char aChar)
Appends a Char to this ShellScript.

Parameters:
aChar - a char to append

appendln

public void appendln(java.lang.Object anObject)
Appends an Object or String to this ShellScript with unix linefeed ("\n").

Parameters:
anObject - the Object to append

appendln

public void appendln(char aChar)
Appends a Char Object or String to this ShellScript with unix linefeed ("\n").

Parameters:
aChar - a char to append

appendln

public void appendln()
Appends an Object or String to this ShellScript with unix linefeed ("\n").


getContent

public java.lang.StringBuffer getContent()
gets the Content of this Script.

Returns:
the Content

getContentAsString

public java.lang.String getContentAsString()
Gets the Content of this Script as String

Returns:
the script as String

toString

public java.lang.String toString()
Dumps the ShellScript Content, and Location. Use getContentAsString() to get this ShellScripts Content

Overrides:
toString in class java.lang.Object
Returns:
The ShellScript as Object dump.

write

public void write(java.lang.String aDestination)
write this to the given Destination FileName

Parameters:
aDestination - a destination filename

exec

public java.lang.String exec(java.lang.String itsParams)
Executes thsi ShellScript with the given Params.
NOTE: the params cannot be contain whitespaces.
This (su -c <br>"cp from to"</br>) would not work:
because the underlaying java.runtime.exec("command") does not handle balanced or unbalanced (") correctly.
else just whitespace separate tokens.
This means for the sample. runtime.exec() would ever execute such as: su "-c" "\"cp" "fromFile" "toFile\""
But this his hidden in Sun's native code ;-(
This was the reason to write thsi class to have a Workaround :-)

Parameters:
itsParams -
Returns:
the output from stdout of the execution.

exec

public java.lang.String exec()
Execute this ShellScript.

Returns:
the output from stdout of the execution.

execute

public static java.lang.String execute(java.lang.String aShell,
                                       java.lang.StringBuffer lines,
                                       java.lang.String aLocation,
                                       java.lang.String itsParams)
Execs ths given lines in the creted shell stored on location.

Parameters:
aShell - A Shell which will be eexecute the script.
lines - The content of the script.
aLocation - The location where to store.
itsParams - Th eoptional params of the script.
Returns:
the exec result

execute

public static java.lang.String execute(java.lang.StringBuffer lines,
                                       java.lang.String aLocation)
Executes ths given lines in the created default shell (sh) stored on location.

Parameters:
lines - the lines of the script to exec.s
aLocation - where to store
Returns:
the stdout of the script.

execAndDelete

public static java.lang.String execAndDelete(java.lang.String aShell,
                                             java.lang.StringBuffer lines,
                                             java.lang.String aLocation,
                                             java.lang.String itsParams)
Executes and removes the script.
The Lines be also written in python or perl,
In this case, the Shell must be python or perl or so.

Parameters:
aShell - The Shell which should exec the script. Can be also be python or perl, if the shellcontent is given in this language.
lines - of the script.
aLocation - where to store.
itsParams - which should be pass to the script.
Returns:
the stdout.

execAndDelete

public static java.lang.String execAndDelete(java.lang.StringBuffer lines,
                                             java.lang.String aLocation)
Executes and removes the script.

Parameters:
lines - of the script.
aLocation - where to store.
Returns:
the sdtout.

main

public static void main(java.lang.String[] args)
Test Main Method Run test with: java -cp .jar com.izforge.izpack.util.os.unix.ShellScript

Parameters:
args - Arguments from Commandline