benry-unixcmd 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73411bbc7b8bd31e455acdc67c7ef3d12df08ec3aa0bab56d631fcf12395b34b
4
+ data.tar.gz: 9fadc9355cf0006c83f48835c55197a4a9498a49b7e03f752af86eb951f03691
5
+ SHA512:
6
+ metadata.gz: 14462cad893ee39cc9e9ae27977b0a71c76fd2b142c2878c14f52f93f9471186a66d2ed895d8e62b53ea3565b5312cd49782d4b1e9c3a8fc0eafcc38c6c53197
7
+ data.tar.gz: 309f3813307636ff6adbe9b72f39fd01a4edf1bcf304b97ead6abd74787742795d7686fc40ededd3b97aa60816bea05dca3670a7e80a9999f6ae92b6acf126bb
data/CHANGES.md ADDED
@@ -0,0 +1,10 @@
1
+ =======
2
+ CHANGES
3
+ =======
4
+
5
+
6
+
7
+ Release X.X.X
8
+ -------------
9
+
10
+ * First public release
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 kuwata-lab.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,936 @@
1
+ benry-unixcmd Gem README
2
+ ========================
3
+
4
+ ($Release: 0.9.0 $)
5
+
6
+ benry-unixcmd gem implements popular UNIX commands, like FileUtils.
7
+
8
+ Features compared to FileUtils:
9
+
10
+ * supports file patterns (`*`, `.`, `{}`) directly.
11
+ * provides `cp :r`, `mv :p`, `rm :rf`, ... instead of `cp_r`, `mv_p`, `rm_rf`, ...
12
+ * prints command prompt `$ ` before command echoback.
13
+ * provides `pushd` which is similar to `cd` but supports nested calls naturally.
14
+ * implements `capture2`, `capture2e`, and `capture3` which calls
15
+ `Popen3.capture2`, `Popen3.capture2`, and `Popen3.capture3` respectively.
16
+ * supports `touch -r reffile`.
17
+ * provides `sys` command which is similar to `sh` in Rake but different in details.
18
+ * provides `zip` and `unzip` commands (requires `rubyzip` gem).
19
+ * provides `store` command which copies files recursively into target directory, keeping file path.
20
+ * provides `atomic_symlink!` command which switches symlink atomically.
21
+
22
+ (benry-unixcmd gem requires Ruby >= 2.3)
23
+
24
+
25
+
26
+ Table of Contents
27
+ =================
28
+
29
+ <!-- TOC -->
30
+
31
+ * <a href="#install">Install</a>
32
+ * <a href="#command-reference">Command Reference</a>
33
+ * <a href="#echo"><code>echo</code></a>
34
+ * <a href="#echoback"><code>echoback</code></a>
35
+ * <a href="#cp"><code>cp</code></a>
36
+ * <a href="#mv"><code>mv</code></a>
37
+ * <a href="#rm"><code>rm</code></a>
38
+ * <a href="#mkdir"><code>mkdir</code></a>
39
+ * <a href="#rmdir"><code>rmdir</code></a>
40
+ * <a href="#ln"><code>ln</code></a>
41
+ * <a href="#atomic_symlink"><code>atomic_symlink!</code></a>
42
+ * <a href="#touch"><code>touch</code></a>
43
+ * <a href="#chmod"><code>chmod</code></a>
44
+ * <a href="#chown"><code>chown</code></a>
45
+ * <a href="#pwd"><code>pwd</code></a>
46
+ * <a href="#store"><code>store</code></a>
47
+ * <a href="#sys"><code>sys</code></a>
48
+ * <a href="#ruby"><code>ruby</code></a>
49
+ * <a href="#capture2"><code>capture2</code></a>
50
+ * <a href="#capture2e"><code>capture2e</code></a>
51
+ * <a href="#capture3"><code>capture3</code></a>
52
+ * <a href="#zip"><code>zip</code></a>
53
+ * <a href="#unzip"><code>unzip</code></a>
54
+ * <a href="#time"><code>time</code></a>
55
+ * <a href="#faq">FAQ</a>
56
+ * <a href="#why-mv-or-cp-requires-to-option">Why <code>mv</code> or <code>cp</code> requires <code>to:</code> option?</a>
57
+ * <a href="#how-to-use-in-rakefile">How to use in Rakefile?</a>
58
+ * <a href="#how-to-change-prompt-string">How to change prompt string?</a>
59
+ * <a href="#how-to-make-prompt-colored">How to make prompt colored?</a>
60
+ * <a href="#how-to-disable-command-echoback">How to disable command echoback?</a>
61
+ * <a href="#license-and-copyright">License and Copyright</a>
62
+
63
+ <!-- /TOC -->
64
+
65
+
66
+
67
+ Install
68
+ =======
69
+
70
+ ```
71
+ $ gem install benry-unixcmd
72
+ ```
73
+
74
+ File: ex1.rb
75
+
76
+ ```ruby
77
+ require 'benry/unixcmd' # !!!!!
78
+ include Benry::UnixCommand # !!!!!
79
+
80
+ output = capture2 "ls -al" # run command and return output
81
+ #print output
82
+ ```
83
+
84
+ Result:
85
+
86
+ ```terminal
87
+ [localhost] ruby ex1.rb
88
+ $ ls -al
89
+ ```
90
+
91
+
92
+
93
+ Command Reference
94
+ =================
95
+
96
+
97
+
98
+ `echo`
99
+ ------
100
+
101
+ File: ex-echo1.rb
102
+
103
+ ```ruby
104
+ require 'benry/unixcmd'
105
+ include Benry::UnixCommand
106
+
107
+ echo "aa", "bb", "cc"
108
+
109
+ echo :n, "aa" # not print "\n"
110
+ echo "bb"
111
+ ```
112
+
113
+ Result:
114
+
115
+ ```terminal
116
+ [localhost]$ ruby ex_echo1.rb
117
+ $ echo aa bb cc
118
+ aa bb cc
119
+ $ echo -n aa
120
+ aa$ echo bb
121
+ bb
122
+ ```
123
+
124
+ Options:
125
+
126
+ * `echo :n` -- don't print "\n".
127
+
128
+
129
+
130
+ `echoback`
131
+ ----------
132
+
133
+ * `echoback "command"` prints `$ command` string into stdout.
134
+ * `echoback "command"` indents command if in block of `cd` or `pushd`.
135
+
136
+ File: ex-echoback1.rb
137
+
138
+ ```
139
+ require 'benry/unixcmd'
140
+ include Benry::UnixCommand
141
+
142
+ echoback "command 123"
143
+ cd "dir1" do
144
+ echoback "command 456"
145
+ cd "dir2" do
146
+ echoback "command 789"
147
+ end
148
+ end
149
+ ```
150
+
151
+ Result:
152
+
153
+ ```terminal
154
+ [localhost]$ ruby ex_echoback1.rb
155
+ $ command 123
156
+ $ cd dir1
157
+ $ command 456
158
+ $ cd dir2
159
+ $ command 789
160
+ $ cd -
161
+ $ cd -
162
+ ```
163
+
164
+
165
+
166
+ `cp`
167
+ ----
168
+
169
+ * `cp "x", "y"` copies `x` to new file `y'. Fails when `y` already exists.
170
+ * `cp! "x", "y"` is similar to above, but overwrites `y` even if it exists.
171
+ * `cp "x", "y", to: "dir"` copies `x` and `y` into `dir`.
172
+ * `cp "x", "y", "dir"` will be error! (use `to: "dir"` instead.)
173
+ * Glob pattern such as `*`, `**`, `?`, and `{}` are available.
174
+ * (See [FAQ](#faq) about `to:` keyword option.)
175
+
176
+ <!--
177
+ File: ex-cp1.rb
178
+ -->
179
+
180
+ ```ruby
181
+ require 'benry/unixcmd'
182
+ include Benry::UnixCommand
183
+
184
+ ## copy file to newfile
185
+ cp "file1.txt", "newfile.txt" # error if newfile.txt already exists.
186
+ cp! "file1.txt", "newfile.txt" # overrides newfile.txt if exists.
187
+
188
+ ## copy dir to newdir recursively
189
+ cp :r, "dir1", "newdir" # error if newdir already exists.
190
+
191
+ ## copy files to existing directory
192
+ cp :pr, "file*.txt", "lib/**/*.rb", to: "dir1" # error if dir1 not exist.
193
+ ```
194
+
195
+ Options:
196
+
197
+ * `cp :p` -- preserves timestamps and permission.
198
+ * `cp :r` -- copies files and directories recursively.
199
+ * `cp :l` -- creates hard links instead of copying files.
200
+ * `cp :f` -- ignores non-existing source files.
201
+ Notice that this is different from `cp -f` of unix command.
202
+
203
+
204
+
205
+ `mv`
206
+ ----
207
+
208
+ * `mv "x", "y"` renames `x` to `y`. Fails when `y` already exists.
209
+ * `mv! "x", "y"` is similar to above, but overwrites `y` even if it exists.
210
+ * `mv "x", "y", to: "dir"` moves `x` and `y` into `dir`.
211
+ * `mv "x", "y", "dir"` will be error! (use `to: "dir"` instead.)
212
+ * Glob patten such as `*`, `**`, `?`, and `{}` are available.
213
+ * (See [FAQ](#faq) about `to:` keyword option.)
214
+
215
+ <!--
216
+ File: ex-mv1.rb
217
+ -->
218
+
219
+ ```ruby
220
+ require 'benry/unixcmd'
221
+ include Benry::UnixCommand
222
+
223
+ ## rename file
224
+ mv "file1.txt", "newfile.txt" # error if newfile.txt already exists.
225
+ mv! "file1.txt", "newfile.txt" # overrides newfile.txt if exists.
226
+
227
+ ## rename directory
228
+ mv "dir1", "newdir" # error if newdir already exists.
229
+
230
+ ## move files and directories to existing directory
231
+ mv "file*.txt", "lib", to: "dir1" # error if dir1 not exist.
232
+
233
+ ## ignore non-existing files.
234
+ mv "foo*.txt", to: "dir1" # error if foo*.txt not exist.
235
+ mv :f, "foo*.txt", to: "dir1" # not error even if foo*.txt not exist.
236
+ ```
237
+
238
+ Options:
239
+
240
+ * `mv :f` -- ignores non-existing source files.
241
+
242
+
243
+
244
+ `rm`
245
+ ----
246
+
247
+ * `rm "x", "y"` removes file `x` and `y`.
248
+ * `rm :r, "dir1"` removes directory recursively.
249
+ * `rm "dir1"` will raise error because `:r` option not specified.
250
+ * `rm "foo*.txt"` will raise error if `foo*.txt` not exists.
251
+ * `rm :f, "foo*.txt"` will not raise error even if `foo*.txt` not exists.
252
+ * Glob patten such as `*`, `**`, `?`, and `{}` are available.
253
+
254
+ <!--
255
+ File: ex-rm1.rb
256
+ -->
257
+
258
+ ```ruby
259
+ require 'benry/unixcmd'
260
+ include Benry::UnixCommand
261
+
262
+ ## remove files
263
+ rm "foo*.txt", "bar*.txt" # error if files not exist.
264
+ rm :f, "foo*.txt", "bar*.txt" # not error even if files not exist.
265
+
266
+ ## remove directory
267
+ rm :r, "dir1" # error if dir1 not exist.
268
+ rm :rf, "dir1" # not error even if dir1 not exist.
269
+ ```
270
+
271
+ Options:
272
+
273
+ * `rm :r` -- remove files and directories recursively.
274
+ * `rm :f` -- ignores non-existing files and directories.
275
+
276
+
277
+
278
+ `mkdir`
279
+ -------
280
+
281
+ * `mkdir "x", "y"` creates `x` and `y` directories.
282
+ * `mkdir :p, "x/y/z"` creates `x/y/z` directory.
283
+ * `mkdir "x"` will be error if `x` already exists.
284
+ * `mkdir :p, "x"` will not be error even if `x` already exists.
285
+ * `mkdir :m, 0775, "x"` creates new directory with permission 0775.
286
+
287
+ <!--
288
+ File: ex-mkdir1.rb
289
+ -->
290
+
291
+ ```ruby
292
+ require 'benry/unixcmd'
293
+ include Benry::UnixCommand
294
+
295
+ ## creates new directory
296
+ mkdir "newdir"
297
+
298
+ ## creates new directory with path
299
+ mkdir :p, "dir/x/y/z"
300
+
301
+ ## creats new directory with specific permission
302
+ mkdir :m, 0755, "newdir"
303
+ ```
304
+
305
+ Options:
306
+
307
+ * `mkdir :p` -- creates intermediate path.
308
+ * `mkdir :m, 0XXX` -- specifies directory permission.
309
+
310
+
311
+
312
+ `rmdir`
313
+ -------
314
+
315
+ * `rmdir "x", "y"` removed empty directores.
316
+ * Raises error when directory not empty.
317
+
318
+ <!--
319
+ File: ex-rmdir1.rb
320
+ -->
321
+
322
+ ```ruby
323
+ require 'benry/unixcmd'
324
+ include Benry::UnixCommand
325
+
326
+ ## remove empty directory
327
+ rmdir "dir" # error if directory not empty.
328
+ ```
329
+
330
+ Options:
331
+
332
+ * (no options)
333
+
334
+
335
+
336
+ `ln`
337
+ ----
338
+
339
+ * `ln "x", "y"` creates hard link.
340
+ * `ln :s, "x", "y"` creates symbolic link. Error if `y` already exists.
341
+ * `ln! :s, "x", "y"` overwrites existing symbolic link `y`.
342
+ * `ln "files*.txt', to: "dir"` creates hard links into `dir`.
343
+ * `ln "files*.txt', "dir"` will be error! (use `to: "dir"` instead.)
344
+ * (See [FAQ](#faq) about `to:` keyword option.)
345
+
346
+ <!--
347
+ File: ex-ln1.rb
348
+ -->
349
+
350
+ ```ruby
351
+ require 'benry/unixcmd'
352
+ include Benry::UnixCommand
353
+
354
+ ## create hard link
355
+ ln "foo1.txt", "dir/foo1.txt"
356
+
357
+ ## create symbolic link
358
+ ln :s, "foo1.txt", "dir/foo1.txt" # error if dir/foo1.txt alreay exists.
359
+ ln! :s, "foo1.txt", "dir/foo1.txt" # overwrites dir/foo1.txt if exists.
360
+
361
+ ## create symbolic link into directory.
362
+ ln :s, "foo1.txt", to: "dir"
363
+
364
+ ## error! use `to: "dir"` instead.
365
+ ln :s, "foo1.txt", "dir"
366
+ ```
367
+
368
+
369
+
370
+ `atomic_symlink!`
371
+ -----------------
372
+
373
+ * `atomic_symlink! "x", "y"` creates symbolic link atomically.
374
+
375
+ <!--
376
+ File: ex-atomic_symlink1.rb
377
+ -->
378
+
379
+ ```ruby
380
+ require 'benry/unixcmd'
381
+ include Benry::UnixCommand
382
+
383
+ ## create symbolic link atomically
384
+ atomic_symlink! "src-20200101", "src"
385
+
386
+ ## the above is same as the following
387
+ tmplink = "src.#{rand().to_s[2..6]}" # random name
388
+ File.symlink("src-20200101", tmplink) # create symblink with random name
389
+ File.rename(tmplink, "src") # rename symlink atomically
390
+ ```
391
+
392
+ Options:
393
+
394
+ * (no options)
395
+
396
+
397
+
398
+ `touch`
399
+ -------
400
+
401
+ * `touch "x"` updates timestamp of file.
402
+ * `touch :r, "reffile", "x"` uses timestamp of `reffile` instead current timestamp.
403
+
404
+ <!--
405
+ File: ex-touch1.rb
406
+ -->
407
+
408
+ ```ruby
409
+ require 'benry/unixcmd'
410
+ include Benry::UnixCommand
411
+
412
+ ## updates timestamp of files to current timestamp.
413
+ touch "files*.txt"
414
+
415
+ ## copy timestamp from reffile to other files.
416
+ touch :r, "reffile", "files*.txt"
417
+ ```
418
+
419
+ Options:
420
+
421
+ * `touch :a` -- updates only access time.
422
+ * `touch :m` -- updates only modification time.
423
+ * `touch :r, "reffile"` -- uses timestamp of `reffile` instead of current timestamp.
424
+
425
+
426
+
427
+ `chmod`
428
+ -------
429
+
430
+ * `chmod 0644, "x"` changes file permission.
431
+ * `chmod :R, "a+r", "dir"` changes permissions recursively.
432
+ * Permission can be `0644` sytle, or `u+w` style.
433
+
434
+ <!--
435
+ File: ex-chmod1.rb
436
+ -->
437
+
438
+ ```ruby
439
+ require 'benry/unixcmd'
440
+ include Benry::UnixCommand
441
+
442
+ ## change permissions of files.
443
+ chmod 0644, "file*.txt"
444
+ chmod "a+r", "file*.txt"
445
+
446
+ ## change permissions recursively.
447
+ chmod :R, 0644, "dir"
448
+ chmod :R, "a+r", "dir"
449
+ ```
450
+
451
+ Optionns:
452
+
453
+ * `chmod :R` -- changes permissions recursively.
454
+
455
+
456
+
457
+ `chown`
458
+ -------
459
+
460
+ * `chown "user:group", "x", "y"` changes owner and group of files.
461
+ * `chown "user", "x", "y"` changes owner of files.
462
+ * `chown ":group", "x", "y"` changes group of files.
463
+ * `chown :R, "user:group", "dir"` changes owner and group recursively.
464
+
465
+ <!--
466
+ File: ex-chown1.rb
467
+ -->
468
+
469
+ ```ruby
470
+ require 'benry/unixcmd'
471
+ include Benry::UnixCommand
472
+
473
+ ## change owner and/or group.
474
+ chown "user1:group1", "file*.txt" # change both owner and group
475
+ chown "user1", "file*.txt" # change owner
476
+ chown ":group1", "file*.txt" # change group
477
+ ```
478
+
479
+ Optionns:
480
+
481
+ * `chown :R` -- changes owner and/or group recursively.
482
+
483
+
484
+
485
+ `pwd`
486
+ -----
487
+
488
+ * `pwd()` prints current working directory path.
489
+
490
+ <!--
491
+ File: ex-pwd1.rb
492
+ -->
493
+
494
+ ```ruby
495
+ require 'benry/unixcmd'
496
+ include Benry::UnixCommand
497
+
498
+ ## prints current working directory
499
+ pwd()
500
+ ```
501
+
502
+ Options:
503
+
504
+ * (no options)
505
+
506
+
507
+
508
+ `store`
509
+ -------
510
+
511
+ * `store "x", "y", to: "dir", ` copies files under `x` and `y` to `dir` keeping file path.
512
+ For example, `x/foo/bar.rb` will be copied as `dir/x/foo/bar.rb`.
513
+ * `store!` overwrites existing files while `store` doesn't.
514
+
515
+ <!--
516
+ File: ex-store1.rb
517
+ -->
518
+
519
+ ```ruby
520
+ require 'benry/unixcmd'
521
+ include Benry::UnixCommand
522
+
523
+ ## copies files into builddir, keeping file path
524
+ store "lib/**/*.rb", "test/**/*.rb", to: "builddir"
525
+ ```
526
+
527
+ Options:
528
+
529
+ * `store :p` -- preserves timestamps, permission, file owner and group.
530
+ * `store :l` -- creates hard link instead of copying file.
531
+ * `store :f` -- ignores non-existing files.
532
+
533
+
534
+
535
+ `sys`
536
+ -----
537
+
538
+ * `sys "ls -al"` runs `ls -al` command.
539
+ * `sys` raises error when command failed.
540
+ * `sys!` ignores error even when command failed.
541
+ * `sys` and `sys!` return `Process::Status` object regardless of command result.
542
+ * `sys` and `sys!` can take a block argument as error handler called only when command failed.
543
+ If result of block argument is truthy, error will not be raised.
544
+
545
+ <!--
546
+ File: ex-sys1.rb
547
+ -->
548
+
549
+ ```ruby
550
+ require 'benry/unixcmd'
551
+ include Benry::UnixCommand
552
+
553
+ ## run `ls` command
554
+ sys "ls foo.txt" # may raise error when command failed
555
+ sys! "ls foo.txt" # ignore error even when command filed
556
+
557
+ ## error handling
558
+ sys "ls /fooobarr" do |stat| # block called only when command failed
559
+ p stats.class #=> Process::Status
560
+ p stat.exitstatus #=> 1 (non-zero)
561
+ true # suppress raising error
562
+ end
563
+ ```
564
+
565
+ Options:
566
+
567
+ * (no options)
568
+
569
+
570
+
571
+ `ruby`
572
+ ------
573
+
574
+ * `ruby "...."` is almost same as `sys "ruby ...."`.
575
+ * `RbConfig.ruby` is used as ruby command path.
576
+ * `ruby` raises error when ruby command failed.
577
+ * `ruby!` ignores error even when ruby command failed.
578
+
579
+ <!--
580
+ File: ex-ruby1.rb
581
+ -->
582
+
583
+ ```ruby
584
+ require 'benry/unixcmd'
585
+ include Benry::UnixCommand
586
+
587
+ ## run ruby command
588
+ ruby "file1.rb" # raise error when ruby command failed
589
+ ruby! "file1.rb" # ignore error even when ruby command filed
590
+ ```
591
+
592
+ Options:
593
+
594
+ * (no options)
595
+
596
+
597
+
598
+ `capture2`
599
+ ----------
600
+
601
+ * `capture2 "ls -al"` runs `ls -al` and returns output of the command.
602
+ * `capture2 "cat -n", stdin_data: "A\nB\n"` run `cat -n` command and uses `"A\nB\n"` as stdin data.
603
+ * `caputre "ls foo"` will raise error when command failed.
604
+ * `caputre! "ls foo"` ignores error even when command failed, and returns command output and process status object.
605
+ * `capture2()` invokes `Popen3.capture2()` internally. All keyword arguments are available.
606
+
607
+ <!--
608
+ File: ex-capture2.rb
609
+ -->
610
+
611
+ ```ruby
612
+ require 'benry/unixcmd'
613
+ include Benry::UnixCommand
614
+
615
+ ## run command and get output of the command.
616
+ output = capture2 "ls -l foo.txt" # error if command failed
617
+ output, process_status = capture2 "ls -l foot.xt" # ignore error even command failed
618
+ puts process_status.exitstatus
619
+
620
+ ## run command with stdin data.
621
+ input = "AA\nBB\nCC\n"
622
+ output = capture2 "cat -n", stdin_data: input
623
+ ```
624
+
625
+ Options:
626
+
627
+ * see [`Popen3.capture2()` manual page](https://docs.ruby-lang.org/en/master/Open3.html#method-c-capture2).
628
+
629
+
630
+
631
+ `capture2e`
632
+ -----------
633
+
634
+ * almost same as `capture2`, but output contains both stdout and stderr.
635
+
636
+ <!--
637
+ File: ex-capture2e.rb
638
+ -->
639
+
640
+ ```ruby
641
+ require 'benry/unixcmd'
642
+ include Benry::UnixCommand
643
+
644
+ ## run command and get output of the command, including stderr.
645
+ output = capture2e "time ls -al"
646
+ output, process_status = capture2e! "time ls -al" # ignore error even command failed
647
+ puts process_status.exitstatus
648
+ ```
649
+
650
+ Options:
651
+
652
+ * see [`Popen3.capture2e()` manual page](https://docs.ruby-lang.org/en/master/Open3.html#method-c-capture2e).
653
+
654
+
655
+
656
+ `capture3`
657
+ ----------
658
+
659
+ * almost same as `capture3`, but returns both stdout output and stderr output.
660
+
661
+ <!--
662
+ File: ex-capture3.rb
663
+ -->
664
+
665
+ ```ruby
666
+ require 'benry/unixcmd'
667
+ include Benry::UnixCommand
668
+
669
+ ## run command and get output of both stdout and stderr separately
670
+ output, error = capture3 "time ls -al"
671
+ output, error, process_status = capture3! "time ls -al" # ignore error even command failed
672
+ puts process_status.exitstatus
673
+
674
+ ## run command with stdin data.
675
+ input = "AA\nBB\nCC\n"
676
+ output, error = capture3 "cat -n", stdin_data: input
677
+ ```
678
+
679
+ Options:
680
+
681
+ * see [`Popen3.capture3()` manual page](https://docs.ruby-lang.org/en/master/Open3.html#method-c-capture3).
682
+
683
+
684
+
685
+ `zip`
686
+ -----
687
+
688
+ * `zip "foo.zip", "file1", "file2"` creates new zip file `foo.zip`.
689
+ * `zip :r, "foo.zip", "dir1"` adds files under `dir1` into zip file recursively.
690
+ * `zip` will be error if zip file already exists.
691
+ * `zip!` will overwrite existing zip file.
692
+ * `zip :'0'` doesn't compress files.
693
+ * `zip :'1'` compress files in best speed.
694
+ * `zip :'9'` compress files in best compression level.
695
+ * `zip` and `zip!` requires `rubyzip` gem. You must install it by yourself.
696
+ * `zip` and `zip!` doesn't support absolute path.
697
+
698
+ <!--
699
+ File: ex-zip1.rb
700
+ -->
701
+
702
+ ```ruby
703
+ require 'benry/unixcmd'
704
+ include Benry::UnixCommand
705
+
706
+ ## create zip file
707
+ zip "foo.zip", "file*.txt" # requires 'rubyzip' gem
708
+
709
+ ## create zip file, adding files under directory
710
+ zip :r, "foo.zip", "dir1"
711
+
712
+ ## create high-compressed zip file
713
+ zip :r9, "foo.zip", "dir1"
714
+ ```
715
+
716
+ Options:
717
+
718
+ * `zip :r` -- adds files under directory into zip file recursively.
719
+ * `zip :'0'` -- not compress files.
720
+ * `zip :'1'` -- compress files in best speed.
721
+ * `zip :'9'` -- compress files in best compression level.
722
+
723
+
724
+
725
+ `unzip`
726
+ -------
727
+
728
+ * `unzip "foo.zip"` extracts files in zip file into current directory.
729
+ * `unzip :d, "dir1", "foo.zip"` extracts files under `dir1`.
730
+ Diretory `dir1` should not exist or should be empty.
731
+ * `unzip "foo.zip"` will be error if extracting file already exists.
732
+ * `unzip! "foo.zip"` will overwrite existing files.
733
+ * `unzip "foo.txt", "file1", "file2"` extracts only `file1` and `file2`.
734
+ * `zunip` and `unzip!` requires `rubyzip` gem. You must install it by yourself.
735
+ * `unzip` and `unzip!` doesn't support absolute path.
736
+
737
+ <!--
738
+ File: ex-unzip1.zip
739
+ -->
740
+
741
+ ```ruby
742
+ require 'benry/unixcmd'
743
+ include Benry::UnixCommand
744
+
745
+ ## extracts zip file
746
+ unzip "foo.zip" # requires 'rubyzip' gem
747
+
748
+ ## extracts files in zip file into the directory.
749
+ unzip :d, "dir1", "foo.zip" # 'dir1' should be empty, or should not exist
750
+
751
+ ## overwrites existing files.
752
+ unzip! "foo.zip"
753
+ ```
754
+
755
+ Options:
756
+
757
+ * `unzip :d, "dir1"` -- extracts files into the directory.
758
+
759
+
760
+
761
+ `time`
762
+ ------
763
+
764
+ * `time do ... end` invokes block and prints elapsed time into stderr.
765
+
766
+ File: ex-time1.rb
767
+
768
+ ```ruby
769
+ require 'benry/unixcmd'
770
+ include Benry::UnixCommand
771
+
772
+ time do
773
+ sh "zip -qr9 dir1.zip dir1"
774
+ end
775
+ ```
776
+
777
+ Result:
778
+
779
+ ```termianl
780
+ [localhost]$ ruby ex-time1.rb
781
+ $ zip -qr9 dir1.zip dir1
782
+
783
+ 1.511s real 1.501s user 0.006s sys
784
+ ```
785
+
786
+
787
+
788
+ FAQ
789
+ ====
790
+
791
+
792
+
793
+ Why `mv` or `cp` requires `to:` option?
794
+ ---------------------------------------
795
+
796
+ Because UNIX command has bad interface which causes unexpected result.
797
+
798
+ For example, `mv` command of UNIX has two function: **rename** and **move**.
799
+
800
+ * rename: `mv foo bar` (if `bar` is a file or not exist)
801
+ * move: `mv foo bar` (if directory `bar` already exists)
802
+
803
+ Obviously, rename function and move function are same form.
804
+ This causes unexpected result easily due to, for example, typo.
805
+
806
+ ```terminal
807
+ ### Assume that you want rename 'foo' file to 'bar'.
808
+ ### But if 'bar' exists as directory, mv command moves 'foo' into 'bar'.
809
+ ### In this case, mv command should be error.
810
+ $ mv foo bar
811
+ ```
812
+
813
+ To avoid this unexpected result, `mv()` command of Benry::UnixCommand handles two functions in different forms.
814
+
815
+ * rename: `mv "foo", "bar"` (error if directory `bar` exists)
816
+ * move: `mv "foo", to: "bar"` (error if 'bar' is a file or not exist)
817
+
818
+ In the same reason, `cp()` and `ln()` of Benry::UnixCommand also requires `to:` option.
819
+
820
+
821
+
822
+ How to use in Rakefile?
823
+ -----------------------
824
+
825
+ File: Rakefile
826
+
827
+ ```ruby
828
+ require 'benry/unixcmd' # !!!!!
829
+ include Benry::UnixCommand # !!!!!
830
+ Rake::DSL.prepend Benry::UnixCommand # !!!!!
831
+
832
+ task :example do
833
+ mkdir :p, "foo/bar/baz"
834
+ here = Dir.pwd()
835
+ pushd "foo/bar/baz" do
836
+ output = capture2 "pwd"
837
+ puts output.sub(here+"/", "")
838
+ end
839
+ end
840
+ ```
841
+
842
+ Result:
843
+
844
+ ```terminal
845
+ [localhost]$ rake example
846
+ $ mkdir -p foo/bar/baz
847
+ $ pushd foo/bar/baz
848
+ $ pwd
849
+ foo/bar/baz
850
+ $ popd # back to /home/yourname
851
+ ```
852
+
853
+
854
+
855
+ How to change prompt string?
856
+ ----------------------------
857
+
858
+ File: ex-prompt1.rb
859
+
860
+ ```ruby
861
+ require 'benry/unixcmd'
862
+ include Benry::UnixCommand
863
+
864
+ def prompt() # !!!!!
865
+ "myname@localhost>" # !!!!!
866
+ end # !!!!!
867
+
868
+ sh "date"
869
+ ```
870
+
871
+ Result:
872
+
873
+ ```terminal
874
+ [localhost]$ ruby ex-prompt1.rb
875
+ myname@localhost> date
876
+ Wed Jan 15 20:23:07 UTC 2021
877
+ ```
878
+
879
+
880
+
881
+ How to make prompt colored?
882
+ ---------------------------
883
+
884
+ <!--
885
+ File: ex-prompt2.rb
886
+ -->
887
+
888
+ ```ruby
889
+ require 'benry/unixcmd'
890
+ include Benry::UnixCommand
891
+
892
+ def prompt()
893
+ s = "myname@localhost>"
894
+ "\e[0;31m#{s}\e[0m" # red
895
+ #"\e[0;32m#{s}\e[0m" # green
896
+ #"\e[0;33m#{s}\e[0m" # yellow
897
+ #"\e[0;34m#{s}\e[0m" # blue
898
+ #"\e[0;35m#{s}\e[0m" # magenta
899
+ #"\e[0;36m#{s}\e[0m" # cyan
900
+ #"\e[0;37m#{s}\e[0m" # white
901
+ end
902
+
903
+ sh "date"
904
+ ```
905
+
906
+
907
+
908
+ How to disable command echoback?
909
+ --------------------------------
910
+
911
+ File: ex-quiet1.rb
912
+
913
+ ```ruby
914
+ require 'benry/unixcmd'
915
+ include Benry::UnixCommand
916
+
917
+ BENRY_ECHOBACK = false # !!!!!
918
+
919
+ sh "date"
920
+ ```
921
+
922
+ Result:
923
+
924
+ ```terminal
925
+ $ ruby ex-quiet1.rb
926
+ Wed Jan 1 22:29:55 UTC 2020 # no echoback, only output
927
+ ```
928
+
929
+
930
+
931
+ License and Copyright
932
+ =====================
933
+
934
+ $License: MIT License $
935
+
936
+ $Copyright: copyright(c) 2021 kuwata-lab.com all rights reserved $