Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657813
  • 博文数量: 96
  • 博客积分: 2005
  • 博客等级: 上尉
  • 技术积分: 1061
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 13:59
文章分类

全部博文(96)

文章存档

2013年(11)

2012年(30)

2011年(55)

分类:

2011-08-16 13:00:10

1. # 直接使用管道
ps -ef |while read firstvar
do
echo "firstvar : $firstvar"
secondvar=$firstvar
echo "secondvar: $secondvar"
done
# bash中循环中设置的变量firstvar无效
# bash中循环内设置的变量secondvar无效
echo "firstvar outside: $firstvar"
echo "secondvar outside: $secondvar"

2. # 使用临时文件进行改造
ps -ef >./tmp.txt
while read firstvar
do 
echo "firstvar : $firstvar"
secondvar=$firstvar
echo "secondvar: $secondvar"
done <./tmp.txt
# bash中循环中设置的变量firstvar无效
# bash中循环内设置的变量secondvar有效
echo "firstvar outside: $firstvar"
echo "secondvar outside: $secondvar"

3.# 使用here document方案解决
INPUT=`ps -ef`
while read firstvar
do 
echo "firstvar : $firstvar"
secondvar=$firstvar
echo "secondvar: $secondvar"
done <
$INPUT
EOF
# bash中循环中设置的变量firstvar无效
# bash中循环内设置的变量secondvar有效
echo "firstvar outside: $firstvar"
echo "secondvar outside: $secondvar"

4. # 不使用管道
ps -ef >/root/tmp.txt
filecount=`wc -l /root/tmp.txt|awk '{print $1}'`
count=0
while [ "$count" -le "$filecount" ]
do 
firstvar=`tail -$count /root/tmp.txt |head -1`
echo "firstvar: $firstvar"
secondvar=$firstvar
echo "secondvar: $secondvar"
count=`echo $count+1 |bc`
done <./tmp.txt
# bash中循环中设置的变量firstvar有效
# bash中循环内设置的变量secondvar有效
echo "firstvar outside: $firstvar"
echo "secondvar outside: $secondvar"

5. # 逐字处理shift左移
set `echo a b c d e`
while [ "$1" != "" ]
do
foo=$1 bar=$2
echo $*
shift
echo foo $foo
echo bar $bar
echo "---------------"
done
阅读(650) | 评论(0) | 转发(0) |
0

上一篇:copy多种方法

下一篇:shell中赋值与替换

给主人留下些什么吧!~~