简体   繁体   English

Shell脚本:如何将循环输出保存到数组

[英]Shell script: How to save loop output to array

This is my simple shell script. 这是我简单的shell脚本。 The objective is to split string "RANDOM948" as array so that I can manipulate any character in string "RANDOM948". 目的是将字符串“ RANDOM948”拆分为数组,以便我可以操纵字符串“ RANDOM948”中的任何字符。

ubuntu@Ubuntu:~$ cat -n longscript.sh 
     1  string="RANDOM948"
     2
     3  c1=${string:0:1}
     4  c2=${string:1:1}
     5  c3=${string:2:1}
     6  c4=${string:3:1}
     7  c5=${string:4:1}
     8  c6=${string:5:1}
     9  c7=${string:6:1}
    10  c8=${string:7:1}
    11  c9=${string:8:1}
    12              
    13  echo $c9 $c4 $c1
ubuntu@Ubuntu:~$ 

ubuntu@Ubuntu:~$ ./longscript.sh 
8 D R
ubuntu@Ubuntu:~$ 

I believe this can be simplify by using for loop. 我相信可以通过使用for循环来简化此过程。 This is my attempt. 这是我的尝试。 However, I have no idea how to save the loop output as array. 但是,我不知道如何将循环输出保存为数组。

ubuntu@Ubuntu:~$ cat -n testscript.sh 
     1  string="RANDOM948"
     2
     3  for i in {1..9}
     4  do
     5   echo c$i=$\{string:`expr $i - 1`:1}
     6  done
     7
     8  # echo $c9 $c4 $c1
ubuntu@Ubuntu:~$ 

ubuntu@Ubuntu:~$ ./testscript.sh 
c1=${string:0:1}
c2=${string:1:1}
c3=${string:2:1}
c4=${string:3:1}
c5=${string:4:1}
c6=${string:5:1}
c7=${string:6:1}
c8=${string:7:1}
c9=${string:8:1}
ubuntu@Ubuntu:~$ 

UPDATE: New code as advised by @sos 更新:@sos建议的新代码

I've updated this code with new line (5 & 6), however it still doesn't work 我已经用新行(5&6)更新了此代码,但是仍然无法正常工作

ubuntu@Ubuntu:~$ cat -n testscript.sh 
     1  string="RANDOM948"
     2
     3  for i in {1..9}
     4  do
     5   typeset -a c
     6   c[${i}]=$\{string:`expr $i - 1`:1}
     7  done
     8
     9  echo TEST OUTPUT $c9 $c4 $c1
ubuntu@Ubuntu:~$ 
ubuntu@Ubuntu:~$ 
ubuntu@Ubuntu:~$ ./testscript.sh 
TEST OUTPUT
ubuntu@Ubuntu:~$ 

replace 更换

    echo c${i}

with

    typeset -a c       # declare a indexed array
    c[${i}]=...

you will do yourself and others a favor if you identify what is running your script by using a shebang line eg 如果您使用shebang行识别正在运行脚本的内容,那么您将对自己和其他人有所帮助,例如

#!/bin/bash

as the first line in your script. 作为脚本的第一行。 In any case, the below example may help you. 无论如何,以下示例可能会对您有所帮助。

cat ./looptest.sh

note the #! 注意#! line.... 线....

#!/bin/bash
for i in {0..9}
do
  myArray[$i]="hello_$i"
  echo set the value of myArray[$i]
done
echo the value of myArray[4] is ${myArray[4]}

Here's the output: 这是输出:

set the value of myArray[0]
set the value of myArray[1]
set the value of myArray[2]
set the value of myArray[3]
set the value of myArray[4]
set the value of myArray[5]
set the value of myArray[6]
set the value of myArray[7]
set the value of myArray[8]
set the value of myArray[9]
the value of myArray[4] is hello_4

Here's what I get 这就是我得到的

    set -x
    for i in {1..9}
      do
        c[${i}]=${string:`expr $i - 1`:1}
     done
    set +x

    + expr 1 - 1
    + c[1]=R
    + expr 2 - 1
    + c[2]=A
    + expr 3 - 1
    + c[3]=N
    + expr 4 - 1
    + c[4]=D
    + expr 5 - 1
    + c[5]=O
    + expr 6 - 1
    + c[6]=M
    + expr 7 - 1
    + c[7]=9
    + expr 8 - 1
    + c[8]=4
    + expr 9 - 1
    + c[9]=8

Note: It's considered "unusual" to begin an array index at 1 rather than 0 注意:从1而不是0开始的数组索引被认为是“不寻常的”

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

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