bash 字符串,以空格分隔,到数组
我想读取一个值并将其与其空间分开,然后根据数组的各个部分执行操作。更准确地说,第一个元素上的 if 语句(如果为 false 则跳过其余)将使用另一个程序(查找数据库)将第 3 个元素从单词更改为数字,第四个和最后一个元素将检查上面是否有非数字或数字64,如果没有,那么它将把它们全部组合在一起(我知道该怎么做),然后继续。我已经在 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] = ""
我想要什么:
vzybilly@vzybilly-laptop:~/Desktop$ ./test.sh
cmd: cmd pers item num
a[0] = "cmd"
a[1] = "pers"
a[2] = "item"
a[3] = "num"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
使用
Instead of
use