sh/bash/dash/ksh/zsh等Shell脚本
-
kusamba
- 帖子: 2
- 注册时间: 2010-09-14 23:30
#1
帖子
由 kusamba » 2011-09-14 23:36
代码: 全选
#!/bin/sh
set -xv
var="this is a test"
export var
grep "a" 01.txt | while read line
do
aVar=`echo ${line} | awk '{print $1}'`
if [ "${aVar}" = "b" ]
then
var="bbb"
break
fi
done
echo $var
01.txt:
a user1@password1
b user2@password2
c user3@password3
为什么var还是"this is a test"?var在循环中确认赋过值的
-
我就是我2
- 帖子: 1215
- 注册时间: 2008-12-13 10:55
- 来自: the Earth
-
联系:
#2
帖子
由 我就是我2 » 2011-09-14 23:51
#!/bin/sh
zsh没有问题。
或者使用下面的代码
代码: 全选
#!/bin/sh
set -xv
var="this is a test"
export var
for i in `grep "a" 01.txt`;
do
.....
then
var="bbb"
break
fi
done
echo "$var"
问题应该是在read命令上。
代码: 全选
#!/bin/sh
set -xv
var="this is a test"
export var
for i in `awk '/a/ && $1=="b"{print $1}' 01.txt`;
do
if [ "${i}" = "b" ]
then
var="bbb"
break
fi
done
echo "$var"
-
ebok
- 帖子: 852
- 注册时间: 2008-02-15 0:09
#3
帖子
由 ebok » 2011-09-15 20:10
sh,bash在管道后的变量相当于局部变量,圆括号中的子shell也一样。
Somebody think they are full of niubility, so they play a zhuangbility, but only reflect their shability.
-
tusooa
- 帖子: 6548
- 注册时间: 2008-10-31 22:12
- 系统: 践兔
-
联系:
#4
帖子
由 tusooa » 2011-09-17 14:06
代码: 全选
grep "a" 01.txt | while read line
有管道,就会起子shell了。
代码: 全选
while read line ; do something ; done <<< "$(grep a 01.txt)"
这样估计可以
-
fnan
- 帖子: 919
- 注册时间: 2009-07-01 22:04
#5
帖子
由 fnan » 2011-09-17 17:07
#!/bin/sh
set -xv
var="this is a test"
export var
grep "a" 01.txt | (while read line
do
aVar=`echo ${line} | awk '{print $1}'`
if [ "${aVar}" = "b" ]
then
var="bbb"
break
fi
done
echo $var)
或者:
set -xv;var="this is a test";grep -q '^b .*a' 01.txt;if [ $? -eq 0 ]; then var="bbb";fi;echo $var
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。