gpt4 book ai didi

shell脚本: "' <<' unmatched"-syntax error using here document

转载 作者:行者123 更新时间:2023-12-02 14:38:04 24 4
gpt4 key购买 nike

您好,我正在尝试编写一个程序,如果感兴趣的人在给定时间上线,该程序将提醒用户。到目前为止我的程序是

#!/usr/bin/ksh
message=""
when=""
validFiles=""
validUsers=""

if [ $# -gt 0 ] ; then
while getopts w:m: opt
do
case $opt in
w) when=$OPTARG;;
m) message=$OPTARG;;
\?) echo $USAGE exit 2;;
esac
done
shift $(($OPTIND - 1))
if [[ $# -gt 0 ]] ; then
for i; do
if [[ -f "$i" && -r "$i" ]]; then
if ! echo $validFiles | grep $i >/dev/null; then
validFiles="$validFiles $i"
fi
elif id $i 2> /dev/null 1>&2; then
if ! echo $validUsers | grep $i > /dev/null; then
validUsers="$validUsers $i"
fi
fi
done
if [[ $when != "" && $validFiles != "" || $validUsers != "" ]] ;then
for i in $validUsers; do
if ! grep $i $validFiles >/dev/null; then
at $when <<"END"
if finger $i | grep $i; then
echo "$i is online" | elm $message
fi
END
fi
done
fi
else
echo "No files or usernames"
fi
else
echo "No arguments provided"
fi

我的问题是,当我尝试运行此程序时,我收到错误消息

 syntax error at line 33 : `<<' unmatched

我不确定为什么会出现这种情况。我检查了许多其他示例,我的 at 命令,此处文档,似乎与他们的相同。有人可以帮我吗?谢谢。

最佳答案

这里的字符串分隔符不能缩进,您的END应该位于行的开头:

$ cat <<EOT
> foo
> bar
> EOT
foo
bar

如果您希望尾部分隔符缩进,可以使用以下语法,但这也会从此处文档本身中删除所有前导制表符(这只适用于制表符!):

$ cat <<-EOT
> foo
> bar
> quux
> EOT
foo
bar
quux

请注意,此行为由 POSIX 指定所以应该可以在所有兼容的 shell 中工作:

If the redirection symbol is "<<-", all leading tabs shall be stripped from input lines and the line containing the trailing delimiter.

关于shell脚本: "' <<' unmatched"-syntax error using here document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com