bash 第 54 行:警告:第 42 行的此处文档由文件尾分隔(需要“EOF”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27201004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:46:05  来源:igfitidea点击:

line 54: warning: here-document at line 42 delimited by end-of-file (wanted `EOF')

basheof

提问by Budrys

I got bash script, but when I run it I got some error:

我有 bash 脚本,但是当我运行它时出现了一些错误:

line 54: warning: here-document at line 42 delimited by end-of-file (wanted `EOF')

第 54 行:警告:第 42 行的此处文档由文件尾分隔(需要“EOF”)

#!/bin/bash
echo "PODAJ IMIE"
read imie
echo  "PODAJ NAZWISKO"
read nazwisko

alias="$imie$nazwisko"
echo "$alias"


while [ 1 ]
do

        i=1

        if [ `egrep $alias /etc/passwd |wc -l` -gt 0 ]; then i=0
        fi

        if [ $i -eq 0  ]; then
                echo "Uzytkownik o podanym aliasie ju? istnieje!"
                echo "PODAJ INNY ALIAS np. "$alias"1"
                read alias

        else
                echo "PODAJ HASLO"
                read -s password
                echo "PODAJ NUMER POKOJU"
                read pokoj

                #adduser --disabled-password --gecos "$imie $nazwisko,$pokoj, ," $alias
                adduser --quiet --disabled-password -shell /bin/bash --home /home/$alias --gecos "$imie $nazwisko, $pokoj, , " $alias
                echo "$alias:$password" | chpasswd
                #echo -e "$haslo\n$haslo\n" | sudo passwd $alias


                dir=/home/$alias
                mkdir $dir/public_html
                mkdir $dir/samba
                cd $dir/public_html
                mkdir private_html

                cat <<EOF >>$dir/.bashrc

                echo "alias ps =/bin/ps"
                echo "alias ls =/bin/ls"
                t=\`date +%F.%H:%M:%S\`
                echo "WITAJ\n"
                echo "DZISIAJ MAMY:$t"
                echo "TWOJE OSTATNIE LOGOWANIE:" && " lastlog -u $USER | tail -n 1 | awk '{print       $4" "$5" "$6" "$7" "$9}'"
                EOF

                break
        fi
done

回答by Mat

The EOFneeds to be at the start of a line, and with nothing after (including no whitespace).

EOF必须在一行的开始,并与后(包括没有空格)什么都没有。

Some of the echos in that block are suspicions. The text up to the end marker will be copied into the file as-is after variable substitution, not executed - and it doesn't look like you want substitution here.

echo该块中的一些s 是怀疑。在变量替换后,直到结束标记的文本将按原样复制到文件中,而不是执行 - 看起来您不想在这里进行替换。

Try this: (notice the quotes around "EOF"on the catline)

试试这个:(各地注意引号"EOF"cat行)

            cat <<"EOF" >>$dir/.bashrc
alias ps=/bin/ps
alias ls=/bin/ls
t=$(date +%F.%H:%M:%S)
echo "WITAJ\n"
echo "DZISIAJ MAMY: $t"
echo "TWOJE OSTATNIE LOGOWANIE:" $(lastlog -u $USER | tail -n 1 | awk '{print       " "" "" "" "}')
EOF

回答by tripleee

The EOF delimiter needs to be aligned in the leftmost column.

EOF 分隔符需要在最左边的列中对齐。

Here's what I often do for alignment.

这是我经常做的对齐。

Lots
    Of
        Indents
            cat <<-____________HERE
                boo
____________HERE
        done
    done
done

回答by shellter

You can also use

你也可以使用

     <Tab><Tab>cat <<-EOF >>$dir/.bashrc
#--------------------^- added a "-" char
     <Tab><Tab>  echo " ... "
     <Tab><Tab>  echo " ... "
     <Tab><Tab>EOF

where EOF is now preceded with a -character. This indicates to the shell that the closing Here-file token will be (can be) indented with tab characters. If you write, by accident, <Tab><Tab><Space>EOFit will not match, but 0,1,2,...,100 <Tab>EOFwillmatch.

现在 EOF 前面有一个-字符。这向 shell 表明关闭的 Here-file 标记将(可以)用制表符缩进。如果你<Tab><Tab><Space>EOF不小心写了,它不会匹配,但 0,1,2,...,100<Tab>EOF匹配。

IHTH

IHTH