1. DIRSTACK(目錄棧)
它顯示目錄棧的棧頂值
,和我們常見的棧一樣爲先進先出
,擁有入棧
和出棧
兩個動作。和DIRSTACK
相關的命令如下:
- dirs (顯示目錄棧)
注意與dir不同
格式:
dirs [-clpv] [+N] [-N]
選項:
-c
刪除所有元素以清空目錄棧
-l
不打印與主目錄相關的波浪號前綴的目錄
-p
每行一個條目打印目錄棧
-v
每行一個條目,以棧中位置爲前綴打印目錄棧
參數:
+N
顯示 dirs 不帶選項啓動時顯示的目錄列表左起中第N 個目錄,從零開始。
-N
顯示 dirs 不帶選項啓動時顯示的目錄列表右起中第N 個目錄,從零開始。
Demo:
$ popd
bash: popd: 目錄棧爲空
#表明現在目錄棧爲空
$ 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 | 目錄]
選項:
-n
抑制添加目錄至棧時通常的改變目錄操作,從而僅對棧進行操作。
參數:
+N
旋轉棧從而第 N 個目錄 (`dirs' 顯示的列表中左起,從零開始)將移動到棧頂。
-N
旋轉棧從而第 N 個目錄 (`dirs' 顯示的列表中右起,從零開始)將移動到棧頂。
dir
將 DIR 目錄添加到棧頂,並且使其成爲當前工作目錄。
通俗來講:+N、-N就是將第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
爲我不小心打錯,但由此可見,絕對目錄
即便是不存在的也可以成功壓棧。
- popd (將目錄彈出棧)
選項:
-n
抑制從棧中刪除目錄時通常的目錄變換操作,從而僅對棧
進行操作。
參數:
+N
刪除第 N 個目錄 (`dirs' 顯示的目錄列表中左起,從零開始)。
例如:`popd +0' 刪除第一個目錄,`popd +1' 刪除第二個。
-N
刪除第 N 個目錄 (`dirs' 顯示的目錄列表中右起,從零開始)。
例如:`popd -0' 刪除最後一個目錄,,`popd -1' 刪除倒數第二個。
2. GLOBIGNORE (通配時忽略)
GLOBIGNORE
是由冒號
分隔的模式列表,表示通配時忽略的文件名集合
。
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 (記錄腳本執行時間)
SECONDS
記錄腳本
從開始到結束所耗費的時間
,以秒
爲單位。也可以記錄終端
打開的時間。
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 (設置Shell過期時間)
TMOUT
變量用於設置Shell的過期時間
,當Shell不爲0
時,Shell在TMOUT秒後將自動註銷
。
我理解的TMOUT是當腳本運行受到
阻塞(等待輸入)
時,將會等待TMOUT秒,超出便停止阻塞
執行下一個命令,如果下n個命令依舊有阻塞便繼續等待
TMOUT秒,超出便停止阻塞
繼續執行下一個命令。
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.