Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在循环中拆分字符串_Bash - Fatal编程技术网

Bash 在循环中拆分字符串

Bash 在循环中拆分字符串,bash,Bash,我在bash中声明了一个数组,如下所示: declare -a StringArray=("A:100" "B:200" "C:300" "D:400 ) 我正在尝试循环并能够打印出以下格式: Letter - A Value - 100 Letter - B Value - 200 我尝试了以下代码,但它只是像这样打印出整个字符串: Letter - A:100 Value Letter - B:200 您需要填充数组

我在bash中声明了一个数组,如下所示:

declare -a StringArray=("A:100" "B:200" "C:300" "D:400 )
我正在尝试循环并能够打印出以下格式:

Letter - A
Value - 100
Letter - B
Value - 200
我尝试了以下代码,但它只是像这样打印出整个字符串:

Letter - A:100
Value
Letter - B:200

您需要填充数组,以便能够将其与索引一起使用

#!/bin/bash
declare -a StringArray=("A:100" "B:200" "C:300" "D:400" )

IFS=:
for tuple in "${StringArray[@]}"; do
    values=($tuple)
    echo "Letter - ${values[0]}"
    echo "Value - ${values[1]}"
done
您可以在阅读元素后使用read:

for a in "${StringArray[@]}"; do
    IFS=: read v1 v2 _ <<< "$a" &&
    printf "Letter - %s\nValue - %s\n" "$v1" "$v2"
done

演示IFS字符串拆分和关联数组创建:

!/usr/bin/env bash 声明-a StringArray=a:100B:200C:300D:400 如果=: printf'字母-%s\n值-%s\n'${StringArray[@]} 声明-A StringAssoc=$printf'[%q]=%q'${StringArray[@]} 字体-p StringAssoc 对于${!StringAssoc[@]}中的k;做 printf'字母-%s\n值-%s\n'$k${StringAssoc[$k]} 完成 输出:

字母A 价值-100 字母-B 价值-200 字母-C 价值-300 字母-D 价值-400 声明-A StringAssoc=[D]=400[C]=300[B]=200[A]=100 字母-D 价值-400 字母-C 价值-300 字母-B 价值-200 字母A 价值-100 请注意,迭代关联数组时,不会保留条目的顺序。

printf "Letter - %s\nValue - %s\n" ${StringArray[*]//:/ }
如果指定数组元素不包含空格字符,则就足够了。在这种情况下不需要循环

否则,将需要一个循环:

StringArray=("A:100" "B:200" "C:300" "D:400" "E:a b")
for val in "${StringArray[@]}"; do
    IFS=: read -r name value <<< "$val"
    printf "Letter - %s\nValue - %s\n" "$name" "$value"
done
应适用于所有情况,前提是:始终用作分隔符。我更喜欢在子shell中运行它,以免弄乱当前shell的IFS

#!/bin/sh

next() {
[[ -s stack ]] && main
end
}

main() {
line=$(ed -s stack < edprint+.txt)
letter=$(echo "${line}" | cut -d':' -f1)
number=$(echo "${line}" | cut -d':' -f2)
echo "Letter = ${letter}.  Number = ${number}" >> report+.txt
ed -s stack < edpop+.txt
next
}

end() {
rm -v ./stack
rm -v ./edprint+.txt
rm -v ./edpop+.txt
exit 0
}

echo "A:100 B:200 C:300 D:400" | tr ' ' '\n' > stack

cat >> edprint+.txt << EOF
1
q
EOF

cat >> edpop+.txt << EOF
1d
wq
EOF

next
我个人也真的不喜欢数组;但我尽量避免告诉别人不要使用它们,因为当别人告诉我不要使用我过去也喜欢的方法时,我会很恼火,我不想成为一个伪君子

StringArray=("A:100" "B:200" "C:300" "D:400" "E:a b")
for val in "${StringArray[@]}"; do
    IFS=: read -r name value <<< "$val"
    printf "Letter - %s\nValue - %s\n" "$name" "$value"
done
(IFS=:; printf 'Letter - %s\nValue - %s\n' ${StringArray[@]})
#!/bin/sh

next() {
[[ -s stack ]] && main
end
}

main() {
line=$(ed -s stack < edprint+.txt)
letter=$(echo "${line}" | cut -d':' -f1)
number=$(echo "${line}" | cut -d':' -f2)
echo "Letter = ${letter}.  Number = ${number}" >> report+.txt
ed -s stack < edpop+.txt
next
}

end() {
rm -v ./stack
rm -v ./edprint+.txt
rm -v ./edpop+.txt
exit 0
}

echo "A:100 B:200 C:300 D:400" | tr ' ' '\n' > stack

cat >> edprint+.txt << EOF
1
q
EOF

cat >> edpop+.txt << EOF
1d
wq
EOF

next