bash script won't execute terminal command - bash

I have written a simple bash script shown below:
#!/bin/bashhere
/net/account/admin/george.chilumbu/nmon2influxdb import /data1/nmon/log-file.nmon
But when i execute the script, i get the following error message
2016/05/25 18:05:18 TimeStamp T0007 not found
However, when i try to run the command directly in the terminal, it runs just fine. Not sure what is the issue here. I even tried putting the command in a function and then calling that function, but that did not seem to work.

Related

MacOS - Script works when run in shell but not when opened with terminal

I am trying to write a script for a Mac which will run each .jar file in its directory sequentially. I used a for loop to iterate over the files like so:
for i in *.jar; do
java -jar "$i" "$#"
done
This works perfectly on my Linux machine and when I invoke it from the shell using ./runJars.sh, but when I run the script by using Open with>Terminal, it fails.
When opening it with Terminal, the loop runs once and java outputs an error stating that it cannot load the file *.jar, so I have concluded the file expansion is not occurring for some reason when it is opened with the Terminal. Everything before that runs correctly.
I have tried adding a line files=*.jar then looping over $files, but that doesn't work either. I have tried looping over $(ls *.jar), but that fails if the file name has embedded spaces.
What I would like to know is why this fails in the Terminal and how I could fix it.

Problems running bash script from incron

I have a simple incron task setup to run a command whenever a particular .json file is written-to, then closed.
/var/www/html/api/private/resources/myfile.json IN_CLOSE_WRITE,IN NO LOOP /var/www/html/api/private/resources/run_service.sh
I can see that whenever the file to written to, there is a syslog entry for the event, and the command that was triggered - along the lines of <date> - incrond: CMD (/var/www/html/api/private/resources/run_service.sh).
But nothing seems to happen...
initially I thought this would be caused by an issue with the script, but replacing the script command to something simple such as echo "hello world" > /tmp/mylog.log still yields no output or results. I seem to have hit a brick wall with this one!
Update
Changing the incron command to read "/bin/bash /var/www/html/api/private/resources/run_service.sh" now seems to triggering the script correctly, as I can now get output from the script.
A simple mistake on my part, despite all examples online showing that using the script as the command should run it, for me it only works if I explicitly call bash to execute it
"<my directory/file to watch> <trigger condition> /bin/bash /var/www/html/api/private/resources/run_service.sh

How to extract information from a command prompt using shell script?

I need a shell script using which I can fetch data from command prompt. I need to fetch data from the command prompt of a router. When I write commands in a shell script it goes the prompt but not executing the next command. So running the script just stuck in the prompt. Bellow is my script file
#!/bin/sh
ccli
rsc
where ccli is the command to enter the prompt and rsc is the command to fetch some infomation.
So please suggest some method.
If ccli reads commands from stdin (which I don't know), you might get further with
printf 'rsc\n' | ccli
For more complicated tasks I suggest you look into expect which was invented for the sole reason of driving interactive programs in a scripted way.

How to link shared library in shell script?

I have wrote a simple shell script where I have only mentioned the following line
export LD_LIBRARY_PATH=/home/lib/
I want to run one program for which I have to link with this library ,before running the program I am running this shell script ,but after this the program is not working it showing the linking error and when I am doing following line it showing nothing
echo $LD_LIBRARY_PATH
but,when I am doing it in shell normally ,it is working.
Can any one tell why this shell script is not working.what is the concept behind it.
Thanks
If you want to run a script for the purpose of modifying environment variables you need to source the script rather than run the script. Running the script starts a new instance of w/e shell is used to run the script, when it returns, all environment variables are back to the way they were before you ran it. Doing "source script.sh" actually runs the commands in the script in your current shell.

Syntax Error when executing shell script with java

I have a shell script which I want to execute within a Java class. The shell script takes an input file and produces an output file from it.
To execute it, I use apache commons exec. Here's my little code:
CommandLine cmdLine = CommandLine.parse("/bin/sh ./generate_void_description.sh ./n3file ./voidfile");
int exitValue = executor.execute(cmdLine);
As a Result i get this Error Message :
./generate_void_description.sh: 44: ./generate_void_description.sh: Syntax error: "(" unexpected
org.apache.commons.exec.ExecuteException: Process exited with an error: 2 (Exit value: 2)
Of course I can read and I understand that the message means that there's a syntax Error in the script but.
1) I didnt write the script and I'm not familiar with bash script syntax
2) The script works perfectly without any errors when I run it on a terminal within ubuntu
Has it something to do with User rights ? I need to create an output file maybe I don't have the permissions to do this if if
The script uses bash features, most noticeably an array in line 44. You'll have to execute the script using /bin/bash, not /bin/sh.
Seems like the bash script is exiting with a return code of 2. If that's normal, try this before making the execute call.
executor.setExitValue(2);

Resources