Mulesoft cannot run a shell script - java

I am trying to run a shell script in a flow in Mulesoft's Anypoint Studio.
While I can get some basic shell to run I cannot get my script to run. This is what my script looks like:
#!/bin/ksh
export JWHome=${pwd}
export LOG4J_JAR=$JWHome/lib/log4j-1.2.7.jar
export CLASSPATH=$JWHome/lib/java.jar:$LOG4J_JAR:$JWHome
I set the above script as a payload and execute it from a Groovy script by running:
payload.execute().text
When I run this script I get the following error:
java.io.IOException: Cannot run program "#!/bin/ksh": error=2, No such file or directory
I get the same error when I remove "#!/bin/ksh" - For example when I run Export to set a variable.
How can I get this to run?

I'm assuming that what you did -you didn't provide enough details- is to use a Groovy script to execute the content of a string in the payload using the execute() method. That is failing because a shell script is not a single command. Only a shell can interpret it, but not as command line.
You can try writing the shell script content to a file (example: myscript.sh, or use a temporary name), then execute the shell script by name ("myscript.sh".execute()). You will probably need to add the path to the file is correct otherwise the execute() method may not find it.

Related

How do i execute a shell script in eclipseIDE?

i want to execute a sheel on my Eclipse, and am unable to do so, is it because eclipse does not support shell script or do we any plugin for it.
I am executing it like this, and Name is Argument than i am passing to my script as search pattern.
$ String[] commands = {"C:/Users/shreyas.a.ramesh.DIR/workspace/Shellweb/src/Search_file.sh", Name};
And error I get is as follows
java.io.IOException: Cannot run program
"C:/Users/shreyas.a.ramesh.DIR/workspace/Shellweb/src/Search_file.sh": CreateProcess error=2, The system cannot find the file specified
Sorry about the code indentation.
This isn't a Java or Eclipse problem. Shell scripts aren't executable binaries. You'll have to run the shell binary itself with whatever arguments it needs to interpret your script.

Translation of java commands in Linux systems

I am currently using a linux system to execute a specific command that translates into another java command (java - jar)
For example:
when i try execute /usr/bin/a in terminal, it will read the /usr/bin/a command and translates into 'java -jar' command
I do not want to execute 'java -jar' command directly and i would to specify a full path in executing the command so is there a possible way to achieve this without using a script like /.sh?
Things that i have attempted:
i have tried using the alias command in bashrc files
for example in bashrc file:
alias /usr/bin/a='java - jar'
but when i try to source the bashrc files, it gives me an invalid alias.
I know i can use a /.sh script to execute the command but that is not my intention to do it.
Have you tried to write a bash script with java -jar and execute it ?
You test.sh file will contain:
java -jar test.jar
And after that you can run
./test.sh
And offcourse you can put to bin directory or create an alias for that bash file

Using maven plugin exec-maven-plugin to run a shell script

I have a java web application project. I am using the exec-maven-plugin to execute a shell script which creates a small txt file in a directory when the project is built using mvn clean isntall. I have confirmed that the script is being executed when running mvn clean install by writing test text. However, the script is not creating the txt file.
When the script is run normally through the terminal, ie ./script.sh , the txt file is created correctly.
toTxt="hello"
printf $toTxt > testText.txt
echo 'This shows up, but testText is not created.'
Does anyone know the reason why this is happpening?
Try either echoing pwd command to see where it should create the file or add <workingDirectory>${project.build.outputDirectory}</workingDirectory>
to your configuration block in pom.xml
NOTE: You can specify something other than ${project.build.outputDirectory} to specifically point to the right place. And make sure you have write permissions to it.
Keep in mind that some commands are not really external programs, but shell built-ins. This means that they don't launch programs, and maven-exec-plugin will not be able to run them. However, you can run bash to get the result, just not pwd.
bash -c "echo $(pwd)"
should do the trick. Maven is launching bash which is then running the script echo $(pwd) which calls the bash builtin function pwd and passes the result back (due to the echo).
Yes, it's a lot of work to get around the lack of a pwd program, but that's because there really isn't a pwd program, it's part of the internal offerings of bash.
http://www.tldp.org/LDP/abs/html/internal.html lists the bash internals (builtin commands).

Execute a shell script (shell script which uses perl and other shell scripts) in Linux from java

I am trying to execute a shell script (that makes use of a perl script and other shell scripts) from java program, however I am not successful, here is what I tried:
On the Linux server, in a folder test1/testJava the following scripts are available:
decode24.sh (the main script I call from java program, this script makes use of the other scripts listed below)
fram.sh
shadow.pl
light.sh
Here is what I try from java:
try {
ConnBean cb = new ConnBean("xx.yyy.zz.p", "user","passme");
ssh = SSHExec.getInstance(cb);
CustomTask ct1 = new ExecShellScript("/test1/testJava", "./decode24.sh", "arg1");
ssh.connect();
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
}catch......
Result after execution:
error message:
./decode.sh[17]: shadow.pl: not found [No such file or directory]
./decode.sh[21]: fram.sh: not found [No such file or directory]
The error comes from your decode.sh script. It cannot find the shadow.pl and fram.sh scripts that it executes. Probably because the CWD or path is not set to the script dir when you run the script.
The most robust way to solve it is to change your decode.sh script to use absolute paths to the shadow.pl and fram.sh scripts. That way you can execute decode.sh from any directory.
If you use bash, check out this question for a neat way to resolve the absolute directory to the scripts to avoid hard coding the path.
Let decode.sh find out the directory it is in (see bash solution) and use the path for calling the others.

not able to Execute shell script containing executable through android app

I have designed one android application in which i want to invoke shell script which in turn executes one executable(i have place both(script and executable) of them in assets folder).
My executable takes one argument as hex number
when i executes shell script through adb shell from my linux(ubuntu) manually, it is working fine but when application does try to execute that executable through script it is not working(not giving even error message).
here is script part;
/data/data/com.example.test/program 0x950000
Thanks in advance.

Categories

Resources