<< generate alphabets and append in the input values >>

Hi Team,
Pls help to get the desired output.

I have a input like below

nodecount=10
host=na7-db1-1-chi

nodecount can be 10 or 8 based on this we need a output (in single line) like below

na7-db1-1-chi:A na7-db1-2-chi:B na7-db1-3-chi:C na7-db1-4-chi:D na7-db1-5-chi:E na7-db1-6-chi:F na7-db1-7-chi:G na7-db1-8-chi:H na7-db1-9-chi:I na7-db1-10-chi:J

Regards
Kamal

Try something like:

#!/bin/bash
nodecount=10
host=na7-db1-1-chi
IFS=- read -a H <<< "$host"
C=({A..Z})
for ((i=1; i<=nodecount; i++))
do
  printf "%s " "${H[0]}-${H[1]}-${i}-${H[3]}:${C[i-1]}"
done
echo
1 Like

Thanks for you reply, It throwing some code error. Can you pls check

$  cat aa
#!/bin/bash
set -x
nodecount=10
host=na7-db1-1-chi
IFS=- read -A H <<< "$host"
C=({A..Z})
for ((i=1; i<=nodecount; i++))
do
  printf "%s " "${H[0]}-${H[1]}-${i}-${H[3]}:${C[i-1]}"
done
[oracle@etfvm20-db1-1-sfm:~]# aa
+ nodecount=10
+ host=na7-db1-1-chi
+ IFS=-
+ read -A H
./aa: line 5: read: -A: invalid option
read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
+ C=({A..Z})
+ (( i=1 ))
+ (( i<=nodecount ))
+ printf '%s ' --1-:A
--1-:A + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --2-:B
--2-:B + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --3-:C
--3-:C + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --4-:D
--4-:D + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --5-:E
--5-:E + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --6-:F
--6-:F + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --7-:G
--7-:G + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --8-:H
--8-:H + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --9-:I
--9-:I + (( i++ ))
+ (( i<=nodecount ))
+ printf '%s ' --10-:J
--10-:J + (( i++ ))
+ (( i<=nodecount ))

Try read -a H , and then read the man bash

2 Likes

thanks RudiC & Scrutinizer perfectly working !!!!