Bash: Syntax error: redirection unexpected

后端 未结 9 1566
终归单人心
终归单人心 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:42

    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.

    0 讨论(0)
  • 2020-11-27 12:44

    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...

    0 讨论(0)
  • 2020-11-27 12:44

    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
    
    0 讨论(0)
  • 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 <<EOF
    tested like a charm
    EOF
    

    and like this (this is what you want):

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

    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 <<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!
    
    0 讨论(0)
  • 2020-11-27 12:56

    Docker:

    I was getting this problem from my Dockerfile as I had:

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

    However, according to this issue, it was solved:

    The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain /bin/sh.

    Note

    To use a different shell, other than /bin/sh, use the exec form passing in the desired shell. For example,

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

    Solution:

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

    Notice the quotes around each parameter.

    0 讨论(0)
  • 2020-11-27 12:58

    Does your script reference /bin/bash or /bin/sh in its hash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator.

    0 讨论(0)
提交回复
热议问题