1

Do not understand why my code works if I take out my loop and variables while manually executing each line. First I thought my variables were wrong, but then I tested my code with the variables but no loop and it worked.

If I put back in the loop (the only thing I'm changing), I get these weird stty errors.

while read p; do
    #Send file
    scp random_file.txt $p:/me/folder"

    #Log in
    ssh $p"@myserver.txt"

    #List file, extract file, append file
    #Code here

    #log out
    exit

done <usernames.txt

I've googled this error (which is a pretty common error) ad nauseam, but none of the solutions are working. Disabling pseudo-tty allocation nor forcing pseudo-tty allocation work for me. I always get an error, no matter the option

-t -t option

tcgetattr: Inappropriate ioctl for device

-t option

Pseudo-terminal will not be allocated because stdin is not a terminal.    
stty: : Invalid argument

-T option

stty: : Invalid argument

So how do I get around these stty errors and why does it stop working when I put it in a loop?

MrPickles
  • 1,085
  • 1
  • 15
  • 31
  • 1
    First glance, `stty` looks like a cuss word. :-o – Fiddling Bits Jul 21 '14 at 14:53
  • 1
    ssh while interactive drops you into a remote shell. ssh while in a script does not do that. The body of your loop after the ssh line is **not** happening on the remote system when scripted this way. It is happening locally. – Etan Reisner Jul 21 '14 at 14:56

2 Answers2

1

The input redirection with <usernames.txt is replacing the standard input with the file usernames.txt. Hence the terminal is no longer the input, causing these errors. One way around this is to use a file descriptor other than standard input, e.g.:

while read p <&3; do
    …
done 3<usernames.txt

Another problem you have is that the commands within the loop are executed locally, not over ssh on the remote machine, so the exit will exit your local shell (after you return from ssh by manually logging out). You can put commands to execute remotely on the ssh command line (see ssh manual, or, e.g., this question), which may eliminate your need to have the terminal as standard input in the first place.

Community
  • 1
  • 1
Arkku
  • 37,604
  • 10
  • 57
  • 79
  • Thank you @Arkku. I tried passing the argument to the `ssh` command by using `<< EOF ... EOF done 3 – MrPickles Jul 21 '14 at 15:52
  • @MrPickles If you use the heredoc then that will be the standard input of the `ssh`, and the pseudo-terminal message is correct. Prevent `ssh` from allocating a pseudo-terminal (it is not needed since you won't be using it interactively). IIRC this option is `-T`. – Arkku Jul 21 '14 at 18:58
1

ssh while interactive drops you into a remote shell. ssh while in a script does not do that. The body of your loop after the ssh line is not happening on the remote system when scripted this way. It is happening locally.

If you want to run code on the remote machine in the context of that ssh connection then you need to write it all as the command argument to the ssh command and/or write a script on the remote machine and execute that script as the ssh command argument.

Etan Reisner
  • 68,917
  • 7
  • 78
  • 118