I want to suppress "no access to tty" error message?

5,358

You can check whether stdin is a terminal before using stty:

if [[ -t 0 ]]; then stty -echo; fi
Share:
5,358

Related videos on Youtube

Redson
Author by

Redson

Updated on September 18, 2022

Comments

  • Redson
    Redson over 1 year

    I have a bash script where I use ssh to connect to another server and run some commands there. I found some sites that the error (Warning: no access to tty (Bad file descriptor). Thus no job control in this shell.) is a friendly message but my bash script gets "stuck". No other commands are executed after this message appears. Other forums said to use ssh -t to suppress the message but its not working for me. My code looks something like this:

    .
    .
    .
    stty -echo
    sshpass "pwd" ssh -o StrictHostKeyChecking=no [email protected] 'su -lc "rm -rf tmp"' 2>/dev/null
    stty echo
    .
    .
    .
    

    The reason why I'm using stty-echo is because I need to switch users to root and the password is displayed on the terminal (which I don't want). I get the error message (Warning: no access to tty (Bad file descriptor). Thus no job control in this shell.) after I enter the password for root on the connect server (ip: 1.1.1.1).

    Any suggestions? Let me know if further explanation is required. Thanks! (My version of bash is GNU bash version 3.2.51(1))

    EDIT

    The error message I get when removing 2>/dev/null is:

    Warning: no access to tty (Bad file descriptor).
    Thus no job control in this shell. 
    stty: standard input: Invalid argument
    
    • Redson
      Redson over 9 years
      @VolkerSiegel I did strip down the code as much as possible. The error message I'm getting is with this code. I'm unsure of what you mean
    • Volker Siegel
      Volker Siegel over 9 years
      Sorry, I meant as much as possible so that the error still occurs. I think the problem will not go away if you remove the ` 2>/dev/null`? But it may hide messages that would help us!
    • Redson
      Redson over 9 years
      @VolkerSiegel I will edit the question to give the message I get without 2>/dev/null
    • slm
      slm over 9 years
      Adding a -T to your ssh command change things? ssh -To ....
    • Volker Siegel
      Volker Siegel over 9 years
      How do you run your script? Do you run it in a terminal?
    • cuonglm
      cuonglm over 9 years
      @Alias: Does it run when you don't use sshpass, but use ssh -t?
    • Redson
      Redson over 9 years
      @slm Still getting the same error
    • Redson
      Redson over 9 years
      @VolkerSiegel I am running the script in a terminal
    • Volker Siegel
      Volker Siegel over 9 years
      I think it may depend on what the input of the script is connected to, so what is the exact command?
    • Redson
      Redson over 9 years
      @Gnouc Still getting the same error
    • Redson
      Redson over 9 years
      @VolkerSiegel Could you clarify? Its a bash script running on the terminal of suse linux enterprise
    • Mark Plotnick
      Mark Plotnick over 9 years
      Do you have the access to create an alternative superuser account on the remote, one that doesn't have csh or tcsh as its shell? I'd like to try to eliminate the cause of error messages rather than suppress all remote error messages, because some error messages you may actually want to see.
    • Volker Siegel
      Volker Siegel over 9 years
      It could be that you run it like ./myscript.sh, or echo foo | myscript.sh | less, for example?
    • Redson
      Redson over 9 years
      @MarkPlotnick I'd to eliminate the problem as well. Unfortunately I don't have access to create a superuser. The only superuser is root. What are csh or tcsh?
    • Redson
      Redson over 9 years
      @VolkerSiegel I run the script like this: ./myscript.sh
    • Mark Plotnick
      Mark Plotnick over 9 years
      csh and tcsh are shells, like bash but different. They are mostly used by older BSD systems, and rarely on Linux. The error message Warning: no access to tty (Bad file descriptor) is coming from them. It looks like either root on the remote side has csh or tcsh as its shell, or one of root's init files is running a csh or tcsh script.
    • Mark Plotnick
      Mark Plotnick over 9 years
      I don't know of an automated way to translate csh/tcsh init files to bash init files. You need to do it by hand. (Luckily, they all have different names - .login, .tcshrc, .cshrc, .profile, .bash_profile, .bashrc) so you won't need to overwrite anything). Converting them may merit a separate question here if you need assistance beyond the documentation. Then, after that, the command to change change the shell depends on the OS; is the system with tcsh Linux, FreeBSD, Windows running Cygwin, or something else?
    • SHW
      SHW over 9 years
      Is /dev/pts mounted ?