'-sh: 구문 오류: 임베디드 Linux 장치에서 프로세스 대체를 위해 'bash'를 사용하려고 할 때 예기치 않은 '(''

'-sh: 구문 오류: 임베디드 Linux 장치에서 프로세스 대체를 위해 'bash'를 사용하려고 할 때 예기치 않은 '(''

이는 다음을 사용합니다."프로세스 대체"( <()) 그리고"흐리독"( cat << EOF...EOF)는 새로운 bash 프로세스를 열고 여기에 --rcfileinclude를 실행합니다 alias foo="echo hey you.

bash --rcfile <(
cat << EOF
alias foo="echo hey you"
EOF
)

여기에서 볼 수 있듯이 우분투에서는 잘 실행됩니다.

$ bash --rcfile <(
> cat << EOF
> alias foo="echo hey you"
> EOF
> )
$ foo
hey you
$ alias
alias foo='echo hey you'

그러나 일부 임베디드 Linux 장치에서 이 프로그램을 실행하려고 하면 다음과 같은 오류가 발생합니다. 왜? 거기에서도 실행되도록 하려면 어떻게 해야 합니까?

-sh: syntax error: unexpected "("

전체 출력:

$ bash --rcfile <(
-sh: syntax error: unexpected "("
$ cat << EOF
> alias foo="echo hey you"
> EOF
alias foo="echo hey you"
$ )
-sh: syntax error: unexpected ")"

도움이 된다면 Linux 장치가 내장된 Ubuntu에서의 출력은 다음과 같습니다 which bash.bash --version

# 1. Ubuntu

$ which bash
/bin/bash
$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


# 2. Embedded Linux device

$ which bash
/bin/bash
$ bash --version
GNU bash, version 5.0.16(1)-release (aarch64-buildroot-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

반복하지 않음:

이것은 관련이 있지만아니요중복된 것 같습니다.프로세스 대체를 사용할 때 Dash가 "구문 오류: "(" 예기치 않은" 오류를 보고합니다..

관련된:

  1. [나만의 질문]https://stackoverflow.com/questions/69891328/what-is-the-syntax-in-shell-bash-and-how-do-i-search-for-it

자신에 대한 메모

나의 궁극적인 목표는 SSH 로그인 시 다음과 같이 일부 사용자 정의 별칭을 자동으로 생성하는 것입니다.

ssh -t username@ip_address '/bin/bash --rcfile <(
cat << EOF
alias foo="echo hey you"
EOF
)'

업데이트 완료! 이것이 내가 생각해낸 것입니다:

# Store your password into a file
echo "my_password" > ~/pw

# Manually add something like this to your ~/.bash_aliases (recommended) or ~/.bashrc file on the PC
# you are ssh-ing FROM:
alias gs_ssh="sshpass -f ~/pw scp /etc/skel/.bashrc [email protected]:/tmp \
&& sshpass -f ~/pw ssh -t -o 'ServerAliveInterval 60' [email protected] 'bash --rcfile /tmp/.bashrc'"

여기에서 내 저장소를 확인하세요.https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/home/.ssh#Optional-but-recommended-alias.

답변1

임베디드 Linux 장치는 /dev/fd/항목을 지원합니까? 이렇게 하려면 다음과 같이 파일 설명자 #3을 통해 초기화 명령을 전달해 보세요.

bash --rcfile /dev/fd/3 3<<EOF
alias foo="echo hey you"
exec 3<&-
EOF

편집: Stéphane Chazelas는 이것을 exec 3<&-"스크립트"의 마지막 명령으로 추가할 것을 제안했습니다. 이렇게 하면 파일 설명자가 닫혀서 전체 쉘 세션 동안 멈추지 않습니다.

답변2

명령 구문이 지원되는지 여부는 현재 실행 중인 셸에 따라 다릅니다.

현재 쉘은 명령을 sh실행하려고 할 때 발생하는 것이며, 쉘이 로그인 쉘로 실행되는 쉘로 bash --rcfile <(...)자신을 식별한다는 것은 오류 메시지에서 분명합니다 . sh쉘은 일반적으로 프로세스 대체를 이해하지 못합니다.

이 문제를 해결하는 한 가지 방법은 먼저 exec bash현재 셸을 프로세스 대체를 이해하는 셸로 바꾸거나 다음을 수행하는 것입니다.Gordon Davidson이 제안한대로대체 파일 설명자를 통해 임시 초기화 파일을 제공합니다.

exec bash

bash --rcfile <( cat <<'BASHRC'
alias foo='echo bumblebee'
BASHRC
)
bash --rcfile /dev/fd/3 3<<'BASHRC'
alias foo='echo bumblebee'
exec 3<&-
BASHRC

두 경우 모두 새 쉘이 문서를 읽기 전에 문서를 확장하기 위해 현재 쉘이 필요하지 않은 한 여기서 문서를 참조하는 것이 가장 좋습니다 bash. exec bash현재 쉘을 완전히 교체하려는 경우에도 사용할 수 있습니다.

관련 정보