简体   繁体   English

用空格分隔的bash字符串到数组

[英]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. 更确切地说,如果第一个(如果假跳过休息)3元素的语句将使用另一个程序(查找数据库)从一个单词更改为一个数字,那么第四个和最后一个将检查上面是否有非数字或数字64,如果没有那么它会将它们全部重新组合在一起(我知道该怎么做)然后继续。 I have been working on this for over 3 hours now across multiple websites from Google. 我已经在谷歌的多个网站上工作了3个多小时。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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