简体   繁体   English

Shell-具有命令和增量命令值的循环数组

[英]Shell - Looping Array with command and increment command values

    var1=$(echo $getDate | awk '{print $1} {print $2}')
    var2=$(echo $getDate | awk '{print $3} {print $4}')
    var3=$(echo $getDate | awk '{print $5} {print $6}')

Instead of repeating like the code above, I need to: 除了重复上面的代码外,我需要:

  1. loop the same command 循环相同的命令
  2. increment the values ({print $1} {print $2}) 递增值({print $ 1} {print $ 2})
  3. store the value in an array 将值存储在数组中

I was doing something like below but I am stuck maybe someone can help me please: 我在做下面的事情,但是我被困住了,也许有人可以帮我一下:

COMMAND=`find $locationA -type f | wc -l`
getDate=$(find $locationA -type f | xargs ls -lrt | awk '{print $6} {print $7}')
a=1
b=2
for i in $COMMAND
do
   i=$(echo $getDate | awk '{print $a} {print $b}')
   myarray+=('$i')
   a=$((a+1))
   b=$((b+1))
done

PS - using ksh PS-使用ksh

Problem: $COMMAND stores the number of files found in $locationA. 问题:$ COMMAND存储在$ locationA中找到的文件数。 I need to loop through the amount of files found and store their dates in an array. 我需要遍历找到的文件数量并将它们的日期存储在数组中。

I don't get the meaning of your example code (what is the 'for' loop supposed to do? What is the content of the variable COMMAND?), but in your question you ask to store something in an array , while in the code you wish to simplify, you don't use an array, but simple variables (var1, var2, ....). 我没有得到您的示例代码的含义(“ for”循环应该做什么?变量COMMAND的内容是什么?),但是在您的问题中,您要求将某些内容存储在数组中 ,而在您希望简化的代码,而不是数组,而是简单的变量(var1,var2,...)。

If I understand your requirement correctly, your variable getDate contains a string of several words, which are separated by spaces, and you want to assign the first two words to var1, the following two words to var2, and so on. 如果我正确理解了您的要求,则变量getDate包含由多个单词组成的字符串,这些单词由空格分隔,并且您想将前两个单词分配给var1,然后将两个单词分配给var2,依此类推。 Is this correct? 这个对吗?

Now the edited code is at least a bit clearer, though I still don't understand, why you use i as a loop variable, and overwrite it in the first statement inside the loop. 现在,尽管我仍然不明白,但是为什么将i用作循环变量,并在循环内的第一条语句中将其覆盖,所以编辑后的代码至少要清晰一些。

However, a few comments: 但是,一些评论:

  • If you push '$i' into your array, you will get a literal '$' sign, followed by the letter 'i'. 如果将“ $ i”推入数组,则会得到一个文字“ $”符号,后跟字母“ i”。 To add a variable i containing to numbers, you need double quotes ("$i"). 要将变量i包含在数字中,您需要双引号(“ $ i”)。

  • I don't understand why you want to loop over the cotnent of the variable COMMAND. 我不明白您为什么要遍历变量COMMAND的内容。 This variable will always hold a single number, which means that the loop will be executed exactly once. 此变量将始终包含一个数字,这意味着循环将仅执行一次。

  • You could use a counting loop, incrementing loop variable by 2 on each iteration. 您可以使用一个计数循环,在每次迭代中将循环变量增加2。 You would have to precalculate the number of iterations beforehand. 您必须预先计算迭代次数。

  • Perhaps an easier alternative, which would work in bash or in zsh (I did not try other shells) is to first turn your variable in an array, 在bash或zsh(我没有尝试过其他shell)中工作的也许更简单的选择是首先将变量放入数组中,

    tmparr=($(echo $getDate|fmt -w 1)) tmparr =($(echo $ getDate | fmt -w 1))

and then use a loop to collect pairs of this element: 然后使用循环来收集该元素对:

myarray=()
for ((i=0; i<${#tmparr[*]}; i+=2))
do
    myarray+=("${tmparr[$i]} ${tmparr[$((i+1))]}")
done

${myarray[0]} will hold a string consisting of the first to words from getDate, etc. $ {myarray [0]}将包含一个字符串,该字符串由getDate等的第一个到第一个单词组成。

This one should work on zsh, at least with newer versions: 此版本应在zsh上运行,至少在较新的版本上可用:

myarray=()
echo $g|fmt -w 1|paste -s -d " \n"|while read s; do myarray+=("$s"); done

This leaves the first pair in ${myarray[1]}, etc. 这将第一对留在$ {myarray [1]}中,依此类推。

It doesn't work with bash (and old zsh versions), because these shells would execute the body of the loop in a subshell. 它不适用于bash(和旧的zsh版本),因为这些shell将在子shell中执行循环的主体。

ADDED: 添加:

On a second thought, in zsh this one would be simpler: 再想一想,在zsh中,这会更简单:

myarray=("${(f)$(echo $g|fmt -w 1|paste -s -d ' \n')}")

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

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