管道输出到read中遇到的问题

1. 管道输出到read命令中, 使用管道echo输出来设置变量将会失败. 然而, 使用管道cat输出看起来能够正常运行.   cat file1 file2 |  while read line

2 .while被放置在子shell中。

1 #!/bin/sh
2 # readpipe.sh
3 #  Bjon Eriksson .
4
5 last="(null)"
6 cat $0 |
7 while read line
8 do
9 echo "{$line}"
10 last=$line
11 done
12 printf "\nAll done, last:$last\n"
13
14 exit 0 # 代码结束.
15 # 下边是脚本的(部分)输出.
16 # ‘echo‘出了多余的大括号.
17
18 #############################################
19
20 ./readpipe.sh
21
22 {#!/bin/sh}
23 {last="(null)"}
24 {cat $0 |}
25 {while read line}
26 {do}
27 {echo "{$line}"}
28 {last=$line}
29 {done}
30 {printf "nAll done, last:$lastn"}
31
32
33 All done, last:(null)
34
35 变量(last)被设置在子shell中, 并没有被设置在外边.
在许多Linux发行版上, gendiff脚本通常都在/usr/bin下, 将find的输出通
过管道传到while read结构中.
1 find $1 \( -name "*$2" -o -name ".*$2" \) -print |
2 while read f; do
3 . . .

3. 例外,在pipe中的一个大括号中的代码段可能运行在一个 子shell中   。

 ls | { read firstline; read secondline; }        echo "First line is $firstline; second line is $secondline" # 不能工作

4. 引用还可以改掉echo‘s不换行的"毛病".

bash$ echo $(ls -l)
total 8 -rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh -rw-rw-r-- 1 bozo bozo 78
Aug 21 12:57 t71.sh
bash$ echo "$(ls -l)"
total 8
-rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh
-rw-rw-r-- 1 bozo bozo 78 Aug 21 12:57 t71.sh
时间: 2024-08-01 17:40:57

管道输出到read中遇到的问题的相关文章

eImage(仅两行代码实现输出从数据库中查询到的二进制字段)标签

功能: 专门用于向浏览器输出从数据库中查询到的二进制字段.支持通用的几十种文件类型 别名为edoc 使用方法: <chtml><eimage id=书包名type=类型>key</eimage></chtml>或 <chtml><edocid==书包名 type=类型>key</edoc></chtml> ·        id:书包名.ESql标签返回的书包名称 ·        type:类型.输出文件类型

输入一个字符串,输出该字符串中对称的子字符串的最大长度。

public class LongestSymmtricalLength2 { /* * Q75题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度. * 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. */ public static void main(String[] args) { String[] strs = { "a","google", "elgoog", "agol

模板输出的数组中增加三元运算符来判断

得出一个好用的判断方法,在模板输出的数组中增加三元运算符来判断,挺方便的.当然你也可以用Ajax~ <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题</title> </head> <body> 我的口号是什么?——><{$data[

java循环练习:1.输出1~100中的所有偶数

package practiceGO; /*  * 1.输出1~100中的所有偶数  *   */ public class Cto { public static void main(String[] args) { System.out.println("1~100所有的偶数为:"); //遍历1~100 for(int i=1; i<=100; i++){ if (i%2 == 0) { System.out.println(i); } } } } 运行: 1~100所有的

【Lua】LWT遍历指定目录并输出到页面中

首先用lua遍历目录: 1 function getDirs(path) 2 local s = {} 3 function attrdir(p) 4 for file in lfs.dir(p) do 5 if file ~= "." and file ~= ".." then 6 local f = p .. file 7 local attr = lfs.attributes (f) 8 if attr.mode == "directory"

【c语言】输入一个递增排序的数组的一个旋转,输出旋转数组中的最小元素

//旋转数组的最小数字 //题目:把一个数组最開始的若干个元素搬到数组的末尾.我们称之为数组的旋转. //输入一个递增排序的数组的一个旋转.输出旋转数组中的最小元素. //比如:数组{3.4,5,1,2}为{1,2.3.4.5}的一个旋转,最小元素是1. #include <stdio.h> #include <assert.h> int min_equ(int *src, int left, int right) { int i = 0; int ret = src[left];

将java log4j的日志配置输出到mysql中

log4j可以支持将log输出到文件,数据库,甚至远程服务器,这次给大家分享下如何输出到mysql中. (1)log4j.properties的配置内容如下: log4j.rootLogger=ERROR,appender1,appender2 log4j.appender.appender1=org.apache.log4j.ConsoleAppender log4j.appender.appender1.layout=org.apache.log4j.PatternLayout log4j.

剑指offer-第三章高质量的代码(输出该链表中倒数第K个节点)

题目:输入一个链表,输出这个链表中倒数第K个节点.(代码的鲁棒性) 思路:用两个指针p1和p2,都指向头节点,开始的时候,p2不动,p1移动k-1次,指向第k个节点.此时,如果p1->next!=null,则同时移动P1和p2.直到p1指向最后一个节点.此时,P2指向倒数第k个节点. C++代码: #include<iostream> using namespace std; struct ListNode { int m_nValue; ListNode* m_pNext; }; Li

输出一个数组中最大和的子数组并且和不溢出

组员 石鹤李海超 一.题目 输出一个数组中最大和的子数组,数组为int32型,有1000个数,保证最大子数组的和不溢出. 二.设计思想 随机产生一千个数的数组,求最大子数组和的过程中判断是否溢出,如果溢出,则输出溢出. 三.源代码 #include<iostream.h> long MAX(unsigned long arry[],int length) { unsigned long sum=arry[0]; unsigned long maxsum=arry[0]; for(int i=1