Calling upon shell scripts / . jar files from within a bash script - linux

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

Related

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

Combine a shell script and a zip file into a single executable for deployment

I have two files for deployment,
1) deploymentpackage.zip -> It contains the database package with few shell scripts.
2) deployment.sh -> It is the primary shell script which first unzips the deploymentpackage.zip and then execute the list of shell files inside it.
It is working as expected.
But what I need is, I need to make the zip file as executable so that I dont want to deliver both deploymentpackage.zip and deployment.sh to client.
So Is it possible to make the deploymentpackage.zip as executable so that I don't want to have another script deployment.sh.
Expectation : Running this deploymentpackage.zip should unzip the same file and run the list of scripts inside it.
If it's ok to assume that the user who will run the script has the unzip utility, then you can create a script like this:
#!/usr/bin/env bash
# commands that you need to do ...
# ...
unzip <(tail -n +$((LINENO + 2)) "$0")
exit
Make sure the script has a newline \n character at the end of the line of exit. And, it's important that the last line of the script is the exit command, and that the unzip command with tail is right in front of it.
Then, you can append to this file the zipped content, for example with:
cat file.zip >> installer.sh
Users will be able to run installer.sh, which will unzip the zipped content at the end of the file.
Write a readme file, and ask your users to chmod the script, then to execute it.
For security reason I hope there is no way to auto-execute such things...
Edit: received a vote down because the OP did not like it, thanks a lot :)

call a shell script(command line tool) inside another shell script

I am using a tool called Droidbox for experiment.
The tool has a shell script droidbox.sh which I can invoke through terminal.
droidbox.sh takes one argument i.e path of the apk
Usage: ./droidbox.sh APK
I want to call the droidbox.sh through a shell script.
I wrote a shell script like
#!/bin/bash
ARG1="/home/xxx/a.apk"
/home/xxx/DroidBox_4.1.1/droidbox.sh "$ARG1"
I am getting error which says
python: can't open file 'scripts/droidbox.py': [Errno 2] No such file or directory
Can anybody point out what am I doing wrong?
Your error message does not come from your script, but from the called one.
It sounds like the droidbox.sh script is not very smart and requires you to set the current working directory before you can call it.
I would typically use also some more variables, so you better see what belongs together:
#!/bin/sh
set -e
BASEDIR="/home/xxx"
DROIDDIR="$BASEDIR/DrroidBox_4.1.1"
APKFILE="$BASEDIR/a.apk"
cd "$DROIDDIR"
"$DROIDDIR/droidbox.sh" "$APKFILE"
If you dont use set -e you better combine commands which need to succeed together:
cd "$DROIDDIR" && "$DROIDDIR/droidbox.sh" "$APKFILE"
Otherwise the cd might fail when the directory is missing and all following commands execute in the wrong directory.
This error is because you're running the script in a different folder than the folder that houses your "scripts/droidbox.py" file. You can fix this in the following way(s):
Move the "scripts/" folder to the directory that you're running this script out of using "mv /path/to/scripts/ ."
Move your customer script to the folder that contains scripts/droidbox.py and run the script from there

Linux version of a .cmd file

I am creating a terminal program and cannot find out what the ending is for Linux. I know in windows it is .cmd. Any help would be great.
Thank you.
Yes, you can remove the .sh at the end and it should work, generally using ./cmd will get it to run. this goes for C programs as well. You do not need to give an extension for the object file, You could then add a path to your bash file and then you can execute it as a normal command.
Look here.
https://stackoverflow.com/a/8779980/2720497
You don't need a file extension on Linux, though typically, people use .sh (sh being short for 'shell').
You can run it one of two ways:
bash myscript.sh
or you can make the script itself executable and run it directly:
chmod a+x myscript.sh # make it executable
./myscript.sh # run it
Linux scripts' first line is typically #!/bin/bash which is the path to the specific shell used to run the script with the second method.

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

Resources