首页 > shell脚本如何读取管道数据?

shell脚本如何读取管道数据?

比如:
自己写个脚本test.sh

ls | test.sh

在test.sh里如何获取数据?


也可以使用其他命令(比如cat)获取 /dev/stdin 中的内容。


使用read

例如 如下内容的test.sh

#!/bin/sh

while read line ; do 
    echo $line
done

[root@imos controllers]# ls -al | sh test.sh
total 28
drwxr-xr-x 3 apache apache 4096 Apr 6 08:40 .
drwxr-xr-x 16 apache apache 4096 Apr 5 17:26 ..
-rw-r--r-- 1 apache apache 114 Apr 5 17:26 index.html
-rw-r--r-- 1 apache apache 859 Apr 5 17:59 summary.php
drwxr-xr-x 6 apache apache 4096 Apr 5 17:26 .svn
-rw-r--r-- 1 root root 53 Apr 6 08:39 test.sh
-rw-r--r-- 1 apache apache 770 Apr 5 17:26 welcome.php

【热门文章】
【热门文章】