7

I am having troubles with a bash script. I am trying to automate the process of sending files to my raspberry pi through ssh. I want to prompt for a path to a file/directory, then copy it to the /home/pi/push directory on my pi. I then want to ask if there is another file to send, and if yes then loop back again, otherwise exit the program. I zeroed out the IP address for obvious security reasons.

done=0
while [ $done -lt 1 ]
do
    read -r -p "Path to file: " path
    spawn scp -r $path pi@000.00.000.00:/home/pi/push
    expect "assword:"
    send "password\r"
    interact

    read -r -p "Send another? [y/N] " response
    if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
    then
        $done=1
    else
        echo "Ending file transfer."
    fi
done

If you have suggestions on a better way to achieve this, that would be great as well!

Tony Mottaz
  • 525
  • 1
  • 7
  • 10
  • 5
    Please take a look: http://www.shellcheck.net/ – Cyrus Feb 22 '16 at 22:47
  • 4
    The `spawn` here worries me -- that's an `expect` keyword, not a shell keyword. You can't mix the languages the way you're trying to do here -- they're two very different interpreters, with two very different syntaxes. – Charles Duffy Feb 22 '16 at 22:51
  • 2
    Good point @CharlesDuffy. Take a look at setting up [public key authentication to your Pi](https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md) to avoid the password entry. – miken32 Feb 22 '16 at 22:55
  • 3
    The reason for the immediate failure is that the script is run with `sh` instead of `bash`, by the way. – that other guy Feb 22 '16 at 23:18
  • Thanks for the heads up @CharlesDuffy. I did not know `expect` was a whole other thing. I'll make the appropriate changes! – Tony Mottaz Feb 23 '16 at 03:33

1 Answers1

0

Just use an or in the test, or escape the parentheses in the regular expression. Also, there's no need to test for case with nocasematch set; and with that requirement out of the way, you don't even need the regular expression.

shopt -s nocasematch
...
if [[ "$response" = "yes" || "$response" = "y" ]]
miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    The POSIX-standard way to do that check would be with a `case` statement. `case $response in [Yy][Ee][Ss]|[Yy]) done=1 ;; *) echo "Ending file transfer." ;; esac` – Charles Duffy Feb 22 '16 at 22:50
  • 1
    ...that said, there's a much bigger issue here in that the OP is trying to use expect syntax in a shell script. – Charles Duffy Feb 22 '16 at 22:52
  • 1
    Thank you so much, @miken32! Works like a charm. Can you explain to me why the old way did not work? I'm just starting to learn this stuff. – Tony Mottaz Feb 23 '16 at 03:36
  • @AnthonyMottaz the parentheses are special characters and need to be escaped with a backslash. I find it easier to read when things are split out. – miken32 Feb 23 '16 at 03:43
  • Now I have the same problem, and I need to use regular expressions, which I can't enclose in a simple value check. How should I fix it? – Leukonoe Mar 07 '17 at 23:28
  • @Leukonoe I'd start by asking a question on this site. – miken32 Mar 08 '17 at 00:25