我有一个脚本,在其中我需要创建一个数组,其中包含一些文件的路径和名称,
然后,在检查用户的arg之后,我需要将路径和名称分别传递给另一个脚本,以匹配数组中的一个名称
array1[0]="/Distenation1/File1.txt"
array1[1]="file1"
#testing another way to set an array
set -A array2 "/Distenation2/File2.txt" file2这里出现了一个问题,因为我无法找到将数组作为整体传递的方法,然后在for循环中将用户arg $1与file2 1或file2 2等匹配,并将相应的路径传递给另一个脚本:
#the following code doesnt work as needed -logically-
for i in ${array1[@]} ${array2[@]}
do
if [ $1 = ${i[1]} ]
then
./sendfile ${i[0]}
fi;
done编辑:我看到的ksh版本= M-11/16/88f版
使用与上面相同的代码,但使用echo来显示一个示例:
代码:
for i in ${array1[@]} ${array2[@]}
do
echo Name : ${i[1]} '\n'Path : ${i[0]}
done产出:
Name :
Path : /Distenation1/file1.txt
Name :
Path : file1
Name :
Path : /Distenation2/file2.txt
Name :
Path : file2所需的结果应是:
Name : file1
Path : /Distenation1/file1.txt
Name : file2
Path : /Distenation2/file2.txt发布于 2019-10-10 17:28:46
我尝试了一项我认为我能理解的工作:
#put all the data inside 1 array
array1[0]="/Distenation1/File1.txt"
array1[1]="file1"
array1[2]="/Distenation2/File2.txt"
array1[3]="file2"
#create a counter
n=0
#the trick here was that 'i' (loop) stores 1 array data per iteration (at first i thought it stores the whole array data then go through them 1 by one)
for i in ${array1[@]}
do
if [ $1 = ${i} ]
then
#here i had to call the original array to be able to read through any data inside the array
echo Name : $i '\n' Path : ${array1[$n+1]}
fi;
let n=n+1
done;产出:
=>test1 file1
Name : file1
Path : /Distenation1/File1.txt我不确定这是否是最佳实践,但我对任何更好的解决方案或优化代码都持开放态度,因为我正在处理一个非常大的脚本,每一毫秒的优化都会产生不同的效果。
https://unix.stackexchange.com/questions/545781
复制相似问题