How to resolve dependecies while running a shell script remotely? - linux

I'm trying to run a shell script remotely. I'm having a resource(.txt file) in one machine and a shell script in another machine, but in the same network. The script resides in a particular directory and the script takes some binary files that resides in some other directory everything in the target machine. Now, I'm not able to run the script remotely. But I can run a simple shell program which does not have any local dependencies. Can you please suggest a solution to resolve this problem?
Here is the command that I used for running a shell-script-without-any-dependencies.
cat input.txt | ssh clrg#192.168.2.22 "sh path/to/shell/script/tokenize.sh"
On running a script with dependencies, I got the following error,
"ambiguous redirect"
../../deter-4/bin/determine: No such file or directory
../../deter-4/bin/determine1: No such file or directory
Now, how can I tell to accept dependencies while running the shell script

I think your problem is the working directory. Try something like this:
cat input.txt | ssh clrg#192.168.2.22 "sh -c 'cd /path/to/shell/script; ./tokenize.sh'"
Your script seems to expect to be run from a specific working directory only. The error messages you mentioned in your comment about the relative paths (starting with ../../) at least suggest that.

Related

Using SSH to execute a command - $PATH is fine but running perl scripts fails?

Going from a Linux host to another Linux host
Say I run:
ssh user#server ' . /etc/profile; /path/to/myScript.pl'
I always get errors involving scripts within that perl script like...
/path/to/otherscript.sh was not found No Such File or Directory
-even though it's obviously there. Running this script locally on "server" works just fine. What's also confusing is that the output of....
ssh user#server ' . /etc/profile; echo $PATH'
...looks EXACTLY the same as echo $PATH when running on "server" locally.
Any ideas as to why this is not working? I do not have permissions to modify the perl script to always* include the complete path to the files listed.
if it's useful this is running with a shebang of #!/usr/bin/env perl - reading up on it now, would this alter my path?**

Perl script in linux environment is not working via cron

When I try to run a Perl script which is called via Linux script manually it works fine but not executable via CRON.
Linux_scrip.sh conatains Perl_script and the command is,
perl_path/perl Perl_script.pl
I got perl_path using which perl command.
Can anyone suggest why is it not executable via CRON entry.
Most likely suspects:
Current work directory isn't as expected.
Permission issues[1].
Environment variables aren't setup as expected.
Requirement of a terminal can't be met.
See the crontab tag wiki for more pitfalls and some debugging tips.
The first thing you should do is to read the error message.
This isn't likely to be an issue for you own cron job, but I've included it since it's quite a common problem for scripts run from other services.
Most probable cause is current working directory.
Before perl command execution, write a command to change directory.
Something like :
cd perl_path;
perl Perl_script.pl

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

sh script with scp variables

I am trying to writing a simple script which will figure out the latest version of a file by the filename and then download that file to the local computer.
What I cant figure out is why my code will work in the shell, but not work when I run it as a script. I am also running my script on cygwin, not sure if that will make a difference.
Here is the script
#!/bin/sh
x=$(ssh user#hostname 'ls -r -t /vgf/day1*.gif | tail -1')
echo $x
scp user#hostname:"${x}" /images/day1.gif
x is correctly assigned, but when I get to the scp command I receive something along the lines of
: No such file or directoryif
However if I run the scp command in the shell it will work
$ sh download.sh
/vgf/day1.gif
: No such file or directoryif
$ scp user#hostname:"${x}" /images/day1.gif
day1.gif 100% 22KB 22.1KB/s 00:00
I would be open to different solutions. If I could prevent the version increasing via some linux administration, I may follow that route, although I am still wondering what the problem is here.
By version, I mean day1_001.gif and the new version becoming day1_002.gif and so on. So when the file saves a file day1.gif, it will overwrite the original without creating another version.

Execute Script using an Alias

I am trying to create an alias that will execute a script. When i cd into the directory where the script is located... lets say /usr/local/bin/startscript then the script runs as expected and starts the application i want it to.
SO. i went into my bashrc file and added an alias
alias startscript='/usr/local/bin/startscript'
The goal is to be able to run the script by simply typing "startscript" from any directory.
However, when i try to use the alias to run the script, it does not work properly as the application that should start, does not.
My script starts with
#!/bin/sh
and then goes from there
any ideas? Thanks
SCRIPT:
#!/bin/sh
#- Check for user 'user'
if [[ "`whoami`" != "user" ]]; then
echo "This script can only be executed by user 'user'."; exit
fi
. /usr/local/bin/etctrx/startscriptdirectory/startscriptsetup
#- Kill manager to avoid multiple processes
pkill -f 'JavaApp.jar'
#- Start
nohup java -classpath /usr/local/bin/etctrx/startscriptdirectory/RequiredJars/ojdbc5.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/activation.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/mail.jar -jar /usr/local/bin/etctrx/startscriptdirectory/JavaApp.jar > ${JAVAAPPLOGS}/startscript.log 2>&1 &
If the script runs as expected while in /usr/local/bin by simply typing startscript, but from another directory the script runs (does not return an error), but doesn't produce the desired results, then the issue is with how you reference the application from within the script.
As others have noted, you shouldn't need an alias for something in /usr/local/bin and if it runs from that directory, obviously your executable permissions are correct too. If the application you're trying to run is also in /usr/local/bin then your script probably assumes it's in the same directory, which wouldn't be the case elsewhere, so you would need to either ad a cd to /usr/local/bin within the script or specify the full application path.
I am able to call the script if i do this, but it still won't give me the
results I want,(application being started) like i do when I run the script from
the directory it lives in
It would appear that the "application" in question is in the same directory as the script, /usr/local/bin, which we have established is already on your PATH. For the script to run correctly but not the application means you might be calling the application wrong, for example
./application
This would fail unless you were calling from /usr/local/bin. Fix would be like this
application

Resources