0

I want to run my shell script on a remote machine and I need user want to give value for some variable for that we use Read command while run script on local machine but I use the same command while run the script on remote machine but it's not working for me. How to use Read command for run script on remote machine?

sample code :

restore (){
        echo "Restore"
}

deploy () {
        echo "Deploy"
}


echo "Select any option to proceed..."
read opt
case $opt in

        restore)
        restore;
        break;;

        deploy)
        deploy;
        break;;
esac

This code work fine for when run scripts on local machine.

Thanks in advance !

  • 1
    In what way is it not working? Please edit your post to include the output. – Mark Smith Dec 23 '15 at 12:36
  • While running the shell script I can't give the option to select any one of the above options. The shell scripts ends – Jeevanantham Mathavaraj Dec 23 '15 at 12:54
  • this can help. You should use a parameter instead of select option with user interaction. – Lety Dec 23 '15 at 13:44
  • You don't need to use named FIFOs to redirect the standard output of one command to standard input of another You can just use anonymous pipes like so: cat FILES... | ssh [...]. If you only have one file to redirect, you don't even need cat and a pipe. Just redirect the file directly to the target process: ssh [...] < FILE. (See muru's answer for a reason, why neither work here.) – David Foerster Dec 24 '15 at 9:37
4

If you tell bash to read commands from stdin, which is what the -s option does, and then redirect its stdin, you cannot provide input via stdin any longer.

You should it as a normal script:

ssh root@192.168.1.116 bash /root/BackupShellScript/remote.sh

And source /usr/local/etc/BackupShellScript/configuration.conf from your script, instead of hacking your way around with cat and process substitution.

1

That script doesn't work even locally - never mind with ssh.

The problem is just that you put your result into option and then use case $opt. So change option to opt and all is well.

#!/bin/bash

restore (){
        echo "Restore"
}

deploy () {
        echo "Deploy"
}

echo "Select any option to proceed..."
select opt in "deploy" "restore" ; do
case $opt in

        restore)
        restore;
        break;;

        deploy)
        deploy;
        break;;
esac
done
  • 1
    I change option to opt in select option in "deploy" "restore" ; do . After that also it is not working for command ssh root@192.168.1.116 "bash -s" < <(cat /usr/local/etc/BackupShellScript/configuration.conf /root/BackupShellScript/remote.sh) – Jeevanantham Mathavaraj Dec 23 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.