当前位置: 技术问答>linux和unix
shell编程实例研讨:有兴趣者请进!
来源: 互联网 发布时间:2015-05-11
本文导语: #!/bin/sh #Description: search command matched with some charactor from system path #Author: #Date: 10/28/2003 echo $PATH >>./path.log cat ./path.log | grep -v '^#'| while read COMLINE do SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}' ./path.log` ech...
#!/bin/sh
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'| while read COMLINE
do
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}' ./path.log`
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
***********************************************************
上面是本人写的一个shell程序,目的是从当前系统命令中查询以"b"开始的命令,并将他们输出,很简单,但本人刚学shell编程,对于其中的一些语法技巧及命令不是很熟,导致目前程序不能正常运行,希望高手能够给予指点其中的错误,或给出更简单的做法,本人不胜感激!
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'| while read COMLINE
do
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}' ./path.log`
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
***********************************************************
上面是本人写的一个shell程序,目的是从当前系统命令中查询以"b"开始的命令,并将他们输出,很简单,但本人刚学shell编程,对于其中的一些语法技巧及命令不是很熟,导致目前程序不能正常运行,希望高手能够给予指点其中的错误,或给出更简单的做法,本人不胜感激!
|
这样:
#!/bin/sh
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
OLDIFS=IFS
IFS=":"
for SEARCH_PATH in $PATH
do
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
#!/bin/sh
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
OLDIFS=IFS
IFS=":"
for SEARCH_PATH in $PATH
do
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
|
IFS=":"
for sp in $PATH
do
find $sp -name "b*" -type f -perm -111 -print 2>/dev/null
done
for sp in $PATH
do
find $sp -name "b*" -type f -perm -111 -print 2>/dev/null
done
|
在COMLINE不知道的情况下,应当去掉while语句,实际上不必用循环也可以一次列出所有的以b开头的命令。
#!/bin/sh
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'
cat ./path.log | tr ":" " " > ./path.log
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $0}' ./path.log`
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
echo "all commond begin with "b" listed"
#!/bin/sh
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'
cat ./path.log | tr ":" " " > ./path.log
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $0}' ./path.log`
echo $SEARCH_PATH
COMMANDS= ls -ar $SEARCH_PATH | grep '^b'
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
echo "all commond begin with "b" listed"
|
to cathy97 (今夜不寂寞) :我知道你的意思了
看看这个脚本,应该好用了!
另外,脚本中RS(Record Separator)和FS(Field Separator),不可以混用。相对应的还有ORS和OFS,具体可以查相关资料
#!/bin/sh
echo $PATH > ./path.log
i=`gawk 'BEGIN{RS=":"};{print $0}' ./path.log | wc -l` #得到PATH中路径的个数
j=1
while [ $j -le $i ] #针对每一个路径,列出以b开头的命令
do
SEARCH_PATH=`gawk 'BEGIN{FS=":"};{print $'$j'}' ./path.log`
echo $SEARCH_PATH
declare -a COMMANDS=`ls -ar $SEARCH_PATH | grep '^b'`
for com in $COMANDS
do
if [ $com ]
then
echo $com
fi
done
j=`expr $j + 1`
done
看看这个脚本,应该好用了!
另外,脚本中RS(Record Separator)和FS(Field Separator),不可以混用。相对应的还有ORS和OFS,具体可以查相关资料
#!/bin/sh
echo $PATH > ./path.log
i=`gawk 'BEGIN{RS=":"};{print $0}' ./path.log | wc -l` #得到PATH中路径的个数
j=1
while [ $j -le $i ] #针对每一个路径,列出以b开头的命令
do
SEARCH_PATH=`gawk 'BEGIN{FS=":"};{print $'$j'}' ./path.log`
echo $SEARCH_PATH
declare -a COMMANDS=`ls -ar $SEARCH_PATH | grep '^b'`
for com in $COMANDS
do
if [ $com ]
then
echo $com
fi
done
j=`expr $j + 1`
done
|
#!/bin/sh
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'| while read COMLINE # COMLINE为var,"read"的结果放
# $COMLINE
do
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}' ./path.log` #这里应该没错
# 至于结果有没有,你要看 ./path.log
echo $SEARCH_PATH
COMMANDS= `ls -ar $SEARCH_PATH | grep '^b'` #我只改这里
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
#Description: search command matched with some charactor from system path
#Author:
#Date: 10/28/2003
echo $PATH >>./path.log
cat ./path.log | grep -v '^#'| while read COMLINE # COMLINE为var,"read"的结果放
# $COMLINE
do
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}' ./path.log` #这里应该没错
# 至于结果有没有,你要看 ./path.log
echo $SEARCH_PATH
COMMANDS= `ls -ar $SEARCH_PATH | grep '^b'` #我只改这里
for com in $COMMANDS
do
if [ test -n $com ]; then
echo $com
fi;
done
done
echo "all commond begin with "b" listed"
|
SEARCH_PATH=`gawk 'BEGIN {FS=":"}; {print $1}'` #$1本意是指匹配{FS=":"}的第一条字段,'n'为行标记,$PATH只是一行,$1为第一条匹配字段.
如你要用gawk,你可以这样:
SEARCH_PATH=`gawk 'BEGIN {RS=":"}; {print $1}' ./path.log` #RS为内部变量,为换行标记,有点象FS;
如你要用gawk,你可以这样:
SEARCH_PATH=`gawk 'BEGIN {RS=":"}; {print $1}' ./path.log` #RS为内部变量,为换行标记,有点象FS;