How do I execute commands by running a file in Linux? I need to run the following commands:
cd /bin
java -cp main
What should the file extension be?
How do I execute commands by running a file in Linux? I need to run the following commands:
cd /bin
java -cp main
What should the file extension be?
What you want to achieve, is one of the most important features of any terminal, to execute commands from a file.
In Linux, you need not have any particular extension for a bash file, although the .sh
extension is customary.
Create a file with any name, (I'll choose script.sh
)
The contents:
#!/bin/bash
cd /bin
java -cp main
Make it executable now:
$ chmod a+x script.sh
Now execute the script:
$ ./script.sh
Voila! Your first bash script. You can learn more about bash scripting here.
Since the OP is running Raspian Wheezy, this can help him create scripts.
Running linux commands in java:
Doing this is possible with java.
Runtime.exec() method should work for you.
Write a shell script file and invoke it from the java code like this...
{
Process pro = Runtime.getRuntime().exec("./script.sh");
pro.waitFor();
}
The script that you create contains your desired command by following the bash introduction given in the other answer.