简体   繁体   English

BASH脚本中的语法错误-readarray + cut命令

[英]Syntax Error in BASH script - readarray + cut command

I am trying to parse a comma separated file to a couple arrays. 我正在尝试将逗号分隔的文件解析为几个数组。

First array will contain values from the 1st column of the file Second array will contain values from the 3rd column of the file 第一个数组将包含文件第一列中的值。第二个数组将包含文件第三列中的值。

I am using readarry and cut to try to accomplish this task, but I am getiing a syntax error: 我正在使用readarry和cut尝试完成此任务,但我遇到语法错误:

readarray -t VLANs < <( cut -d, -f1 /logs/repository/vlansList.csv )
readarray -t VLANs < <(cut -d, -f1 $1)

None of the 2 approaches above are working. 以上两种方法均无效。

Error is: 错误是:

./ios2NX.sh: line 59: syntax error near unexpected token `<'
./ios2NX.sh: line 59: `         readarray -t VLANs < <( cut -d, -f1 /logs/repository/vlansList.csv)'

Funny thing is that the error is not displayed if I test the script with bash - [script_name] 有趣的是,如果我使用bash测试脚本,则不会显示错误-[script_name]

I tried putting the path in quotes and double quotes, the same error was thrown. 我尝试将路径放在引号和双引号中,引发了相同的错误。

Do you guys know what I am missing here or can suggest a better approach than the readarray + cut ? 你们知道我在这里缺少什么吗,还是可以提出比readarray + cut更好的方法? (lines in my file contain white spaces) (我文件中的行包含空格)

Thanks in advance! 提前致谢!

The problem is that you're writing bash but running with sh . 问题是您正在编写bash但使用sh运行。

If your shebang is #!/bin/sh , replace it with #!/bin/bash 如果您的shebang是#!/bin/sh ,则将其替换为#!/bin/bash

If you're running sh yourfile , instead run bash yourfile . 如果您正在运行sh yourfile ,请运行bash yourfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM