Friday, October 22, 2010

HowTo: Run the .sh File Shell Script In Linux / UNIX

I've downloaded a software for Linux from the Internet. There is a file called How do I run an .sh file to install the software?

.sh file is nothing but the shell script to install given application or to perform other tasks under UNIX like operating systems. The easiest way to run .sh shell script in Linux or UNIX is to type the following commands. Open the terminal (your shell prompt) and type the command:




.sh File As Root User

Sometime you need to install application which requires root level privalage
Some time you need root access to install application; without root, you won't have the necessary permissions to install application or make system level modifications. Root access is disabled by default on many Linux and UNIX like systems. Simply use sudo or su as follows:

sudo bash
Type your password. Another option is to use the su command as follows to become superuser:

su -
Type root user password and finally run your script:
chmod Command: Run Shell Script In Linux

Another recommend option is to set an executable permission using the chmod command as follows:

chmod +x

Now your can run your .sh file as follows


How to use Unix shell script to send e-mail.

Recently, got problem to send e-mail from Unix with an existing shell script. It took me a long time to fixed. Here is the implementation of mine.

1. Mail sender


public class MailSender {

public void send() throws Exception {
String[] cmd = { "sh", "/opt/app/test/",
"Victor Mail Sender", "hello Victor!", "",
"" };

Runtime rt = Runtime.getRuntime();

Process p = rt.exec(cmd);

BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String x = null;
while ((x = br.readLine()) != null) {
} finally {
try {
if (br != null)
} catch (Exception e) {

public static void main(String[] args) {
try {
MailSender ms = new MailSender();
} catch (Exception e) {

In the above code, when we build the command used to send mail, we use "sh" as the first value of string array. If without this value, the must be set an executable permission. Using array to execute the shell script is to allow space in the subject and message body. If you put them in a string as a command to execute, it will not work and all the space in the command will be used as a separator.

The follow info is copied from The Java Developers Almanac.

     try {
       // Execute a command without arguments
       String command = "ls";
       Process child = Runtime.getRuntime().exec(command);
       // Execute a command with an argument
       command = "ls /tmp";
       child = Runtime.getRuntime().exec(command);
   } catch (IOException e) {

   //If an argument contain spaces, it is necessary to use the overload that requires the command and its arguments to be supplied in an array:
   try {
       // Execute a command with an argument that contains a space
       String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
       commands = new String[]{"grep", "hello world", "c:\\Documents and Settings\\f.txt"};
       Process child = Runtime.getRuntime().exec(commands);
   } catch (IOException e) {

Unix Shell Scirpt:


# --------------------------
# 1, Set up environment
# --------------------------


#Current environment variable
TS=$(date +%Y%m%d-%H%M%S)

# ------------------------------------------------------------
# 2, Check parameter and write initial info into log
# ------------------------------------------------------------
echo "--------------------------- ${TS} -------------------------- "  >>${LOG_HIS}

# Check if has parameter
if [ "$#" -lt 4 ]
    echo "Error: Missed parameters !" >> ${LOG_HIS}
    exit 1
    echo "Subject:  ${Subject}" >> ${_LOG_HIS}
    echo "body:  ${body}" >> ${LOG_HIS}
    echo "From:  ${from}" >> ${LOG_HIS}
    echo "To:  ${to}" >> ${LOG_HIS}

echo  $body |mailx -m -r $from -s "$Subject"  $to >> ${LOG_HIS}

exit 0