Sunday 26 February 2012

BASH - result of remote command into local variable

If you like to code in BASH this tip may be interesing for you. It is possible to call a remote command in BASH for example through SSH and put it's result into a local variable. For example you can run your script by CRON on local machine and login to remote server through SSH to check if some file appears in given directory.

It's other problem to choose a login method - by pass entered into script code or by chenge of SSH certificates keys - better is to not put password into script because of security reasons. 


To put result of remote command into a local variable in BASH use this construction:

result=$(ssh login@host remote_command)
For example:
result=$(ssh login@host ls -all|grep searched_file.txt)


Article on CC-BY-SA-3.0