简体   繁体   中英

Shell script: How to save loop output to array

This is my simple shell script. The objective is to split string "RANDOM948" as array so that I can manipulate any character in string "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. 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

I've updated this code with new line (5 & 6), however it still doesn't work

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

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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