读取文件后,找不到回显命令
After read file, echo command not found
我确定这是重复的,但我搜索相关信息却一无所获。
我正在使用 mapfile 读取文件,但我需要 运行 脚本的设备没有加载它。所以我选择了另一种选择。
这不是我的脚本,而是一个证明我观点的测试脚本。
我有一个包含大量统计数据的文件,为了理智,请在下方缩短。
Status
Availability : available
State : enabled
Reason : The virtual server is available
CMP : enabled
CMP Mode : all-cpus
Traffic ClientSide Ephemeral General
Bits In 0 0 -
Bits Out 0 0 -
Packets In 0 0 -
Packets Out 0 0 -
Current Connections 0 0 -
Maximum Connections 0 0 -
Total Connections 0 0 -
Min Conn Duration/msec - - 0
Max Conn Duration/msec - - 0
Mean Conn Duration/msec - - 0
Total Requests - - 0
我正在使用以下代码将文件读入一个数组(我想在脚本中多次使用这个数组)。但是在逐行呼应时。我在每一行都找不到命令。我不知道如何解决这个问题。
#!/bin/bash
getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < ""
}
infile="/home/tony/Desktop/test.txt"
file=getArray $infile
for i in ${file[@]};
do :
echo "$i"
done
结果如下
/home/tony/Desktop/test.txt: line 1: Status: command not found
/home/tony/Desktop/test.txt: line 2: Availability: command not found
/home/tony/Desktop/test.txt: line 3: State: command not found
/home/tony/Desktop/test.txt: line 4: Reason: command not found
/home/tony/Desktop/test.txt: line 5: CMP: command not found
/home/tony/Desktop/test.txt: line 6: CMP: command not found
/home/tony/Desktop/test.txt: line 9: Traffic: command not found
/home/tony/Desktop/test.txt: line 10: Bits: command not found
我试过两次 qouting $i、单次 qouting $i 和 qouting/unqouting 数组。除了 :command not found
,我没有尝试过任何结果
这个有效:
getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < ""
}
infile="/tmp/file"
getArray "$infile" # you would need a return value for "file=getArray $infile"
# and you would write it differently
for i in "${array[@]}";
do
echo "$i"
done
您遇到了三个问题:
file=getArray $infile
不 return 数组读取。您需要更改函数和分配才能工作
- 你的循环中有一个额外的
:
。
- 您需要在 for 循环中将数组括起来以避免分词。
你是对的。我假设函数会 return 数组到 $file,但事实并非如此。我在函数中创建 ${file[@]}。
成功了
#!/bin/bash
getArray() {
file=() # Create array
while IFS= read -r line # Read a line
do
file+=("$line") # Append line to the array
done < ""
}
infile="/home/tony/Desktop/test.txt"
getArray $infile
for i in "${file[@]}";
do :
echo "$i"
done
我确定这是重复的,但我搜索相关信息却一无所获。
我正在使用 mapfile 读取文件,但我需要 运行 脚本的设备没有加载它。所以我选择了另一种选择。
这不是我的脚本,而是一个证明我观点的测试脚本。
我有一个包含大量统计数据的文件,为了理智,请在下方缩短。
Status
Availability : available
State : enabled
Reason : The virtual server is available
CMP : enabled
CMP Mode : all-cpus
Traffic ClientSide Ephemeral General
Bits In 0 0 -
Bits Out 0 0 -
Packets In 0 0 -
Packets Out 0 0 -
Current Connections 0 0 -
Maximum Connections 0 0 -
Total Connections 0 0 -
Min Conn Duration/msec - - 0
Max Conn Duration/msec - - 0
Mean Conn Duration/msec - - 0
Total Requests - - 0
我正在使用以下代码将文件读入一个数组(我想在脚本中多次使用这个数组)。但是在逐行呼应时。我在每一行都找不到命令。我不知道如何解决这个问题。
#!/bin/bash
getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < ""
}
infile="/home/tony/Desktop/test.txt"
file=getArray $infile
for i in ${file[@]};
do :
echo "$i"
done
结果如下
/home/tony/Desktop/test.txt: line 1: Status: command not found
/home/tony/Desktop/test.txt: line 2: Availability: command not found
/home/tony/Desktop/test.txt: line 3: State: command not found
/home/tony/Desktop/test.txt: line 4: Reason: command not found
/home/tony/Desktop/test.txt: line 5: CMP: command not found
/home/tony/Desktop/test.txt: line 6: CMP: command not found
/home/tony/Desktop/test.txt: line 9: Traffic: command not found
/home/tony/Desktop/test.txt: line 10: Bits: command not found
我试过两次 qouting $i、单次 qouting $i 和 qouting/unqouting 数组。除了 :command not found
,我没有尝试过任何结果这个有效:
getArray() {
array=() # Create array
while IFS= read -r line # Read a line
do
array+=("$line") # Append line to the array
done < ""
}
infile="/tmp/file"
getArray "$infile" # you would need a return value for "file=getArray $infile"
# and you would write it differently
for i in "${array[@]}";
do
echo "$i"
done
您遇到了三个问题:
file=getArray $infile
不 return 数组读取。您需要更改函数和分配才能工作- 你的循环中有一个额外的
:
。 - 您需要在 for 循环中将数组括起来以避免分词。
你是对的。我假设函数会 return 数组到 $file,但事实并非如此。我在函数中创建 ${file[@]}。
成功了
#!/bin/bash
getArray() {
file=() # Create array
while IFS= read -r line # Read a line
do
file+=("$line") # Append line to the array
done < ""
}
infile="/home/tony/Desktop/test.txt"
getArray $infile
for i in "${file[@]}";
do :
echo "$i"
done