歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

Bash內部變量學習,Bash變量學習

Bash內部變量學習,Bash變量學習


Bash內部變量學習,Bash變量學習


$SECONDS變量---------腳本已經運行的秒數,腳本實例:

 1 #!/bin/bash -
 2 #===========================================================================================
 3 #
 4 #
 5 #                                    FILE:seconds.sh
 6 #                                    USAGE:./seconds.sh                              
 7 #    DESCRIPTION:
 8 #    
 9 #         OPTIONS:---
10 #    REQUIREMENTS:---
11 #            BUGS:---
12 #           NOTES:---
13 #          AUTHOR:Yuan Shaoqian(Mark),[email protected]
14 #    ORGANIZATION:
15 #         CREATED:09/02/2016 21:19
16 #        REVISION:---
17 #===========================================================================================
18 TIME_LIMIT=10
19 INTERVAL=1
20 
21 echo
22 echo "Hit Control-C to exit before $TIME_LIMIT seconds."
23 echo 
24 
25 while [ "$SECONDS" -le "$TIME_LIMIT" ]
26 do # $SECONDS is an internal shell variable
27 if [ "$SECONDS" -eq 1 ]
28 then
29   units=second
30 else
31   units=seconds
32 fi
33 
34 echo "This script has been running $SECONDS $units."
35 #On a slow or overburdened machine,the script may skip a count every once in a while.
36 sleep $INTERVAL
37 done 
38 exit 0

$HOME變量-------------用於引用Bash實例的全路徑名。

$IFS變量--------IFS時內部字段分隔符的縮寫。此變量決定當Bash解析字符串時將怎樣識別字段,或單詞分界線。變量$IFS的默認值是空格(空格/制表符/換行),但可以被修改。

1 $ set x y z
2 $ IFS=“:;-”
3 $ echo "$*"
4 x:y:z

$OSTYPE變量-----操作系統的類型

$TMOUT變量------如果$TMOUT被指定了一個非零的值,此值會被Bash的內部命令read作為默認的超時秒數。

 1 set -o nounset
 2 TMOUT=3
 3 
 4 echo "Are you sure?(Y/N)"
 5 read input
 6 
 7 if [ "$input" == "Y" ]
 8 then
 9   echo "Continue..."
10 else
11   echo "Exit!"
12 fi 

$UID變量------當前用戶的賬號識別碼(ID號),與/etc/passwd中記錄的相同,$UID是一個只讀變量,不支持修改。

1 root_id=0
2 if [ "$UID" -eq "$root_id" ]
3 then
4   echo "You are root."
5 else
6   echo "You are just an ordinary user."
7 fi
8 
9 exit 0

 

http://xxxxxx/Linuxjc/1155428.html TechArticle

Copyright © Linux教程網 All Rights Reserved