Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: (t)csh, set local echo off

28 views
Skip to first unread message

Janis Papanagnou

unread,
Aug 1, 2020, 4:26:54 AM8/1/20
to
On 01.08.2020 09:48, Michael Welle wrote:
> Hello,
>
> I want to read in a password from keyboard. Switching the local echo off
> for that would be a nice feature. All experiments with stty -echo or
> setty were not successful, neither on Linux nor on Solaris. With ksh,
> for instance, it works as expected. Any idea how to read in a password
> without local echo with (t)csh, please?

I don't know (t)csh but if it doesn't support suppressed echo (where I'm
surprised of) then I'd probably try the (t)csh equivalent[*] of

y=$( ksh -c 'stty -echo ; read x ; echo "$x" ; stty echo' )

i.e. use command substitution $(...) to execute the read procedure in a
shell that supports it and assign its value to a tcsh variable.

[*] Maybe it's something like using 'set', 'setvar', 'let' and/or using
the `...` backticks instead of $(...); you certainly know better how to
tranlate it to (t)csh.

Janis

> Regards
> hmw
>

Keith Thompson

unread,
Aug 1, 2020, 5:44:38 AM8/1/20
to
Michael Welle <mwe0...@gmx.net> writes:
> I want to read in a password from keyboard. Switching the local echo off
> for that would be a nice feature. All experiments with stty -echo or
> setty were not successful, neither on Linux nor on Solaris. With ksh,
> for instance, it works as expected. Any idea how to read in a password
> without local echo with (t)csh, please?

csh (if it isn't a symlink to tcsh) should allow stty -echo.

tcsh disallows changing some tty settings for your [in]convenience. The
"setty" builtin command might be able to override this, but in about 30
seconds of experiments I wasn't able to make it do so. "man tcsh" and
search for "setty".

Or just do this:

set p = `bash -c 'read -s -p "Password: " ; echo $REPLY'` ; echo ''

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Kenny McCormack

unread,
Aug 1, 2020, 9:49:30 AM8/1/20
to
In article <vqmfvgx...@news.c0t0d0s0.de>,
Michael Welle <mwe0...@gmx.net> wrote:
...
>ah, trees and forests ;). I though about just using read, but on most
>systems that is a shell built-in. Using the above is possible.
>
>I discovered that stty -echo would work in a csh-script, but not if I
>use it interactively (as I had done while testing).

Well, it sounds like you got it sorted, but just for the record:

% stty -echo ; echo "enter something: \c" ; set foo="$<";echo
% echo "foo = $foo"

works as expected, and afterwards, the terminal echoes as normal. This is
a Good Thing. This is a feature of tcsh - that each time, before it
prompts for interactive input, it fixes the terminal modes. This
eliminates the endless support calls that the makers of other shells
receive like "My terminal is messed up - I can't type anything - what do I
do?".

So, yes, it all works fine in a script, and it also works fine
interactively, once you understand how tcsh works.

--
Which of these is the crazier bit of right wing lunacy?
1) We've just had another mass shooting; now is not the time to be talking about gun control.

2) We've just had a massive hurricane; now is not the time to be talking about climate change.

Kaz Kylheku

unread,
Aug 1, 2020, 12:09:08 PM8/1/20
to
On 2020-08-01, Michael Welle <mwe0...@gmx.net> wrote:
> Hello,
>
> I want to read in a password from keyboard. Switching the local echo off
> for that would be a nice feature. All experiments with stty -echo or
> setty were not successful, neither on Linux nor on Solaris. With ksh,
> for instance, it works as expected. Any idea how to read in a password
> without local echo with (t)csh, please?

You can't really experiment with stty in the interactive mode of a shell
like Bash, because that keeps saving and restoring the TTY settings
between its own command prompt and the job it puts into the foreground.

All the stty actions you require have to be put into the same command
line to be run together in the same job. It will definitely work.

It's probably best to put this into its own script, which uses "trap" to
install a handler which restores the termios settings on exit or abrupt
termination.

The GNU coreutils stty has a -g option which produces the state of
termios encoded as a character string. The string can be passed as an
argument to stty again to restore the tty to those settings.

The Solaris stty has this also.

I have both in a Solaris 10 installation; the Solaris one reports a
larger string.

0:solarflare:~$ /usr/bin/stty -g
2d02:5:f04bf:8a3b:3:1c:7f:15:4:ff:ff:0:11:13:1a:19:12:f:17:16:0:0:1:1:0:00:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

0:solarflare:~$ /opt/csw/bin/stty -g
2d02:5:f04bf:8a3b:3:1c:7f:15:4:ff:ff:0:11:13:1a:19:12:f:17:16:0:0:0

If the -g option is available, it should be the preferred mechanism
for restoring the TTY settings.

Side remark:

There is an old C library function called getpass for doing this.

The Single Unix Specificatio marked it as "legacy" in the 1990's.

POSIX removed it in 2001.

I'm not aware of a command utility wrapper around getpass.

Kenny McCormack

unread,
Aug 1, 2020, 1:08:43 PM8/1/20
to
In article <202008010...@kylheku.com>,
Kaz Kylheku <793-84...@kylheku.com> wrote:
>On 2020-08-01, Michael Welle <mwe0...@gmx.net> wrote:
>> Hello,
>>
>> I want to read in a password from keyboard. Switching the local echo off
>> for that would be a nice feature. All experiments with stty -echo or
>> setty were not successful, neither on Linux nor on Solaris. With ksh,
>> for instance, it works as expected. Any idea how to read in a password
>> without local echo with (t)csh, please?
>
>You can't really experiment with stty in the interactive mode of a shell
>like tcsh, because that keeps saving and restoring the TTY settings
>between its own command prompt and the job it puts into the foreground.

FTFY.

bash works "as expected":

$ stty -echo
$ typestuffherebutwithnoecho
$ stty echo
things are back to normal.

As noted in my other post, only tcsh (IME) does the Right Thing here.

--
Mike Huckabee has yet to consciously uncouple from Josh Duggar.

0 new messages