I am trying to make a .sh file that when clicked it runs the script inside. I am trying to recursively find a certain string value inside the contents of the files from a given folder, using $ grep -r "word" /home/folder_name but I don't know how to do so without running the script in terminal.
Any ideas for this?
Linux shell scripts can be written very basically.To make a shell script, start the script with #!/bin/sh and add normal linux commands. That is a simple explanation, but it is sufficient for most simple scenarios.
Example:
#!/bin/sh
echo "Hello, World!`
So I have a bash script which has some .sh files within it, the .sh files contain instructions to copy from and execute a .jar file which is also called upon at the beginning of the script. I can't seem to successfully execute the .sh files but the .jar file at the beginning executes without any issues. Here is what I mean: java -jar energyapp-SNAPSHOT-jar-with-dependencies.jar &This is at the beginning, and has no problem executing. Then later in the script I have this: ./mAhSim.sh & sh ./solarsim.sh & sh ./batterysim.sh &. These have instructions as follows:
java -cp energyapp-simulators-0.0.5-SNAPSHOT-jar-with-dependencies.jar com..xml.SolarSimulator'
but when I execute it, it fires up the browser as expected, but I get an error reading "Error retrieving events & data"
Any help is greatly appreciated, thanks folks
You can use source command to execute another shell script within the same folder.
Use this:
source filename.sh
Or you can use the alias:
. filename.sh
Also make sure all your files are executable.
So you can do:
source mAhSim.sh && source solarsim.sh && source batterysim.sh
I have a batch file and it has below command(.bat file) to execute my application. I need to move the same application to Linux environment and need to write .sh file to execute the same application. I don't have any idea of the shell scripting. Please can some one give idea to convert this to .sh file?
SET currentDir=%CD%
CD %~dp0
SET CLASSPATH=./lib/*;
java -Dlogback.configurationFile=./com/logback.xml -cp "%CLASSPATH%";"App.jar" com.test.main.MainClient
SET ERROR_LEVEL=%ERRORLEVEL%
CD %currentDir%
EXIT /B %ERROR_LEVEL%
Since it dosn't appear to be very complex, conversion shouldn't be too dificult. Have look here for a comparison of batch and bash syntax.
You can leave your java-command almost as it is, just change the way you call CLASSPATH. And keep in mind that bash is case-sensitive.
I have one .sh file which runs well on MAC system. I want to run the same .sh file on windows using CYGWIN. My question is, Do I need to make any modification in the commands while using it in Windows system with CYGWIN? Please help.
e.g. I have the following commands in .sh file-
export ML_SERIALIZE_DIR=/Users/Mxyz/ML
export ML_SERIALIZE_GRAPH=true
echo ML_SERIALIZE_DIR = $ML_SERIALIZE_DIR
echo ML_SERIALIZE_GRAPH = $ML_SERIALIZE_GRAPH
java -DCL_LOG_DIR="/Users/Mxyz/ML" -classpath .:lib/:Ml-mobxyz-import.jar org.xy.mobxyz.mobxyz.ML
echo "Batch program is complete"
No need to change anything but make sure that all required binaries by your .sh must be there in CYGWIN
Using a Linux shell, how do I start a program with a different working directory from the current working directory?
For example, I have a binary file helloworld that creates the file hello-world.txt in the current directory.
This file is inside of directory /a.
Currently, I am in the directory /b. I want to start my program running ../a/helloworld and get the hello-world.txt somewhere in a third directory /c.
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c, then executes helloworld from /a. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c, make the execution of helloworld conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:
(cd /c && exec /a/helloworld)
This way is more similar to how you usually run commands on the shell. To see the practical difference, you have to run ps ef from another shell with each solution.
An option which doesn't require a subshell and is built in to bash
(pushd SOME_PATH && run_stuff; popd)
Demo:
$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit
sh -c 'cd /c && ../a/helloworld'
Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:
cd SOME_PATH && run_some_command ; cd -
I always think UNIX tools should be written as filters, read input from stdin and write output to stdout. If possible you could change your helloworld binary to write the contents of the text file to stdout rather than a specific file. That way you can use the shell to write your file anywhere.
$ cd ~/b
$ ~/a/helloworld > ~/c/helloworld.txt
why not keep it simple
cd SOME_PATH && run_some_command && cd -
the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.
One way to do that is to create a wrapper shell script.
The shell script would change the current directory to /c, then run /a/helloworld. Once the shell script exits, the current directory reverts back to /b.
Here's a bash shell script example:
#!/bin/bash
cd /c
/a/helloworld
If you always want it to go to /C, use an absolute path when you write the file.
If you want to perform this inside your program then I would do something like:
#include <unistd.h>
int main()
{
if(chdir("/c") < 0 )
{
printf("Failed\n");
return -1 ;
}
// rest of your program...
}
from the current directory provide the full path to the script directory to execute the command
/root/server/user/home/bin/script.sh