日期:2014-05-16  浏览次数:20531 次

shell中读取文件后的变量不能传递出来吗?
在list里有一组数字,我要判断每行数字的大小,然后输出个数,但是我读完后,发现count的变量传递不出来,这是怎么回事?

#!/bin/bash

if [ "$2" == "" ]; then
   echo 'Usage:' $0 '<trj.xyz> <newtrj.xyz>'
   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 < list
------解决方案--------------------
source 脚本
------解决方案--------------------
管道引起sub shell