shell脚本学习指南-学习(2)

1、I/O重定向符:<   >  》与管道   |   

#! /bin/bash

echo -n "Enter your name!"    //输出

printf "the first program is  '%s  %s '\n"  hello word  //比echo移植性更好,必须\n 才能换行

tr -d '\r' < dos-file.txt   

tr -d '\r' < dos-file.txt > unix-file.txt   //删除dos-file.txt中回车符,将内容重新定向输入到unix-file.txt中

for f in dos-file*.txt
 do
 tr -d '\r' <$f >>big-unix-file.txt
done       // 把dos-file.txt内容追加到big-unix-file.txt中

tr -d '\r' < dos-file.txt | sort >  unix-file2.txt     //将dos-file.txt中内容排序输出到unix-file2.txt中


exit #


重定向符号:<读取作为输入,>输出重定向;  》追加重定向   

管道:|


2、用管道写脚本,传入参数。 .findUser.sh,添加执行权限

#! /bin/bash

who | grep $1 //grep后面接收一个参数

exit #
执行./findUser.sh  #@#ing 命令,结果如下:



3、执行跟踪

set -x将执行跟踪的功能打开,当前命令执行完才打开跟踪, set +x 将执行跟踪的功能关闭,当前命令之行结束才关闭跟踪。

#!  /bin/bash
set -x
echo 1st echo
set +x
echo 2end echo
exit #
执行完毕,控制台输出:

++ echo 1st echo
1st echo
++ set +x
2end echo

4、grep文本匹配命令

用法:显示匹配一个或者多个模式的文本行,时常作为pipeline的第一步,以便于对匹配的数据作进一步处理。

有各种参数,-i  -l   -f  等

下一篇文章专门写grep。

posted @ 2014-11-12 11:14  飞扬的薰衣草  阅读(150)  评论(0编辑  收藏  举报