【发布时间】:2018-06-18 21:57:45
【问题描述】:
我发布这个以防其他人遇到这个问题。我会向 IBM 立案,如果他们有更新,我会报告。
我在使用文件时在 AIX 7.1(但不是 6.1)上遇到问题 驱动循环(例如:cat myfile.txt | while read myvar)。调用 ssh from inside 导致循环在第一次迭代时提前退出。
在我的真实示例中,我没有使用“ls”来填充文件,但在这里这样做更容易演示。
我有以下代码:
#!/usr/bin/ksh93
ssh LESAUN01 -l root "ls -1 c*" > file.list
echo "------------------------------------------"
cat file.list
echo "------------------------------------------"
echo ""
echo "Loop 1"
echo ""
cat file.list | while read file1
do
echo " File: $file1"
done
echo ""
echo "Loop 2"
echo ""
cat file.list | while read file2
do
echo " File: $file2"
ssh LESAUN01 -l root "ls -l $file2"
done
echo ""
echo "Done"
exit
当我从 AIX 6.1 lpar 运行它时,我得到了这些预期的结果
-----------------------------------------
client.txt
customer_handover.log
------------------------------------------
Loop 1
File: client.txt
File: customer_handover.log
Loop 2
File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12 2015 client.txt
File: customer_handover.log
-rw------- 1 root root 27533 Aug 31 18:04 customer_handover.log
Done
在 7.1 上运行时,我得到以下结果:
------------------------------------------
client.txt
customer_handover.log
------------------------------------------
Loop 1
File: client.txt
File: customer_handover.log
Loop 2
File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12 2015 client.txt
Done
我的解决方案是从数组而不是文件(在将文件加载到数组之后)提供循环,这可以按预期工作。
这适用于 6.1 和 7.1
#!/usr/bin/ksh93
ssh LESAUN01 -l root "ls -1 c*" > file.list
echo "------------------------------------------"
cat file.list
echo "------------------------------------------"
echo ""
echo "Loop"
echo ""
i=0
set -A file_array
cat file.list | while read line
do
file_array[ $i ]="$line"
(( i++ ))
done
for x in "${!file_array[@]}"
do
echo " File: ${file_array[$x]}"
ssh LESAUN01 -l root "ls -l ${file_array[$x]}"
done
echo ""
echo "Done"
exit
这给出了预期的结果。
------------------------------------------
client.txt
customer_handover.log
------------------------------------------
Loop
File: client.txt
-rw-r--r-- 1 root root 91323 Feb 12 2015 client.txt
File: customer_handover.log
-rw------- 1 root root 27533 Aug 31 18:04 customer_handover.log
Done
【问题讨论】:
-
首先,您可以将选项移到主机名之前:´ssh -l root LEASUN01 ...´ 或简单地´ssh root@LEASUN01 ...´