bash string, separated by spaces, to array
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"
Instead of
a=$(echo "$IN")
use
a=($IN)
精彩评论