简体   繁体   中英

Should I use Runtime.exec() to call bash shell script?

I have some files to be appended and sorted using unix sort utility. Then we would use Java to process the sorted file.

Initially I would want to use a bash shell script to do the sorting and at the later part of the script file, I call the Java program. But my concern is that shell script doesn't have a mature log library, you can only redirect output stream and error stream.

If I want the log to be better looking, should I use Runtime.exec() to call the bash shell script part in Java so that I can log them?

The following is the shell script file:

    cd /home/app/xyz/data

    echo -n "Total number of files: "
    ls | wc -l

    for file in ls ABC_*.DAT
    do
        echo "Appending" $file "..."
        cat $file >> /home/app/xyz/data/unsorted.DAT
        echo "Moving" $file "to back up folder..."
        mv $file /home/app/xyz/data/backup
    done

    sort -t ',' -k 1,1 -k 5,7 -k 2,2r unsorted.DAT -o sorted.DAT

    CALL JAVA_PROGRAM sorted.DAT

IMHO if you want to use Java you should rewrite this script logic to Java to improve portability. It is a short script, so it won't be difficult. You can look here: Optimal way to sort a txt file in Java .

But it depends on a scenario and usecase. It is possible to write script with good logging;)

I dont think "better logging" is a reason to execute shell scripts from java. The java process need only take the name of the file to process as argument. The acutal processing could be done directly on the terminal if possible. This way, your java program is portable too. But need to know the entire use-case to come to the right decision.

The shell script simply concatenates all ABC_*.DAT files in the current directory into a single file /home/app/xyz/data/unsorted.DAT , and then it sort the content to /home/app/xyz/data/unsorted.DAT into sorted.DAT. Finally, the script would call a Java program to process sorted.DAT .

You can do file reading, concatenation and sorting from Java. If you need some helper classes to process files from directories etc., then I would recommend Apache Fileutils . So, if I would recommend exploring the possibility of abandoning the shell script, consolidate all the processing into a single Java program, and avoid Runtime.exec() in this (apparently) simple case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM