$ cat file1 > file2
$ dirs 2>&1 > /dev/null
執行dirs時所產生的standard error丟到standard out再到黑洞/dev/null去,
就是甚麼也看不到。
exec 最常用在與file descriptor的關聯這裡
$ exec 3> $LOGFILE
$ exec 3>> $LOGFILE
$ exec 2>&-
$ exec < infile
其中各意義分別為
開一個3號file descriptor相對於$LOGFILE。把standard err關掉。
把目前shell的standard input變成infile
$ cat something >&3
$ read <3
現在3這個file descriptor就相對於LOGFILE了,所以cat something的內容會到
LOGFILE去。
注意file descriptor 1,2,3與導向符號的空格。
> 重新導向並且先把檔案虧空(我真是敗家子:-))
>> 把結果導向並且append到檔案後
< 重新導向standard input
>&n 把standard out丟給file descriptor n
<&n 從file descriptor n拿東西給standard in
>&- close standard out
<<text 把stanadard in導向,直到讀到text為止。
要注意的是有些後面只能接檔案,有的接file descriptor,還有
接file descriptor時不能有空格。
自動ftp的scripts,在很早很早以前,中山大學的ftp站可是有名的X情網站,
不過他只會在半夜
才開放目錄的存取,所以我們就叫電腦在半夜時cron job自動幫我們ftp。哈哈
#!/bin/ksh
# A script to automate FTP transfer
HOST=ftp
USER=user
PASSWORD=password
FILENAME_PATTERN=remote_files
REMOTE_DIR=/usr/doc
LOCAL_DIR=/usr/local
# -i = non-interactive, -n = disable auto-login
ftp -i -n <<HERE
open $HOST
user $USER $PASSWORD
cd $REMOTE_DIR
lcd $LOCAL_DIR
mget $FILENAME_PATTERN
close
bye
HERE
注意<<的用法,ftp的input會一直讀到HERE為止,open, user, cd, mget
都是ftp中的命令。不過也可以把
這些ftp命令寫成一個檔案然後ftp < 檔案也行。