开发者

Calling a Java executable in bash

开发者 https://www.devze.com 2023-01-10 13:57 出处:网络
I would like to call the Java class file from compiling the following code: import java.io.*; public class hex_to_dec {

I would like to call the Java class file from compiling the following code:

import java.io.*;

public class hex_to_dec { 
    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;

    public hex_to_dec (String stringPath, String stringPath_dec)
    {
        try
        {
            bufferedReader = new BufferedReader(new FileReader(stringPath));
            bufferedWriter = new BufferedWriter(new FileWriter(stringPath_dec, false));
        } catch (Exception e) {
            System.out.println("Error in opening file." + e);
        }
    }
    public void Parse() 
    {
        try {
            String tempLine;
            int temp;           
            while((tempLine = bufferedReader.readLine()) != null) {
                String[] tempBytes = tempLine.split(" ");
                temp = Integer.valueOf(tempBytes[0], 16);
                tempBytes[0] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[2], 16); 
                tempBytes[2] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[3], 16);  
                tempBytes[3] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[5], 16);
                tempBytes[5] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[6], 16);
                tempBytes[6] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[8], 16);
                tempBytes[8] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[9], 16);
                tempBytes[9] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[11], 16);
                tempBytes[11] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[12], 16);
                tempBytes[12] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[14], 16);
                tempBytes[14] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[15], 16);
                tempBytes[15] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[17], 16);
                tempBytes[17] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[18], 16);
                tempBytes[18] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[20], 16);
                tempBytes[20] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[21], 16);
                tempBytes[21] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[23], 16);
                tempBytes[23] = String.valueOf(((byte) temp));

                            for (int i = 0; i < tempBytes.length; i++)
                {
                    bufferedWriter.append(tempBytes[i] + " ");
                }
                bufferedWriter.append("\n");                
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            System.err.println("Error:" + e);
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) { 
        // TODO Auto-generated method stub
        hex_to_dec dat开发者_StackOverflow社区a = new hex_to_dec(
                "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed\\s5_node12",
                "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed_dec\\s5_node12"); 
        data.Parse();
    }  
}

However, it requires an argument, and I don't know how to pass arguments into calling this command cleaning in bash. Also, I would like to be able to parse through a directory to call this function recursively through all the text files under the subdirectories of a selected directory. What's the easiest way of achieving this?

Thanks in advance! Hope this is not too demanding.


I think you have a couple of steps here, first is to change the main to utilize args. You should check args.length to make sure a source file is specified, for example:

(warning: untested java from a C programmer)

public static void main(String[] args)
{
  if (args.length == 1)
  {
    hex_to_dec data = new hex_to_dec(args[0], args[0] + ".dec");
    data.Parse();
  }
}

Once the class accepts an argument, you will want to compile it.

javac hex_to_dec.java

Once it is compiled, you can use a script to recursively handle a directory.

#!/bin/sh
find . | xargs -L 1 java hex_to_dec

Note that if your goal is to convert a file of hex numbers to decimal, using java and bash is probably overkill. You could accomplish this using a single shell script like:

#!/bin/sh
find . -type f | while read filename
do

  # skip the file if it is already decoded
  if [ "${filename%.dec}" != "${filename}" -o -z "${filename}" ]
  then
    continue
  fi

  (
    # read the file, line by line
    cat "${filename}" | while read line
    do
      line=$(
        echo "${line}"                   |
        sed -e "s/[[:space:]]\{1,\}/;/g" | # split the line by spaces
        tr '[:lower:]' '[:upper:]')        # convert lower to uppercase

       echo "ibase=16; ${line}"          | # format the line for bc
        bc                               | # convert hex to dec
        tr "\n" " "                        # rejoin the output to a line

      echo ""                              # add the new line
    done
  ) > "${filename}.dec"
done
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号