4

I want to take a value read in and separate it from its spaces, then do stuff depending on the parts of the array. More exactly, if statement on the first (if false skip rest) 3 element will be changed from a word to a number using another program (a look up database) the 4th and last will check if there is a non-number or number above 64, if there isn't then it will combine it all back together (which I know how to do) then continue on. I have been working on this for over 3 hours now across multiple websites from Google.

vzybilly@vzybilly-laptop:~/Desktop$ cat ./test.sh
#!/bin/bash
read -p "cmd: " IN

#OIFS=$IFS
#IFS=';'
#arr2=$IN

#a=$(echo $IN | tr " " "\n")
a=$(echo "$IN")
for i in $(seq 0 $((4 - 1))); do
    echo "a[$i] = \"${a[$i]}\""
done

#IFS=$OIFS

exit 0
vzybilly@vzybilly-laptop:~/Desktop$ ./test.sh
cmd: cmd pers item num
a[0] = "cmd pers item num"
a[1] = ""
a[2] = ""
a[3] = ""

What I want:

vzybilly@vzybilly-laptop:~/Desktop$ ./test.sh
cmd: cmd pers item num
a[0] = "cmd"
a[1] = "pers"
a[2] = "item"
a[3] = "num"

1 Answer 1

5

Instead of

a=$(echo "$IN")

use

a=($IN)
Sign up to request clarification or add additional context in comments.

2 Comments

nope: vzybilly@vzybilly-laptop:~/Desktop$ ./test.sh cmd: cmd pers item num ./test.sh: line 9: cmd: command not found a[0] = "" a[1] = "" a[2] = "" a[3] = ""
Did you by any chance write a=$($IN) instead of a=($IN)? My command should not be executing anything; and $() executes stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.