No output when running jar from shell script - linux

I am running a jar-file in a shell script calling hierarchy. I want to redirect the output from executing the jar file to my logfile "loga.log" but this does only work when I run this script with "sh"-command.
My calling hierarchy basically looks like this:
Script A calls script B with:
/path/to/script/scriptb.sh &> /path/to/logs/loga.log
Script B calls script C with:
/path/to/script/scriptc.sh > /path/to/logs/logb.log
Script C executes jar-file with:
java -Xms512m -Xmx1280m -Djava.awt.headless=true -jar /path/to/jar/thejar.jar > /path/to/logs/stdout.log #2>/path/to/logs/stderr.log
The first script is started by command line with:
sh /path/to/scripts/scripta.sh
and then I get written my output as wanted to the loga.log-file. When I start the script via:
/path/to/scripts/scripta.sh
this doesn't work and the output is displayed in the console. Unfortunately I am not able to use the sh command because these scripts must be triggered by a UC4-job system which calls this script without using sh. Calling the second script using "sh" inside the first one doesn't work eigther.
How can I edit the scripts to be able to log all output to the specified logfile when running the first one via the above command (without sh)?
Thanks in advance,
Martin

Related

Can't access Java from shell script

I am trying to execute a jar file from a bash script, but for whatever reason, java subcommands are not working within the shell script. Attached is an example of a simple script, and the error it returns. The same error is outputted when I use any subcommand for java.
What do I need to fix?
#!/bin/sh
java -version

How to write a shell script file that can run a script in Linux

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!`

start another bash script from main bash script

I have a bash script, from which I need to start another script with source. This is working fine, but I also need to pass the 2nd script parameters.
Example:
source /usr/local/scripts/parallel.sh test --gnu
So I need to start parallel.sh with a given data-source file called test, and I also need to assign a parameter --gnu at the end. But it is not recognizing the file and the parameter.
The source command is likely not what you're looking for.
When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.
So, unless you need access to variables or functions inside parallel.sh, just call it directly:
/usr/local/scripts/parallel.sh test --gnu
As long as the script is executable (chmod +x /usr/local/scripts/parallel.sh) and set up to work with the options you're passing ($1 will contain "test" and $2 will contain "--gnu") it should work just fine.

How to change directory through a script file

i want to execute some commands through terminal. I have on script for executing commands.Some commands are working but when trying to change directory its not changing. There is no error while executing that script.The script which i made is executable and is mention below:
make clean
make
cd /home/user
save this as script.sh and make it executable
Current working directory is a process property. Each process has independent value for its working directory. Your script works correctly: it changes the current working directory of the shell process that executes it.
If you want your interactive shell to change working directory you have to instruct it. You can do it by "sourcing" your script into your interactive shell. "Sourcing" means reading the script and executing the commands by the shell that sources it. This is opposed to "executing" the script, where a separate shell process is started and executes the script contents.
You can source a script using source or . commands. Like this:
source script.sh
or this:
. script.sh

Running a command in shell script

I have a shell script file (run.sh) that contains the following:
#!/bin/bash
%JAVA_HOME%/bin/java -jar umar.jar
when i try to run it (./run.sh), it gives me following:
umar/bin/run.sh: line 1: fg: no job control
However if I run same command directly on shell, it works perfectly.
What's wrong with the script file?
Thanks
%foo% is not how you do command substitution in a bourne/BASH shell script. I assume you're running this from a Windows command line, which is why it works when you run it directly. Try using proper bourne syntax:
${JAVA_HOME}/bin/java -jar umar.jar
Try turning on monitor mode
set -m
%JAVA_HOME% will substitute a Windows environment variable and is appropriate in a .bat file.
Try the following shell script which should work on most UNIX like systems.
#!/bin/bash
$JAVA_HOME/bin/java -jar umar.jar

Resources