Recently in Shell Category

BASH for loop

user-pic
Vote 0 Votes

# Output 1 2 3...99
for i in {1..99}
do
   echo $i
done

for ((i=1;i<=99;i++))
do
   echo $i
done

for i in `seq 1 99`
do
   echo $i
done

# Output 1 3 5...99
for i in `seq 1 2 99`
do
   echo $i
done

# Output a to z
for i in {a..z}
do
   echo $i
done

Pure-FTP uploadscript

user-pic
Vote 0 Votes

Pure-FTPd 在 configure 時加 --with-uploadscript 參數可以 enable uploadscript 的功能,
就是上傳檔案後可以呼叫外部程式或 script.

pure-ftpd 啟動參數加 -o, 有設定檔(pure-ftpd.conf)的系統就設 CallUploadScript yes

必需先啟動 pure-ftpd 後再啟動 pure-uploadscript, 不然無法運作.
pure-uploadscript -Br /usr/local/bin/uploadscript.sh

上傳檔案的絕對路徑會帶到第一個參數 $1
其他可用的環境變數如下:
$UPLOAD_SIZE : the size of the file, in bytes.
$UPLOAD_PERMS : the permissions, as an octal value.
$UPLOAD_UID : the uid of the owner.
$UPLOAD_GID : the group the file belongs to.
$UPLOAD_USER : the name of the owner.
$UPLOAD_GROUP : the group name the file belongs to.
$UPLOAD_VUSER : the full user name, or the virtual user name. (127 chars max)

uploadscript 範例, 上傳的檔會移到 /tmp/日期 目錄下, 並記錄一筆 Log 到 /var/log/uploadscript.log
#!/bin/sh
DIR=/tmp/`date +%Y%m%d`
[ -d $DIR ] || mkdir $DIR
mv "$1" $DIR
echo `date` size $UPLOAD_SIZE by $UPLOAD_USER >> /var/log/uploadscript.log

Pure-FTPd 1.0.29 released on 2010-03-15, 最近改版還滿勤的.

另一套 ProFTPD 在 1.3.3 版(2010-02-24 Released)也加入了 mod_exec,
比起 Pure-FTPd uploadscript 更為強大, 各種 FTP 的 event 都能呼叫外部程式或 script.

ssh-copy-id

user-pic
Vote 0 Votes

要用 SSH public key authentication 通常會這樣做
ssh-keygen
scp ~/.ssh/id_rsa.pub remote-host
ssh remote-machine
cat id_rsa.pub >> ~/.ssh/authorized_keys

2-4 步驟可以用一行指令取代
ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host

ref. 10 個最酷的 Linux 單行命令
Top Ten One-Liners from CommandLineFu Explained
CommandLineFu All commands sorted by votes

SADDR=台中市中區中山路1號
DADDR=台中市西屯區文華路100號
w3m -dump "http://maps.google.com.tw/maps?saddr=$SADDR&daddr=$DADDR" | egrep "公里.*大約"

輸出結果:
8.3 公里 - 大約 24 分鐘

[Linux] Turn All Services Off

user-pic
Vote 0 Votes

chkconfig --list | awk '/0:/{system("chkconfig "$1" off")}'

Linux 安裝好預設會開一些 Service, 若用不到就先全關, 再開自己要的.
chkconfig crond on
chkconfig network on
chkconfig syslog on
chkconfig sshd on

Using shell variable in awk

user-pic
Vote 0 Votes

Shell variable must within single-quotes in awk command, just like '$A'

e.g. File fruit.txt content as follow
apple 99
orange 25
lemon 30

FRUIT=apple, To get number after $FRUIT
awk '/'$FRUIT'/ {print $2}' fruit.txt (Rignt)
awk '/$FRUIT/ {print $2}' fruit.txt (Wrong, not work)

About this Archive

This page is an archive of recent entries in the Shell category.

Security is the previous category.

Software is the next category.

Find recent content on the main index or look in the archives to find all content.