2

i wrote a sample script which will split the string into multiple variable

#!/bin/bash
IN="One-XX-X-17.0.0"
IFS='-' read -a ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
    echo "${ADDR[i]}"
done

while IFS='.' read -a ADDR[3]; do
      for i in "${ADDR[3]}"; do
           process "$i"
done

like var11 = One, var12=XX, var13=X, major1=17, minor1=0, rel1=0

but while running this script every time getting error . Could someone tell me what wrong with my script

TeamZ
  • 343
  • 6
  • 15
  • 1
    What error are you getting? – Bart Friederichs Oct 10 '17 at 06:46
  • it was showing this below error i don't know why ./versionsplit.sh: line 3: cannot create temp file for here-document: No space left on device ./versionsplit.sh: line 8: cannot create temp file for here-document: No space left on device – TeamZ Oct 10 '17 at 08:23

1 Answers1

3
#!/bin/bash
IN="One-XX-X-17.0.0"
IFS='-' read -a ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
    echo "$i"
done
#split 17.0.0 into NUM
IFS='.' read -a NUM <<<${ADDR[3]};
for i in "${NUM[@]}"; do
    echo "$i"
done

output:

One
XX
X
17.0.0
17
0
0

UPD

#same code
#array of names of variable
var=(major minor rel)

#split 17.0.0 into NUM
IFS='.' read -a NUM <<<${ADDR[3]};

for((i=0;i<${#NUM[@]};i++)); do
    #assign
    eval ${var[$i]}=${NUM[$i]}
done
#now test
echo $major
echo $minor 
echo $rel

output:

17
0
0
tso
  • 4,732
  • 2
  • 22
  • 32
  • thanks for your reply but the output looks like this One XX X 17.0.0 17 0 0 – TeamZ Oct 10 '17 at 08:26
  • You mean on one line?, then replace `echo "$i"` with `printf " %s" "$i"` (or `echo -n " $i"`) and add `echo ""` after `done`. – David C. Rankin Oct 10 '17 at 08:47
  • still no success , this is not an issue could you tell me after execution of below block `code` #split 17.0.0 into NUM IFS='.' read -a NUM <<<${ADDR[3]}; for i in "${NUM[@]}"; do echo "$i" done how do assign the output to different variable like Major = 17 Minor = 0 Rel = 0 – TeamZ Oct 10 '17 at 09:00
  • updated answer. – tso Oct 10 '17 at 11:08