Bash: Syntax error: redirection unexpected

后端 未结 9 1714
终归单人心
终归单人心 2020-11-27 12:30

I do this in a script:

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

and I get:

Syntax error: redirection unexpected

9条回答
  •  没有蜡笔的小新
    2020-11-27 12:51

    You can get the output of that command and put it in a variable. then use heredoc. for example:

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

    can be written like:

    nc -l -p 80 <

    and like this (this is what you want):

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

    Practical example in busybox under docker container:

    kasra@ubuntu:~$ docker run --rm -it busybox
    / # nc -l -p 80 <<< "tested like a charm";
    sh: syntax error: unexpected redirection
    
    
    / # nc -l -p 80 < 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 < $text
    > EOF
    ^Cpunt!
    

提交回复
热议问题