Programing

배쉬 : 구문 오류 : 예기치 않은 리디렉션

lottogame 2020. 7. 14. 08:17
반응형

배쉬 : 구문 오류 : 예기치 않은 리디렉션


스크립트에서이 작업을 수행합니다.

read direc <<< $(basename `pwd`)

그리고 나는 얻는다 :

Syntax error: redirection unexpected

우분투 머신에서

/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)

다른 수세 시스템에서는이 오류가 발생하지 않습니다.

/bin/bash --version
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

왜 오류가 발생합니까?


스크립트 참조합니까 /bin/bash또는 /bin/sh해시 뱅 라인? 우분투의 기본 시스템 쉘은 bash가 아닌 dash 이므로 스크립트가 예상 한 것과 다른 쉘을 사용합니다. 대시에는 리디렉션 연산자 가 없습니다 .#!/bin/sh<<<


도커 :

내가했던 것처럼 Dockerfile 에서이 문제가 발생했습니다.

RUN bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

그러나이 문제 에 따라 해결되었습니다.

간부 형태는 가능 쉘 문자열 munging을 피하고,에 할 수 있습니다 RUN포함되지 않은 기본 이미지를 사용하여 명령 /bin/sh.

노트

이외의 다른 쉘을 사용하려면 원하는 쉘에 전달 /bin/sh되는 exec 양식을 사용하십시오 . 예를 들어

RUN ["/bin/bash", "-c", "echo hello"]

해결책:

RUN ["/bin/bash", "-c", "bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)"]

매개 변수 주위에 따옴표가 있습니다 .


해당 명령의 출력을 가져 와서 변수에 넣을 수 있습니다. 그런 다음을 사용하십시오 heredoc. 예를 들면 다음과 같습니다.

nc -l -p 80 <<< "tested like a charm";

다음과 같이 쓸 수 있습니다 :

nc -l -p 80 <<EOF
tested like a charm
EOF

그리고 이것처럼 (이것이 당신이 원하는 것입니다) :

text="tested like a charm"
nc -l -p 80 <<EOF
$text
EOF

컨테이너 busybox아래의 실제 예 docker:

kasra@ubuntu:~$ docker run --rm -it busybox
/ # nc -l -p 80 <<< "tested like a charm";
sh: syntax error: unexpected redirection


/ # nc -l -p 80 <<EOL
> tested like a charm
> EOL
^Cpunt!       => socket listening, no errors. ^Cpunt! is result of CTRL+C signal.


/ # text="tested like a charm"
/ # nc -l -p 80 <<EOF
> $text
> EOF
^Cpunt!

다음을 사용하여 스크립트를 실행하는 경우 :

sudo sh ./script.sh

그런 다음 대신 다음을 사용하십시오.

sudo bash ./script.sh

The reason for this is that Bash is not the default shell for Ubuntu. So, if you use "sh" then it will just use the default shell; which is actually Dash. This will happen regardless if you have #!/bin/bash at the top of your script. As a result, you will need to explicitly specify to use bash as shown above, and your script should run at expected.

Dash doesn't support redirects the same as Bash.


do it the simpler way,

direc=$(basename `pwd`)

Or use the shell

$ direc=${PWD##*/}

Another reason to the error may be if you are running a cron job that updates a subversion working copy and then has attempted to run a versioned script that was in a conflicted state after the update...


In my case error is because i have put ">>" twice

mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> >> $LOG_PATH

i just correct it as

mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> $LOG_PATH

On my machine, if I run a script directly, the default is bash.

If I run it with sudo, the default is sh.

That’s why I was hitting this problem when I used sudo.


Before running the script, you should check first line of the shell script for the interpreter.

Eg: if scripts starts with /bin/bash , run the script using the below command "bash script_name.sh"

if script starts with /bin/sh, run the script using the below command "sh script_name.sh"

./sample.sh - This will detect the interpreter from the first line of the script and run.

Different Linux distributions having different shells as default.

참고URL : https://stackoverflow.com/questions/2462317/bash-syntax-error-redirection-unexpected

반응형