名稱:expr
使用權限:所有使用者
### 字串長度
shell>> expr length "this is a test"
14
### 數位商數
shell>> expr 14 % 9
5
### 從位置處抓取字串
shell>> expr substr "this is a test" 3 5
is is
### 數位串 only the first character
shell>> expr index "testforthegame" e
2
### 字串真實重現
shell>> expr quote thisisatestformela
thisisatestformela
名稱: tr
### 1.比方說要把目錄下所有的大寫檔名換為小寫檔名?
似乎有很多方式,"tr"是其中一種:
#!/bin/sh
dir="/tmp/testdir";
files=`find $dir -type f`;
for i in $files
do
dir_name=`dirname $i`;
ori_filename=`basename $i`
new_filename=`echo $ori_filename | tr [:upper:] [:lower:]` > /dev/null;
#echo $new_filename;
mv $dir_name/$ori_filename $dir_name/$new_filename
done
### 2.自己試驗中...lowercase to uppercase
tr abcdef...[del] ABCDE...[del]
tr a-z A-Z
tr [:lower:] [:upper:]
shell>> echo "this is a test" | tr a-z A-Z > www
shell>> cat www
THIS IS A TEST
### 3.去掉不想要的字串
shell>> tr -d this ### 去掉有關 t.e.s.t
this
man
man
test
e
### 4.取代字串
shell>> tr -s "this" "TEST"
this
TEST
th
TE
參數:
c 輸出結果至標準輸出設備(一般指螢幕)
f 強迫寫入檔案,若目的檔已經存在,則會被覆蓋 (force)
v 將程式執行的訊息印在螢幕上 (verbose)
b 設定共同字串數的上限,以位元計算,可以設定的值為 9 至 16 bits 。由於值越大,能使用的共同字串就 越多,壓縮比例就越大,所以一般使用預設值 16 bits (bits)
d 將壓縮檔解壓縮
V 列出版本訊息