Often when working with Bash scripts we find that we want to add some options to the script by way of arguments or variables. Easily enough Bash command line arguments can be accessed by $1, $2, etc within the script where $1 is the first argument or variable and #2 is the second etc.

Example:

#!/usr/bin/env bash
 
echo the first argument is $1
echo second argument is $2
echo eleventh argument is $11
echo total number of arguments is $#

Resources:

, , ,

When working in a unix environment you may find the need to compare the contents of 2 files. To do this unix provides the diff command. The diff command takes 2 files as an argument and outputs the differences between them in a simple easy to understand format using a simple change notation using numbers and the c d a and , characters.

How to run the diff command

Running the diff command is done by typing diff followed by the 2 files to be compared.

diff file1.txt file2.txt
diff file1.txt updates/file2.txt

Understand the output

The diff command outputs the difference found in the file or nothing if there are no differences. The basic syntax is that a reference line will be outputted dictating the lines affected and the type of change followed by the contents of the files for the differing lines.
The type of change is coded by the character between the line references. Where:

  • c: item changed
  • d: item was deleted
  • a: item was added

To the left of the code character is denoted the line numbers present in the first file and to the right is the line numbers in the second file. For example

  • 1c1
    Means the first line differs between the two files
  • 8d7
    Means that line 8 of file one was deleted from file 2 and it would have been present after line 7
  • 9a9
    Means that line 9 of file two was added and would be present after line 9 in file one
  • 2,4c3,5
    Means that there was a multi-line change where lines 2-4 in the first file where changed to the value in lines 3-5 of the second file.

In addition to the codes that explain the differences the lines that are different are also outputted and follow the codes. A < character before teh output means first file and > character means second file. When output is shown for both files in the case of a change — will separate the output of their content. For example

2,4c2,4
< bbb
< ccc
< ddd
---
> bcd
> cde
> def
,

As one that seldom finds himself having to run Java Applications from a Unix Bash Script, I thought it would be helpful to share the appropriate commands i use to execute the code and to notify the user of success or failure. For reference the folder structure I use is:

app_name
    - lib
    - config

where the lib folder contains all necessary jar files to run the application, and the config folder contains any properties or configuration files passed as run time arguments

Writing the script

  1. Specify the script is a bash script
    #!/bin/bash
  2. Define SUCCESS and FAILURE variables
    FAILURE=1
    SUCCESS=0
  3. Define the appdirectory
    APP_HOME=/tmp/app
  4. Define the lib folder variable
    LIB=${APP_HOME}/lib/
  5. Define the classpath variable. This is a : seperated list of jars needed by the application
    CP=${LIB}abc.jar:${LIB}xyz.jar
  6. Define java home
    APP_JAVA_HOME=/usr/jdk/instances/jdk1.5.0
  7. Execute the Application with any arguments and java settings
    ${APP_JAVA_HOME}/bin/java -ms256m -cp "${CP}"
      com.mdibtz.TestApp "${APP_HOME}/config/config.properties"
  8. Test for Failure
    if [ $? -ne 0 ]
    then
        exit ${FAILURE}
    fi
  9. Success Logic if needed
    exit ${SUCCESS}

Complete Script

#!/bin/bash
 
FAILURE=1
SUCCESS=0
 
APP_HOME=`pwd`
LIB=${APP_HOME}/lib/
 
CP=${LIB}activation-1.1.jar:${LIB}mail-1.4.jar
 
APP_JAVA_HOME=/usr/jdk/instances/jdk1.5.0
 
${APP_JAVA_HOME}/bin/java -ms256m -mx768m -cp "${CP}" 
  com.mdbitz.TestApp "${APP_HOME}/config/config.properties"
 
if [ $? -ne 0 ]
then
    echo "AppFailed during run"
    exit ${FAILURE}
fi
 
echo "App successfully run"
exit ${SUCCESS}
, , ,

When transferring files between Windows and Unix you may find that on occasions your file has been modified and that ^M characters exist at end of lines.  This problem is caused when files are transferred to the server as Binary. However if you change the type to ASCII then you will find that the ^M characters are not added. If however you find the file has ^M characters then Unix provides a handy dos2unix command to strip the unwanted ^M characters.  For full reference unix2dos is also provided to format the file back.

dos2unix usage:

dos2unix <file> <output_file>
, ,