開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2)の13章(ディレクトリ操作)の13.13(練習問題)7を解いてみる。
その他参考書籍
7.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use File::Basename;
use File::Spec;
my $option = $ARGV[0] eq "-s";
shift @ARGV if $option;
my($src, $dst) = @ARGV;
$dst = File::Spec->catfile($dst, basename $src) if -d $dst;
if ($option) {
symlink $src, $dst or warn "can't symlink $src to $dst: $!";
} else {
link $src, $dst or warn "can't link $src to $dst: $!";
}
入出力結果(Terminal)
$ ls tmp* tmp.txt $ ./sample.pl tmp.txt tmp1.txt $ ./sample.pl -s tmp.txt tmp2.txt $ ls -l tmp* -rw-r--r--@ 2 kamimura staff 6 8 16 12:08 tmp.txt -rw-r--r--@ 2 kamimura staff 6 8 16 12:08 tmp1.txt lrwxr-xr-x 1 kamimura staff 7 8 18 15:11 tmp2.txt -< tmp.txt $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
import os, sys
option = ""
src = ""
dst = ""
if sys.argv[1] == "-s":
option, src, dst = sys.argv[1:]
else:
src, dst = sys.argv[1:]
if os.path.isdir(dst):
dst = os.path.abspath(dst) + os.path.sep + os.path.basename(src)
if option == "-s":
os.symlink(src, dst)
else:
os.link(src, dst)
入出力結果(Terminal)
$ ls tmp* tmp.txt $ ./sample.py tmp.txt tmp1.txt $ ./sample.py -s tmp.txt tmp2.txt $ ls -l tmp* -rw-r--r--@ 2 kamimura staff 6 8 16 12:08 tmp.txt -rw-r--r--@ 2 kamimura staff 6 8 16 12:08 tmp1.txt lrwxr-xr-x 1 kamimura staff 7 8 18 15:19 tmp2.txt -< tmp.txt $
0 コメント:
コメントを投稿