mccli dataset show --recursive \
| awk -F"/" '!/^Dataset.*Domain|^0,23000|^----|^$/ {print $2"/"$1}' \
| while read dataset
do
echo $dataset
mccli dataset show --name="$dataset" | grep -i "freezemethod:value=none"
done
它在其中一台 Linux 机器上工作。当我将相同的脚本复制到其他 Linux 时。我收到以下错误。 (我在 vi 编辑器中插入后粘贴) .
./freezemethod: line 2: syntax error near unexpected token `|'
'/freezemethod: line 2: `| awk -F"/" '!/^Dataset.*Domain|^0,23000|^----|^$/ {print $2"/"$1}' \
最佳答案
检查以确保第 1 行末尾的 \
后面没有 字符(空格、制表符等)。如果它确实后面有字符,它不会被视为续行,第 2 行将被视为新命令,引发类似什么的错误你看到了:
./freezemethod: line 2: syntax error near unexpected token '|'
最好的检查方法是运行转储命令,例如:
od -xcb ./freezemethod | head -30l
并检查文件前两行的二进制信息,不起作用的那一行。
作为确认,以下文件testprog.sh
:
echo hello \
| cut -c1-2
当使用bash testprog.sh
运行时,将输出hello
的前两个字符:
he
如果您在 \
之后放置一个空格字符,您将看到:
hello
testprog.sh: line 2: syntax error near unexpected token '|'
testprog.sh: line 2: '| cat'
echo
工作正常没有 过滤器(输出完整的单词),并且单独的命令导致问题,因为它以 |
开头.
而且,根据您在评论中发布的内容,问题正是我所描述的。您生成的 od
命令(我添加到最后一行):
root@w2ran0301:/tmp/#: od -xcb ./freezemethod | head -30l
0000000 636d 6c63 2069 6164 6174 6573 2074 6873
m c c l i d a t a s e t s h
155 143 143 154 151 040 144 141 164 141 163 145 164 040 163 150
0000020 776f 2d20 722d 6365 7275 6973 6576 5c20
o w - - r e c u r s i v e \
157 167 040 055 055 162 145 143 165 162 163 151 166 145 040 134
0000040 0a0d 207c 7761 206b 462d 2f22 2022 2127
^^^^
0a0d
序列(你必须倒过来读)实际上是一个 CR/LF
序列,这意味着你的第一行是:
mccli dataset show --recursive \^M
(^M
代表行尾的 CR)。
这意味着在\
和行尾之间是一个字符,所以\
正在转义它 而不是充当行继续符。
有许多选项可用于从文件中删除这些 CR 字符,例如 this answer 中所示的选项。 .
一种方法是先备份文件:
cp freezemethod freezemethod-cr
然后使用该备份文件重新创建原始没有回车符:
sed 's/\r$//' freezemethod-cr >freezemethod
这应该会为您提供一个具有正确行尾的 freezemethod
。
关于linux - ./freezemethod : line 2: syntax error near unexpected token `|' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29160116/