简体   繁体   中英

Convert Java program to Shell script

If I setup a maven project and write a Java program with main method, I want to convert this program to an equivalent shell script. In other words, the shell script should accept the arguments and should behave in the same way that the java program would behave when main method is called with arguments. How do I achieve this?

You can use shell script's variable arguments to achieve this. First you can write your logic in Java and then pass all the arguments from shell script to it.

#! /bin/bash
java <class_name> $@

$@ will pass all the arguments to the Java program.

import java.util.Scanner; public class Print_Series3 {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n,i,pr=0;
   System.out.printf("Enter the range of number(Limit):");
    n=sc.nextInt();
    for(i=1;i<=n;i++)
    {
        if(i%2==0)
        {
            pr=(int) (2*Math.pow(i,2)+1);
            System.out.print(pr+" ");
        }
        else
        {
            pr=(int) (2*Math.pow(i,2)-1);
            System.out.printf(pr+" ");
        }
    }
    sc.close();
}

}

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