Assuming you meant Java and not javascript, due to the syntax of your example.
You can use Runtime.getRuntime().exec(String) to execute a shell command. Try passing your command into that method. If this doesn't work, you could try passing a String[] with the structure ["/bin/sh", "-c", **command**].
Compiling and Executing a Java File With a Shell Script
convert a java program into a linux command - Stack Overflow
java - converting a shell script to a jar file - Stack Overflow
Converting a java program to linux script
Sample script that can might further help-
#!/bin/bash
#Set whatever number of arguments you expect for the Java jar you have
ARGS_EXPECTED=3
if [ $# -ne $ARGS_EXPECTED ]
then
echo "[$HOSTNAME]: Usage: `basename $0` filename arg1 arg2"
exit 1
fi
java -cp yourfile.jar com.yourpkg.Driver $1 $2 $3
Save the above content to a file, say test.sh
and use the command to give an executable permission chmod +x test.sh
run like ./test.sh filename arg1 arg2 from current directory where test.sh is
I thing this can be useful for your case: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/binfmt_misc.txt?id=HEAD
Hey, I have been given a task to convert a java program into a linux script...
Ik basics of linux and I can convert some basic lines of java into linux, but problem arises when custom packages comes into picture...
The java program has some custom packages imported and designed to perform specific task like connecting to a server with credentials such as username and host etc
So my query is how do I manage this custom packages in linux scripts, I could not find anything about this on internet, and even search this subreddit but did not find anything...
Also my lead doesnt want this to run this program through script, she explicitly wants it to convert into a unix script...
I don't know anything about Java, but I can show you a proof of concept. Say we have localfile.txt:
Here is the local file.
and on the remote machine, we have remote.sh:
#!/bin/bash
cat /dev/stdin
Note that the script on the remote machine invokes a program which reads from stdin. Then pass the contents of localfile.txt to your ssh command:
user@local:~$ cat localfile.txt | ssh user@remote remote.sh
Here is the local file.
Your Java program is trying to read a file which does not exist on the remote machine. I guess you could try to mimic a local file.
Change remote.sh to:
#!/bin/bash
cat "$@"
and invoke it with
user@local:~$ cat localfile.txt | ssh user@remote 'remote.sh <(cat /dev/stdin)'
Here is the local file.
I think it would be easier to change that part in your Java program to read from stdin.
I guess things might get messy if localfile.txt contains anything that might be interpreted by the shell as expandable (such as *), but that's for you to figure out.
The problem is that the shell redirection (<) sends the file over the ssh tunnel. And the Java class is expecting not the file, but a string with the "filename" of a local file that will be read with a FileReader.
Instead of passing the filename to the FileReader, read from the standard input.
InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader iR = new BufferedReader (isReader);
Used this as a reference: https://stackoverflow.com/questions/5724646/how-to-pipe-input-to-java-program-with-bash
Now your class will be:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
public class ReadFirstLine
{
public static void main(String[] args) throws Exception
{
String filename = args[0];
System.out.println(filename);
//BufferedReader iR = new BufferedReader (new FileReader(filename));
InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader iR = new BufferedReader (isReader);
BufferedWriter oW = new BufferedWriter(new OutputStreamWriter(System.out));
//outputWriter.write(iR.readLine());
System.out.println(iR.readLine());
iR.close();
oW.close();
}
}
But for this task I would definitely use instead:
head -1 filename.txt
:)