以下是我的shell脚本代码:
#!/bin/bash
first=0
my_for_loop()
{
first="$1";
if [ $first -eq 0 ]
then
return 1;
elif [ $first -eq 1 ]
then
return 0;
fi
}
while read line; do
for word in $line; do
echo "word = '$word'"; ( echo "$word" | grep -Eq "\([0-9]+\)" ) && ( word="${word%\)}"; word="`echo ${word} | cut -d'(' -f 2`"; echo "no = $word";
my_for_loop "$first"; first="$?"; echo "foo = $first")
done
echo "first value is $first" )
done
基本上我试图从输入中读取单词并检查它是否为#34;([0-9] +)" {例如。 (120)如果是,则提取数字,即120
当我试图设置变量"第一个"在模式出现奇数时为1,在出现事件时为0
我尝试了过程替换,但没有正常工作。
你能帮助我在for循环中设置布尔值,以便它在下一次迭代中保留值吗?
答案 0 :(得分:0)
避免子进程。这是否是你想要的:
i=0
while read line ; do
cutted=$(sed 's/^(\([0-9]*\))$/\1/' <<< $line)
if [ "${line}" != "${cutted}" ]; then
(( i = !i))
echo "First=$i and ${cutted}"
fi
done
当你用文字剪切东西时使用另一个循环
i=0
while read line ; do
for word in $line; do
echo "${word}"
cutted=$(sed 's/^(\([0-9]*\))$/\1/' <<< ${word})
if [ "${word}" != "${cutted}" ]; then
(( i = !i))
echo "First=$i and ${cutted}"
fi
done
done