ターミナルの画面を消去する方法
特別なインストールなしで実現したい。
- POSIX互換環境(Linux/Unix/Mac): clear
- Windows
- コマンドプロンプト (cmd.exe): cls
- CygWin (2011以前): cmd /c cls
- CygWin (mintty): echo -en "\ec"
CygWin の闇は深い。
ターミナルの画面を消去する方法
特別なインストールなしで実現したい。
CygWin の闇は深い。
UNIX: find –mtime コマンドについて
あるディレクトリ配下でn日間更新されていないファイルを削除する場合、などのケースで
「find」コマンドの「-mtime」オプションを使うのは古来からある由緒正しき方法である。
例えばこのように。
find target -mtime +7 |xargs rm -f
このとき、n日前に更新されたファイルは削除されるのか?
やや直感的でない部分があるので整理しておく。
man ページを見たところ、処理のポイントは以下2点である。
まず、検査対象のファイルそれぞれに対して「find コマンドが実行された時刻」から「ファイルの最終更新時刻」が差し引かれた値(秒単位)が求められる。
そして、それが何日分に相当するかが算出される。
このとき 1日に満たない時間は切り捨てられる。(よって結果は必ず整数となる)
例えば、1秒前に更新されたファイルなら「0」、ちょうど3日前(3*24*60*60=259,200秒前)に更新されたファイルなら「3」、
その1秒後(現在から259,199秒前)に更新されたファイルなら「2」となる。
そして、パラメータとして指定された値と比較される。ここで符号は次のように解釈される。
+ : 指定された値より大きい場合にマッチ
符号なし: 指定された値と一致する場合のみマッチ
- : 指定された値より小さい場合にマッチ
例えば、符号を付けずに「-mtime 3」と指定した場合は上記の1.の計算結果が「3」のものだけが
find コマンドの結果として得られることとなる。
「-mtime -3」であれば「2」以下のものが、「-mtime +3」であれば「4」以上のものが抽出される。
従って、コマンドの結果は以下の図のようになる。(図で白い丸は端点を含まないこと、黒い丸は端点を含むことを表す)
一般に、「-mtime +n」と指定した場合、最終更新時刻がちょうど(n+1)日前よりも古いファイルが抽出される。
(秒単位でちょうど(n+1)日前のものも含む)
ファイルの状態と現在時刻
$ ls -l total 4 -rw-r--r-- 1 mog staff 0 2012-04-03 00:00 04030000.txt -rw-r--r-- 1 mog staff 0 2012-04-04 00:00 04040000.txt -rw-r--r-- 1 mog staff 0 2012-04-05 00:00 04050000.txt -rw-r--r-- 1 mog staff 0 2012-04-06 00:00 04060000.txt -rw-r--r-- 1 mog staff 0 2012-04-07 00:00 04070000.txt -rw-r--r-- 1 mog staff 0 2012-04-08 00:00 04080000.txt -rw-r--r-- 1 mog staff 0 2012-04-09 00:00 04090000.txt -rw-r--r-- 1 mog staff 0 2012-04-10 00:00 04100000.txt $ date 2012年04月09日 (月) 23時48分58秒 JST $
find コマンド実行結果
$ find *.txt -mtime +3 -ls 235 1 -rw-r--r-- 1 mog staff 0 4月 3 00:00 04030000.txt 234 1 -rw-r--r-- 1 mog staff 0 4月 4 00:00 04040000.txt 184 1 -rw-r--r-- 1 mog staff 0 4月 5 00:00 04050000.txt $ find *.txt -mtime 3 -ls 183 1 -rw-r--r-- 1 mog staff 0 4月 6 00:00 04060000.txt $ find *.txt -mtime -3 -ls 134 1 -rw-r--r-- 1 mog staff 0 4月 7 00:00 04070000.txt 186 1 -rw-r--r-- 1 mog staff 0 4月 8 00:00 04080000.txt 190 1 -rw-r--r-- 1 mog staff 0 4月 9 00:00 04090000.txt 198 1 -rw-r--r-- 1 mog staff 0 4月 10 00:00 04100000.txt $
find コマンド開始時刻の取得
if (time(&now) == (time_t)(-1)) {
(void) fprintf(stderr, gettext("%s: time() %s\n"),
cmdname, strerror(errno));
exit(1);
}
ファイルの最終更新時刻との差分算出
case MTIME:
t = statb->st_mtime;
days:
l = (now-t)/A_DAY;
goto num;
結果の比較
num:
if (np->second.i == '+')
val = (l > np->first.l);
else if (np->second.i == '-')
val = (l < np->first.l);
else
val = (l == np->first.l);
break;
最後に、やや特殊な例であるがファイルの更新日時が find コマンドの実行時刻よりも新しい場合も考えられる。
実装はカーネルに依存する部分も多いが、それらはマイナス値として正しく判定されるよう工夫がなされているようだ。
参考:
OpenSolaris の find コマンドのソース
http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/find/
GNU project 'findutils'
http://ftp.gnu.org/pub/gnu/findutils/
UNIX: 全ての行の逆順表示
例えばファイルリストの逆順を手っ取り早く求めたい時、ファイルの内容を逆から読みたい時、
以下のコマンドを利用すればよい。
Linux (GNU): tac
Solaris : tail -r
UNIX: GZIP/BZIP2 環境変数の罠
環境変数 GZIP / BZIP2 には特別な効果があり、定義するとそれぞれ gzip / bzip2 コマンドを実行したときに
デフォルトのパラメータとして評価されるようになる。
これを知らずにこのようなシェルを書いてしまうと・・・
GZIP=/usr/bin/gzip $GZIP foo.txt実際にはこのようなコマンドを実行したのと同じである。
/usr/bin/gzip /usr/bin/gzip foo.txt図らずも、gzip プログラム本体の圧縮が試みられる結果となる。
参考:
http://www.gnu.org/software/gzip/manual/gzip.html#Environment
HTA、他: 自分自身のフルパスを取得する方法
スクリプトファイルの自分自身のパスと、その親ディレクトリを取得する方法の一覧。
実現方法はいずれも一例である。
・HTA (HTML Applications) – VBScript
path = unescape(window.location.pathname)
If window.location.host <> "" Then path = "\\" & window.location.host & path
dir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(path)・VBScript
path = WScript.ScriptFullName
dir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(path)WScript.Echo path & vbCrLf & dir
・Bourne Shell
path=`(cd \`dirname $0\` && pwd)`/`basename $0`
dir=`dirname $path`
echo "${path}\n${dir}"・Perl
use File::Basename;
use File::Spec;
$path = File::Spec->rel2abs(__FILE__);
$dir = dirname $path;
print "$path\n$dir\n";import os
path = os.path.abspath(__file__)
dir = os.path.dirname(path)
print '%s\n%s' % (path, dir)
__file__ にフルパスが入るかは処理系依存の模様。
import sys を行なってから sys.argv[0] を参照する方法でも代替可能。
・Windows バッチファイル ※2012/04/01追記
@echo off
set full_path=%~f0set dir=%~dp0echo %full_path%
echo %dir%
Bシェル: 相対パスから絶対パスを得る方法
dirname コマンドで得られるディレクトリに cd し、pwd コマンドを行うことで絶対パスを得ることができる。
このときサブシェル内で実行することで、実際のシェルプロセスには影響を与えないようにする。
・例1 自分自身の居場所を特定する
ABSPATH=`(cd \`/usr/bin/dirname $0\` && pwd)`/`/usr/bin/basename $0`
・例2 自分自身の親ディレクトリを特定する
PARENTDIR=`(cd \`/usr/bin/dirname $0\`/.. && pwd)`
プロジェクトごとディレクトリを移動する場合などに便利。
参考:
http://dokonoumanohone.blog47.fc2.com/blog-entry-2.html
http://blog.hansode.org/archives/51481467.html
GCログの整形 その2
GCログのタイムスタンプ(JavaVMを起動してからの秒数)をシステム時刻に整形するスクリプト。
ファイルの最終更新時刻と最終エントリのタイムスタンプを突き合わせる発想は凄い。
http://www.theserverlabs.com/blog/2010/05/26/human-readable-jvm-gc-timestamps/
以下転載
#!/usr/bin/env python
import sys, os, datetime
# true if string is a positive float
def validSeconds(str_sec):
try:
return 0 < float(str_sec)
except ValueError:
return False
# show usage
if len(sys.argv) < 2:
print "Usage: %s <gc.log>" % (sys.argv[0])
sys.exit(1)
file_str = sys.argv[1]
lastmod_date = datetime.datetime.fromtimestamp(os.path.getmtime(file_str))
file = open(file_str, 'r')
lines = file.readlines()
file.close()
# get last elapsed time
for line in reversed(lines):
parts = line.split(':')
if validSeconds(parts[0]):
break
# calculate start time
start_date = lastmod_date - datetime.timedelta(seconds=float(parts[0]))
# print file prepending human readable time where appropiate
for line in lines:
parts = line.split(':')
if not validSeconds(parts[0]):
print line.rstrip()
continue
line_date = start_date + datetime.timedelta(seconds=float(parts[0]))
print "%s: %s" % (line_date.isoformat(), line.rstrip())
GCログの整形 その1
GCログから数値だけを抽出したい場合、sed で文字を消してしまうのが一番簡単。
sed –e 's/[^0-9.]/ /g'
UNIX: バイナリファイルのエンコードコマンド
UNIX標準コマンド「uuencode」「uudecode」を使えば、
バイナリファイルをASCII文字列に変換(エンコード)したり、元に戻すことが可能である。
・基本的な使い方
$ uuencode /bin/pydoc pydoc
begin 755 pydoc
M(R$O=7-R+V)I;B]P>71H;VX*"FEM<&]R="!P>61O8PII9B!?7VYA;65?7R ]
>/2 G7U]M86EN7U\G.@H@(" @<'ED;V,N8VQI*"D*
end
$ uuencode pydoc < /bin/pydocbegin 755 pydoc
M(R$O=7-R+V)I;B]P>71H;VX*"FEM<&]R="!P>61O8PII9B!?7VYA;65?7R ]
>/2 G7U]M86EN7U\G.@H@(" @<'ED;V,N8VQI*"D*
end
復元の時のために、実際のデータとは別に「ファイル名」を指定しないといけない。
・「-m」をつけると、Base64変換ができる
$ uuencode -m /bin/pydoc pydoc
begin-base64 755 pydoc
IyEvdXNyL2Jpbi9weXRob24KCmltcG9ydCBweWRvYwppZiBfX25hbWVfXyA9PSAnX19t
YWluX18nOgogICAgcHlkb2MuY2xpKCkK
====
・通常はエンコード結果をファイルに書き出して使う
$ date > date.txt$ uuencode ./date.txt date.txt > ./date.enc$ cat ./date.enc
begin 644 date.txt
L,C Q,>6YM# XYIR(,3'FEZ4@*.:<J"D@,C+FF8(T,.6(AC XYZ>2($I35 H
end
$ rm -i ./date.txt
rm: remove regular file `./date.txt'? y
$ uudecode ./date.enc
$ cat ./date.txt
2011年08月11日 (木) 22時40分08秒 JST
日付を出力した「date.txt」をエンコード。元ファイルを消してデコード。正しく復元できたことがわかる。
・圧縮してからエンコードすると、サイズを節約できる
$ gzip -c ./date.txt | uuencode date.txt > ./date.encmdbによるコアダンプ解析
UNIX環境(特にSolaris)におけるユーザープロセスのダンプ解析についてのメモ。
まず、実際にコアを吐くプログラムを作ってみる。
#include <assert.h>
int main(void) {
assert(!"THIS IS A CRASH TEST.");
return 0;
}
$ /usr/sfw/bin/g++ -O2 -Wall -o ./crash_test ./crash_test.c $ ./crash_test Assertion failed: !"THIS IS A CRASH TEST.", file ./crash_test.c, line 4 異常終了(コアダンプ)
$ ls -l ./core.crash_test* -rw------- 1 root root 2906931 3月 6日 19:36 ./core.crash_test.869
Solaris環境におけるコアダンプの解析ツールは、「pstack」をはじめとするprocツールと呼ばれるコマンド群、「mdb」(Modular Debugger)、「dbx」などがある。
procツールは、稼働中のプロセス(※)だけでなくコアダンプに対しても使用することができる。
(※稼働中のプロセスに対して実行するとプロセスが一旦ロックされるのでタイミングは慎重に)
【名前】
proc, pflags, pcred, pldd, psig, pstack, pfiles, pwdx,
pstop, prun, pwait, ptime - proc ツール
【形式】
/usr/bin/pflags [-r] pid | core [/lwp] ...
/usr/bin/pcred [pid | core]...
/usr/bin/pcred [-u user/uid] [-g group/gid] [-G grouplist] pid...
/usr/bin/pcred -l login pid...
/usr/bin/pldd [-F] [pid | core]...
/usr/bin/psig [-n] pid ...
/usr/bin/pstack [-F] pid | core [/lwp] ...
/usr/bin/pfiles [-Fn] pid...
/usr/bin/pwdx pid...
/usr/bin/pstop pid...
/usr/bin/prun pid...
/usr/bin/pwait [-v] pid...
/usr/bin/ptime command [arg]...
【名前】
pmap - プロセスのアドレス空間に関する情報を表示する
【形式】
/usr/bin/pmap [-rslF] [pid | core]...
/usr/bin/pmap -x [-aslF] [pid | core]...
/usr/bin/pmap -S [-alF] [pid | core]...
# pflags ./core.crash_test.869
core './core.crash_test.869' of 869: ./crash_test
data model = _ILP32 flags = MSACCT|MSFORK
/1: flags = 0
sigmask = 0xffffbefc,0x0000ffff cursig = SIGABRT
# pcred ./core.crash_test.869
core of 869: e/r/suid=101 e/r/sgid=10
# pldd ./core.crash_test.869
core './core.crash_test.869' of 869: ./crash_test
/usr/sfw/lib/libstdc++.so.6.0.3
/lib/libm.so.2
/usr/sfw/lib/libgcc_s.so.1
/lib/libc.so.1
# pstack ./core.crash_test.869
core './core.crash_test.869' of 869: ./crash_test
d25bab25 _lwp_kill (1, 6) + 15
d2561ee3 raise (6) + 1f
d2541d1d abort (8047e64, d27fc818, 65737341, 6f697472, 6166206e, 64656c69) + cd
d2541f37 _assert (80509ab, 805099c, 4, 8060b88, 0, 8050932) + 6b
08050921 main (1, 8047ea8, 8047eb0) + 1d
080507ac _start (1, 8047f48, 0, 8047f55, 8047f64, 8047f73) + 80
# pmap ./core.crash_test.869
core './core.crash_test.869' of 869: ./crash_test
08046000 8K rwx-- [ stack ]
08050000 4K r-x--
08060000 4K rwx--
08061000 8K rwx-- [ heap ]
D2510000 1080K r-x-- /lib/libc.so.1
D262E000 32K rw--- /lib/libc.so.1
D2636000 8K rw--- /lib/libc.so.1
D2640000 28K r-x-- /usr/sfw/lib/libgcc_s.so.1
D2656000 4K rwx-- /usr/sfw/lib/libgcc_s.so.1
D2660000 268K r-x-- /lib/libm.so.2
D26B2000 16K rwx-- /lib/libm.so.2
D26C0000 688K r-x-- /usr/sfw/lib/libstdc++.so.6.0.3
D2770000 24K rwx--
D277B000 132K rwx-- /usr/sfw/lib/libstdc++.so.6.0.3
D279C000 24K rwx-- /usr/sfw/lib/libstdc++.so.6.0.3
D27B9000 4K rwx--
D27C0000 4K rwx--
D27C4000 160K r-x-- /lib/ld.so.1
D27F0000 4K rwx--
D27FC000 8K rwx-- /lib/ld.so.1
D27FE000 4K rwx-- /lib/ld.so.1
total 2512K
pstackの結果(17〜21行目)から、クラッシュの発生箇所が特定できる。
下から読んでいくと、main関数から「_assert」が呼び出され、「abort」「raise」「_lwp_kill」という順で関数がコールされたことがわかる。
lwpとはLight Weight Processの略で、プロセスの内部のプログラムの実行単位である。
詳細は以下を参照。
http://kotobank.jp/word/LWP
「dbx」を使用するためには別途インストールが必要なため、今回はOS標準となっている「mdb」を使ってみよう。
まずは mdb コアファイルのパス で起動。
「>」というプロンプトが返ってきたら、dcmd(Debugger CoMmanD)と呼ばれるコマンドを入力してデバッグ情報を参照する。
# mdb ./core.crash_test.869
Loading modules: [ libc.so.1 ld.so.1 ]
> ::help
Each debugger command in mdb
is structured as follows:
[ address [, count]] verb [ arguments ... ]
^ ^ ^ ^
the start --+ | | +-- arguments are strings which can be
address can be an | | quoted using "" or '' or
expression | | expressions enclosed in $[ ]
| |
the repeat count --+ +--------- the verb is a name which begins
is also an expression with either $, :, or ::. it can also
be a format specifier (/ \ ? or =)
For information on debugger commands (dcmds) and walkers, type:
::help cmdname ... for more detailed information on a command
::dcmds ... for a list of dcmds and their descriptions
::walkers ... for a list of walkers and their descriptions
::dmods -l ... for a list of modules and their dcmds and walkers
::formats ... for a list of format characters for / \ ? and =
For information on command-line options, type:
$ mdb -? ... in your shell for a complete list of options
> ::dcmds
$< - replace input with macro
$<< - source macro
$> - log session to a file
$? - print status and registers
$C - print stack backtrace
$G - enable/disable C++ demangling support
$L - print list of the active lwp ids
$M - list macro aliases
$P - set debugger prompt string
$Q - quit debugger
(以下略)
> ::status debugging core file of crash_test (32-bit) from freya file: /home/mog/crash_test initial argv: ./crash_test threading model: multi-threaded status: process terminated by SIGABRT (Abort) panic message: Assertion failed: !"THIS IS A CRASH TEST.", file ./crash_test.c, line 4
> ::stack libc.so.1`_lwp_kill+0x15(1, 6) libc.so.1`raise+0x1f(6) libc.so.1`abort+0xcd(8047e64, d27fc818, 65737341, 6f697472, 6166206e, 64656c69) libc.so.1`_assert+0x6b(80509ab, 805099c, 4, 8060b88, 0, 8050932) main+0x1d(1, 8047ea8, 8047eb0) _start+0x80(1, 8047f48, 0, 8047f55, 8047f64, 8047f73)
> 80509ab::dump
0 1 2 3 4 5 6 7 8 9 a\/ c d e f 0123456789avcdef
80509a0: 6173685f 74657374 2e630021 22544849 ash_test.c.!"THI
0x080509ab はassertで指定した文字列(assert()の引数)のアドレスであることがわかる。
より広範囲のアドレスをダンプするには、アドレス,バイト数::dump というフォーマットを使う。(バイト数は16進)
例えば、「80509ab,100::dump」とすれば、アドレス 80509ab から 100(16進) = 256(10進) バイトの範囲の
メモリ内容が表示される。
http://mogproject.blogspot.jp/2011/03/solaris-mdb.html
> $?
no process
SIGABRT: Abort
%cs = 0x016b %eax = 0x00000000
%ds = 0x0173 %ebx = 0xd262e000
%ss = 0x0173 %ecx = 0x08047bc4
%es = 0x0173 %edx = 0xd8956ee0
%fs = 0x0000 %esi = 0x08047c48
%gs = 0x01c3 %edi = 0x08047ee0
%eip = 0xd25bab25 libc.so.1`_lwp_kill+0x15
%ebp = 0x08047bd4
%kesp = 0xe51a8fe4
%eflags = 0x00000282
id=0 vip=0 vif=0 ac=0 vm=0 rf=0 nt=0 iopl=0x0
status=
%esp = 0x08047bc4
%trapno = 0xe
%err = 0x14
> $m
BASE LIMIT SIZE NAME
8046000 8048000 2000 [ stack ]
8050000 8051000 1000 /home/mog/crash_test
8060000 8061000 1000 /home/mog/crash_test
8061000 8063000 2000 [ heap ]
d2510000 d261e000 10e000 /lib/libc.so.1
d262e000 d2636000 8000 /lib/libc.so.1
d2636000 d2638000 2000 /lib/libc.so.1
d2640000 d2647000 7000 /usr/sfw/lib/libgcc_s.so.1
d2656000 d2657000 1000 /usr/sfw/lib/libgcc_s.so.1
d2660000 d26a3000 43000 /lib/libm.so.2
d26b2000 d26b6000 4000 /lib/libm.so.2
d26c0000 d276c000 ac000 /usr/sfw/lib/libstdc++.so.6.0.3
d2770000 d2776000 6000
d277b000 d279c000 21000 /usr/sfw/lib/libstdc++.so.6.0.3
d279c000 d27a2000 6000 /usr/sfw/lib/libstdc++.so.6.0.3
d27b9000 d27ba000 1000
d27c0000 d27c1000 1000
d27c4000 d27ec000 28000 /lib/ld.so.1
d27f0000 d27f1000 1000
d27fc000 d27fe000 2000 /lib/ld.so.1
d27fe000 d27ff000 1000 /lib/ld.so.1
> ::nm Value Size Type Bind Other Shndx Name 0x00000000|0x00000000|NOTY |LOCL |0x0 |UNDEF | 0x00000000|0x00000000|FILE |LOCL |0x0 |ABS |./crash_test 0x080500d4|0x00000000|SECT |LOCL |0x0 |1 | 0x080500e8|0x00000000|SECT |LOCL |0x0 |2 | (中略) 0x08060b88|0x00000018|OBJT |LOCL |0x0 |24 |object.2 0x080508a0|0x00000000|FUNC |LOCL |0x0 |11 |frame_dummy 0x00000000|0x00000000|FILE |LOCL |0x0 |ABS |crash_test.c 0x00000000|0x00000000|FILE |LOCL |0x0 |ABS |crtstuff.c 0x08060b28|0x00000000|OBJT |LOCL |0x0 |19 |__CTOR_END__ (中略) 0x080609c4|0x00000000|OBJT |GLOB |0x0 |16 |_GLOBAL_OFFSET_TABLE_ 0x08050980|0x00000018|FUNC |GLOB |0x0 |13 |_fini 0x080506fc|0x00000000|FUNC |GLOB |0x0 |UNDEF |__assert 0x0805068c|0x00000000|FUNC |GLOB |0x0 |UNDEF |atexit 0x00000000|0x00000000|NOTY |WEAK |0x0 |UNDEF |_Jv_RegisterClasses 0x00000000|0x00000000|NOTY |WEAK |0x0 |UNDEF |__fsr_init_value 0x0805070c|0x00000000|FUNC |WEAK |0x0 |UNDEF |_get_exit_frame_monitor 0x08060b1c|0x00000004|OBJT |GLOB |0x0 |18 |__longdouble_used 0x080507c4|0x0000007b|FUNC |GLOB |0x0 |11 |__fsr 0x0805069c|0x00000000|FUNC |GLOB |0x0 |UNDEF |__fpstart 0x08050904|0x00000021|FUNC |GLOB |0x0 |11 |main 0x080506ac|0x00000000|FUNC |GLOB |0x0 |UNDEF |exit 0x080506dc|0x00000000|FUNC |WEAK |0x0 |UNDEF |__register_frame_info_bases 0xd26c0000|0x00000000|NOTY |LOCL |0x0 |UNDEF | 0xd26c0000|0x00000000|FILE |LOCL |0x0 |ABS |.libs/libstdc++.so.6.0.3 0xd26c00b4|0x00000000|SECT |LOCL |0x0 |1 | 0xd26c7314|0x00000000|SECT |LOCL |0x0 |2 | (以下略)
> ::quit #
個人的な経験としては、
といった流れで原因の特定にたどり着くことが多い。
※2012/11/4時点リンク切れ
http://developers.sun.com/solaris/articles/manage_core_dump.html
Core Dump Management on the Solaris OS
http://www.oracle.com/technetwork/server-storage/solaris10/manage-core-dump-138834.html
Solaris 8 オペレーティング環境の概要 - proc ツールによるコアファイルの検査
http://download.oracle.com/docs/cd/E19455-01/806-6320/whatsnewch2-147/index.html
Solaris Core Analysis, Part 1: mdb - Ben Rockwood
http://cuddletech.com/blog/pivot/entry.php?id=965
mdbでサクサクとカーネルをデバック - mdb (modular debugger) ※2012/11/4時点リンク切れ
http://blogs.sun.com/eota/entry/mdb%E3%81%A7%E3%82%B5%E3%82%AF%E3%82%B5%E3%82%AF%E3%81%A8%E3%82%AB%E3%83%BC%E3%83%8D%E3%83%AB%E3%82%92%E3%83%87%E3%83%90%E3%83%83%E3%82%AF_mdb_modular_debugger
.netrc ファイルによるFTP自動ログイン
UNIX ftp プログラム(FTPクライアント)では、ホームディレクトリに「.netrc」というファイルを記述することでログインなどの処理を自動化することができる。
(例)
machine リモートホスト login ログインユーザ password パスワード
上記のように記述すれば、対象のホストへ接続すると自動的にログインが行われる。
ただし、ファイルのパーミッションが悪い(ユーザ以外に参照権限がある)と怒られてしまう。
1: $ ls -l ./.netrc 2: -rw-r--r-- 1 mog staff 42 3月 5日 12:06 ./.netrc 3: $ ftp localhost 4: Connected to localhost. 5: 220 freya.local FTP server ready. 6: Error - .netrc file not correct mode. 7: Remove password or correct mode. 8: 221 Goodbye. 9: No control connection for command: ファイル番号が違います。 10: ftp>chmod すれば想定どおりの動きになる。
1: $ ls -l ./.netrc 2: -rw------- 1 mog staff 42 3月 5日 12:06 ./.netrc 3: $ ftp localhost 4: Connected to localhost. 5: 220 freya.local FTP server ready. 6: 331 Password required for mog. 7: 230 User mog logged in. 8: Remote system type is UNIX. 9: Using binary mode to transfer files.10: ftp>
Solarisのkshで確認。
とあるファイルを初期化するのに、たとえばこんな関数を作ったとする。
1: func() { 2: : > file 3: echo $? 4: }ここで、fileへの書き込みに失敗した場合、非ゼロの値がエコーされることを期待する。
しかし実際には何も表示されない。
$ func
ksh: file: 作成できません。
$
どうやら、組み込みコマンドのリダイレクションに失敗すると、その時点で関数から抜け出てしまうようである。
1: func2() { 2: /bin/true > file 3: echo $? 4: } 5: func3() { 6: (:) > file 7: echo $? 8: }外部コマンドを使うか、サブシェルにすることで期待した結果を得ることができる。
$ func2
ksh: file: 作成できません。
1
$ func3
ksh: file: 作成できません。
1
$
指定容量のダミーファイルを作成
ddコマンドを利用して、指定したサイズぴったりのファイルを作成する。
# dd if=/dev/zero of=./out_file bs=102400 count=1
ブロックサイズとカウントは適宜調整。