1. DIRSTACK(目錄棧)
它顯示目錄棧的棧頂值,和我們常見的棧一樣為先進(jìn)先出,擁有入棧和出棧兩個(gè)動(dòng)作。和DIRSTACK相關(guān)的命令如下:
- dirs (顯示目錄棧)
注意與dir不同
格式:
dirs [-clpv] [+N] [-N]
選項(xiàng):
-c刪除所有元素以清空目錄棧
-l不打印與主目錄相關(guān)的波浪號(hào)前綴的目錄
-p每行一個(gè)條目打印目錄棧
-v每行一個(gè)條目,以棧中位置為前綴打印目錄棧
參數(shù):
+N顯示 dirs 不帶選項(xiàng)啟動(dòng)時(shí)顯示的目錄列表左起中第N 個(gè)目錄,從零開始。
-N顯示 dirs 不帶選項(xiàng)啟動(dòng)時(shí)顯示的目錄列表右起中第N 個(gè)目錄,從零開始。
Demo:
$ popd
bash: popd: 目錄棧為空
#表明現(xiàn)在目錄棧為空
$ echo $DIRSTACK
/home/jin1ming
$ pushd .
~ ~
$ pushd go
~/go ~ ~
$ pushd ../linux_shell
~/linux_shell ~/go ~ ~
$ dirs -l -p
/home/jin1ming/linux_shell
/home/jin1ming/go
/home/jin1ming
/home/jin1ming
$ dirs -v
0 ~/linux_shell
1 ~/go
2 ~
3 ~
$ dirs +1
~/go
$ dirs +2
~
$ dirs -l -p
/home/jin1ming/linux_shell
/home/jin1ming/go
/home/jin1ming
/home/jin1ming
$ dirs +0
~/linux_shell
$ dirs -1
~
$ dirs -0
~
$ dirs -2
~/go
- pushd (將目錄壓棧)
格式:
pushd [-n] [+N | -N | 目錄]
選項(xiàng):
-n抑制添加目錄至棧時(shí)通常的改變目錄操作,從而僅對(duì)棧進(jìn)行操作。
參數(shù):
+N旋轉(zhuǎn)棧從而第 N 個(gè)目錄 (`dirs' 顯示的列表中左起,從零開始)將移動(dòng)到棧頂。
-N旋轉(zhuǎn)棧從而第 N 個(gè)目錄 (`dirs' 顯示的列表中右起,從零開始)將移動(dòng)到棧頂。
dir將 DIR 目錄添加到棧頂,并且使其成為當(dāng)前工作目錄。
通俗來講:+N、-N就是將第N個(gè)目錄移到棧頂,相當(dāng)于一個(gè)循環(huán)鏈表的指針往后(+N)移了2次。
Demo:
$ pushd -n go
~ go
$ pushd -n linux_shell
~ linux_shell go
$ pushd -n /hoem
~ /hoem linux_shell go
$ pushd -n /home
~ /home /hoem linux_shell go
$ dirs -v -l
0 /home/jin1ming
1 /home
2 /hoem
3 linux_shell
4 go
/hoem為我不小心打錯(cuò),但由此可見,絕對(duì)目錄即便是不存在的也可以成功壓棧。
- popd (將目錄彈出棧)
選項(xiàng):
-n抑制從棧中刪除目錄時(shí)通常的目錄變換操作,從而僅對(duì)棧
進(jìn)行操作。
參數(shù):
+N刪除第 N 個(gè)目錄 (`dirs' 顯示的目錄列表中左起,從零開始)。
例如:`popd +0' 刪除第一個(gè)目錄,`popd +1' 刪除第二個(gè)。
-N刪除第 N 個(gè)目錄 (`dirs' 顯示的目錄列表中右起,從零開始)。
例如:`popd -0' 刪除最后一個(gè)目錄,,`popd -1' 刪除倒數(shù)第二個(gè)。
2. GLOBIGNORE (通配時(shí)忽略)
GLOBIGNORE是由冒號(hào)分隔的模式列表,表示通配時(shí)忽略的文件名集合。
Demo:
$ cd linux_shell/
$ ls
1.txt 4aa 4ae 4ai awkdomo2.awk new1.txt selectdomo.sh testgo.sh
2 4ab 4af 4aj awkdomo.awk phoneinfo shelldemo.sh test.sh
2.txt 4ac 4ag 4ak file score sort1.txt
3 4ad 4ah 4al fordemo.sh selectdemo.sh sort2.txt
$ GLOBIGNORE="*.sh:*.awk"
$ ls *
1.txt 2.txt 4aa 4ac 4ae 4ag 4ai 4ak file phoneinfo sort1.txt
2 3 4ab 4ad 4af 4ah 4aj 4al new1.txt score sort2.txt
$ ls
1.txt 4aa 4ae 4ai awkdomo2.awk new1.txt selectdomo.sh testgo.sh
2 4ab 4af 4aj awkdomo.awk phoneinfo shelldemo.sh test.sh
2.txt 4ac 4ag 4ak file score sort1.txt
3 4ad 4ah 4al fordemo.sh selectdemo.sh sort2.txt
3.SECONDS (記錄腳本執(zhí)行時(shí)間)
SECONDS記錄腳本從開始到結(jié)束所耗費(fèi)的時(shí)間,以秒為單位。也可以記錄終端打開的時(shí)間。
Demo:
$ echo $SECONDS
21
$ echo $SECONDS
25
$ echo $SECONDS
27
$ echo $SECONDS
37
$ SECONDS=0;echo $SECONDS
0
$ cat secondsdemo.sh
#!/bin/bash
sum=0
for num in {1..10}
do
let "sum+=num"
sleep 1
done
echo "sum=$sum"
echo "Running time is $SECONDS."
$ ./secondsdemo.sh
sum=55
Running time is 10.
TMOUT (設(shè)置Shell過期時(shí)間)
TMOUT變量用于設(shè)置Shell的過期時(shí)間,當(dāng)Shell不為0時(shí),Shell在TMOUT秒后將自動(dòng)注銷。
我理解的TMOUT是當(dāng)腳本運(yùn)行受到
阻塞(等待輸入)時(shí),將會(huì)等待TMOUT秒,超出便停止阻塞執(zhí)行下一個(gè)命令,如果下n個(gè)命令依舊有阻塞便繼續(xù)等待TMOUT秒,超出便停止阻塞繼續(xù)執(zhí)行下一個(gè)命令。
Demo:
$ cat tmoutdemo.sh
#!/bin/bash
TMOUT=3
for (( i=0; i<3; i++ ))
do
echo "What is your name?"
read name
if [ $name ]
then
echo "Your name is $name."
break
else
echo "No name."
fi
done
$ ./tmoutdemo.sh
What is your name?
No name.
What is your name?
haha
Your name is haha.