简体   繁体   English

将Java程序转换为Shell脚本

[英]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.如果我设置一个 maven 项目并使用 main 方法编写一个 Java 程序,我想将该程序转换为等效的 shell 脚本。 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?换句话说,shell 脚本应该接受 arguments,并且应该与 arguments 调用 main 方法时 java 程序的行为相同。我如何实现这一点?

You can use shell script's variable arguments to achieve this.您可以使用 shell 脚本的变量 arguments来实现这一点。 First you can write your logic in Java and then pass all the arguments from shell script to it.首先,您可以在Java中编写您的逻辑,然后将 shell 脚本中的所有 arguments 传递给它。

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

$@ will pass all the arguments to the Java program. $@会将所有 arguments 传递给 Java 程序。

import java.util.Scanner;导入 java.util.Scanner; public class Print_Series3 {公共 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();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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