当前位置: 技术问答>linux和unix
shell中读取文件后的变量不能传递出来吗?
来源: 互联网 发布时间:2017-01-15
本文导语: 在list里有一组数字,我要判断每行数字的大小,然后输出个数,但是我读完后,发现count的变量传递不出来,这是怎么回事? #!/bin/bash if [ "$2" == "" ]; then echo 'Usage:' $0 ' ' exit 0 fi firstend=24 secondend=108 ...
在list里有一组数字,我要判断每行数字的大小,然后输出个数,但是我读完后,发现count的变量传递不出来,这是怎么回事?
#!/bin/bash
if [ "$2" == "" ]; then
echo 'Usage:' $0 ' '
exit 0
fi
firstend=24
secondend=108
thirdend=368
count1=0
count2=0
count3=0
cat list | while read line
do
if [[ $line .le. $firstend ]] && [[ $line .ge.1 ]]
then
count1=$(($count1+1))
elif [[ $line .gt. $firstend ]] && [[ $line .le. $secondend ]]
then
count2=$(($count2+1))
else
count3=$(($count3+1))
fi
done
echo "the first shell has '$(($firstend-$count1))' atoms"
echo "the second shell has '$(($secondend-$firstend-$count2))' atoms"
echo "the third shell has '$(($thirdend-$secondend-$firstend-$count3' atoms"
#!/bin/bash
if [ "$2" == "" ]; then
echo 'Usage:' $0 ' '
exit 0
fi
firstend=24
secondend=108
thirdend=368
count1=0
count2=0
count3=0
cat list | while read line
do
if [[ $line .le. $firstend ]] && [[ $line .ge.1 ]]
then
count1=$(($count1+1))
elif [[ $line .gt. $firstend ]] && [[ $line .le. $secondend ]]
then
count2=$(($count2+1))
else
count3=$(($count3+1))
fi
done
echo "the first shell has '$(($firstend-$count1))' atoms"
echo "the second shell has '$(($secondend-$firstend-$count2))' atoms"
echo "the third shell has '$(($thirdend-$secondend-$firstend-$count3' atoms"
|
试试
while read line
do
...
done
while read line
do
...
done