-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.viki
More file actions
179 lines (134 loc) · 4.51 KB
/
Copy pathbash.viki
File metadata and controls
179 lines (134 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
* redirection
#Quote <<---
>> {
>> echo | exp file=/dev/fd/3 useri... 3>&1 >&4 |
>> gzip | split -n 2047m - exp.dmp.
>> } 4>&1
> Why needs '>&4' and '4>&1' here? What do they do?
4>&1 dupplicates the outer stdout to fd 4, >&4, aka 1>&4
restores "exp's" stdout to that same resource. So, the overall
effect is to preserve exp's stdout. Otherwise, anything written
by "exp" on its stdout would go to the pipe to gzip as well.
If you want to make things tidy, you could do:
{
echo 4>&- | exp file=/dev/fd/3 useri... 3>&1 >&4 4>&- |
gzip 4>&- | split -n 2047m - exp.dmp. 4>&-
} 4>&1
To close that fd 4 fall all the commands as none of them use it
(nor should they use it).
---
note: pipeline connect pipe-item's stdin/stdout before each redirection
_the 'outer' fd 4, is the same (or duplicate of) the 'inner' fd 4._ I THINK.
* get the string form "..."
data:
#Quote <<---
WARNING: "src/third_party/WebKit/WebKitLibraries" is no longer part of
this client. It is recommended that you manually remove it.
WARNING: "src/third_party/GTM" is no longer part of this client. It
is recommended that you manually remove it.
...
---
methods:
#Quote <<---
awk -v RS=\" '!(NR%2)'
cut -d\" -sf2 filename
awk -F\" '/"/{print $2}'
awk -F\" '/".*"/{print $2}'
sed -ne 's/.*"\([^"]*\)".*/\1/p' input
awk 'match($0, /"[^"]*"/) { print substr($0, RSTART + 1, RLENGTH - 2); }'
---
http://groups.google.com/group/comp.unix.shell/browse_thread/thread/bc8a8404d945bfa7#
* a function for running commands
I have a function to run commands and check the results:
#Quote <<----
run_or_die()
{
echo -n "Try to run '$@'... "
eval "$@"
[[ $? -eq 0 ]] && echo 'OK!' && return
echo 'Failed!'
clean_up
report_result "${TEST}" "FAIL" 0
exec 1>&6 2>&7
cat "$OUTPUTFILE"
exit -1
}
----
and then I find another similar function from here: http://groups.google.com/group/comp.unix.shell/browse_thread/thread/46b7ecaa924554c8
as this one:
#Quote <<----
run_cmd() {
cmd=$1
shift
echo 'Command line: ' "$cmd" ${1+"$@"} # has paramters if $1 being set (null or no-null)
"$cmd" ${1+"$@"}
}
----
But the second method cannot run a pipeline!!
* read: seems always discards the spaces at the end of input, except ...
#Quote <<----
[hpt@localhost temp]$ read buf <<< "hello,world ";echo "buf is: '$buf'"
buf is: 'hello,world'
[hpt@localhost temp]$ IFS= read buf <<< "hello,world ";echo "buf is: '$buf'"
buf is: 'hello,world '
[hpt@localhost temp]$ read <<< "hello,world ";echo "REPLY is: '$REPLY'"
REPLY is: 'hello,world '
----
* Why is testing x$foo = "x" better than $foo = ""
#Quote <<----
Prepeding a dummy safely prevents the values from being mistaken as
operators. Otherwise, a few very special values like "=" and "!"
become a problem in some shells:
[ -n "=" ] Bourne shell and ksh88 give the = a higher priority
and expect a comparison
[ "!" = "x" ] Bourne shell, ksh88 and some earlier dash (e.g. 0.4.26)
expect a negated comparison
BTW, test can become even more fragile, if the seldomly used operator -a
and the value "=" are used (instead of connecting two separate conditions)
[ -n "=" -a {another condition} ] breaks in almost all shells,
one exception is pdksh
http://www.in-ulm.de/~mascheck/various/test/
----
and
#Quote <<----
You're on the portable and safe side, if you can rewrite -a/-o with &&/||.
But problems are less likely if you prepend dummies and avoid negation
(which I forgot at first).
Negation is ambiguous: "! condition -a condition" can be parsed as
( ! condition ) -a condition
or
! ( condition -a condition )
----
* Get the third line based on a patterned line
Hongyi Zhao wrote:
> Hi all,
>
> I want to obtain the third line before each matched line. For
> example:
>
> ...
> _here_comes_line_a
> _here_comes_line_b
> _here_comes_line_c
> _here_comes_the_matched_line
> ...
> _here_comes_line_d
> _here_comes_line_e
> _here_comes_line_f
> _here_comes_the_matched_line
> ...
>
> I want to based on the regex matched lines to the third line before
> each matched line, i.e., the _here_comes_line_a and
> _here_comes_line_d in the above example. Any hints on this issue?
** method 1
awk '/matched_line/{print s1}{s1=s2;s2=s3;s3=$0}'
** method 2
awk '/matched/{print a[NR-3]}{a[NR]=$0}' file # this one will unnecessarily memorize the whole file...
** method 3
awk '/matched/{print a[NR%3]}{a[NR%3]=$0}'
** method 4
$ tac inputfile | awk '/matched_line/{i=4}!--i' | tac
** method 5
txr
**