표준 입력을 사용할 때 현재 "stty -g" 설정을 저장할 수 있습니까?

표준 입력을 사용할 때 현재 "stty -g" 설정을 저장할 수 있습니까?

소비하고 있지만 이에 대해 불평하는 stty스크립트의 현재 설정을 저장한 다음 복원하고 싶습니다 .stdinstty -g

stty: '표준 입력': 장치에 부적절한 ioctl

stdin파일 설명자를 닫고 stty재정의된 FD가 있는 하위 쉘을 호출해 보았습니다. stdin나는 분리하는 방법을 모르고 stty -g도움이나 조언을 원합니다.

저는 특히 POSIX 호환성에 관심이 있습니다. Bash/Zsh-isms를 사용하지 마십시오.

문제를 재현하기 위한 최소 스크립트:

#!/usr/bin/env sh

# Save this so we can restore it later:
saved_tty_settings=$(stty -g)
printf 'stty settings: "%s"\n' "$saved_tty_settings"

# ...Script contents here that do something with stdin.

# Restore settings later
# (not working because the variable above is empty):
stty "$saved_tty_settings"

print 'foo\nbar\n' | ./sttytest오류를 보려면 실행하세요 .

답변1

@icarus의 댓글:

아마도 saved_tty_settings=$(stty -g < /dev/tty)?

실제로는 올바른 방향을 가리켰지만 그것이 이야기의 끝은 아닙니다.

다음 상황에서는 동일한 리디렉션을 적용해야 합니다.다시 덮다 stty상태도 마찬가지다.그렇지 않으면 여전히 Invalid argumentioctl 오류가 발생합니다.다시 덮다단계...

올바른 접근 방식:

saved_tty_settings="$(stty -g < /dev/tty)"

# ...do terminal-changing stuff...

stty "$saved_tty_settings" < /dev/tty

이것은 제가 테스트한 실제 스크립트입니다. 전체 프로세스를 클래식 Bourne 셸 스크립트로 다시 작성했습니다.

#!/bin/sh
# This is sttytest.sh

# This backs up current terminal settings...
STTY_SETTINGS="`stty -g < /dev/tty`"
echo "stty settings: $STTY_SETTINGS"

# This reads from standard input...
while IFS= read LINE
do
    echo "input: $LINE"
done

# This restores terminal settings...
if stty "$STTY_SETTINGS" < /dev/tty
then
    echo "stty settings has been restored sucessfully."
fi

테스트 실행:

printf 'This\ntext\nis\nfrom\na\nredirection.\n' | sh sttytest.sh

결과:

stty settings: 2d02:5:4bf:8a3b:3:1c:7f:15:4:0:1:ff:11:13:1a:ff:12:f:17:16:ff:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
input: This
input: text
input: is
input: from
input: a
input: redirection.
stty settings has been restored sucessfully.

Debian Almquist Shell dash0.5.7 및 GNU Coreutils 8.13 사용 stty.

관련 정보