Reputation: 49
I want to run a bash script on a mac remotely from a batch script on a windows machine. On Windows I have this:
@echo off
echo bash /applications/snowflake/table-updater/test2.sh; exit>tmp_file
putty -ssh User@remote_machine -pw password -m tmp_file
And here is test2.sh on the remote machine
#!/bin/bash
# test2.sh
#
#
7za x table-apps.zip -y -o/Applications/snowflake/applications
When the batch file runs it logs in successfully, but for some reason fails to run the bash file. However the bash file runs fine from mac terminal, where it unzips the files perfectly. What could be happening here?
Please note test2.sh is actually in Applications/snowflake/table-updater as specified in the batch file. And the tmp file does write fine as well. My aim is to have a script to access a further 10 remote machines with the same directory structure.
Thanks in advance
Upvotes: 1
Views: 868
Reputation: 189307
The standard program which resembles the scriptable Unix command ssh
in the PuTTy suite is called plink
, and would probably be the recommended tool here. The putty
program adds a substantial terminal emulation layer which is unnecessary for noninteractive scripting (drawing of terminal windows, managing their layout, cursor addressing, fonts, etc) and it lacks the simple feature to specify a command directly as an argument.
plink user@remote_machine -pw password /Applications/snowflake/table-updater/test2.sh
From your comments, it appears that the problem is actually in your script, not in how you are connecting. If you get 7za: command not found
your script is being executed successfully, but fails because of a PATH
problem.
At the prompt, any command you execute will receive a copy of your interactive environment. A self-sufficient script should take care to set up the environment for itself if it needs resources from non-standard locations. In your case, I would add the following before the 7za
invocation:
PATH=$PATH:/Applications/snowflake/table-updater
which augments the standard PATH
with the location where you apparently have 7za
installed. (Any standard installation will take precedence, because we are adding the nonstandard directory at the end of the PATH
-- add in front if you want the opposite behavior.)
In the general case, if there are other settings in your interactive .bashrc
(or similar shell startup file) which needs to be set up in order for the script to work, the script needs to set this up one way or another. For troubleshooting, a quick and dirty fix is to add . /Users/you/.bashrc
at the top of the script (where /Users/you
should obviously be replaced with the real path to your home directory); but for proper operation, the script itself should contain the code it needs, and mustn't depend on an individual user's personal settings file (which could change without notice anyway).
Upvotes: 2