開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.28 (プログラミング言語)
初めてのPerl 第7版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の13章(ディレクトリ操作)、13.4(練習問題)6の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use v5.18;
use File::Basename;
use File::Spec;
say '6.';
my ($old, $new) = @ARGV;
if (-d $new) {
$old = basename $old;
$new = File::Spec->catfile($new, $old) if -d $new;
}
link $old, $new or warn "can't link $old to $new: $!";
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ touch a.txt
$ tree
.
├── a.txt
├── sample6.pl
└── tmp
1 directory, 2 files
$ ./sample6.pl a.txt b.txt
6.
$ ls -l *.txt
-rw-r--r-- 2 {username} staff 0 8 30 15:02 a.txt
-rw-r--r-- 2 {username} staff 0 8 30 15:02 b.txt
$ ./sample6.pl a.txt c.txt
6.
$ ls -l {a,b,c}.txt
-rw-r--r-- 3 {username} staff 0 8 30 15:02 a.txt
-rw-r--r-- 3 {username} staff 0 8 30 15:02 b.txt
-rw-r--r-- 3 {username} staff 0 8 30 15:02 c.txt
$ ./sample6.pl a.txt tmp/
6.
$ ls -l *.txt
-rw-r--r-- 4 {username} staff 0 8 30 15:02 a.txt
-rw-r--r-- 4 {username} staff 0 8 30 15:02 b.txt
-rw-r--r-- 4 {username} staff 0 8 30 15:02 c.txt
$ ls -l tmp/*.txt
-rw-r--r-- 4 {username} staff 0 8 30 15:02 tmp/a.txt
$ tree
.
├── a.txt
├── b.txt
├── c.txt
├── sample6.pl
└── tmp
└── a.txt
1 directory, 5 files
$ mkdir tmp1
$ ./sample6.pl tmp/a.txt tmp1
6.
$ tree
.
├── a.txt
├── b.txt
├── c.txt
├── sample6.pl
├── tmp
│ └── a.txt
└── tmp1
└── a.txt
2 directories, 6 files
$ ls -l tmp1/*.txt
-rw-r--r-- 5 {username} staff 0 8 30 15:02 tmp1/a.txt
$ ls -l *.txt
-rw-r--r-- 5 {username} staff 0 8 30 15:02 a.txt
-rw-r--r-- 5 {username} staff 0 8 30 15:02 b.txt
-rw-r--r-- 5 {username} staff 0 8 30 15:02 c.txt
$
0 コメント:
コメントを投稿