你的位置:首页 > 操作系统

[操作系统]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),yuanshaoqiancom@gmail.com14 #  ORGANIZATION:15 #     CREATED:09/02/2016 21:1916 #    REVISION:---17 #===========================================================================================18 TIME_LIMIT=1019 INTERVAL=120 21 echo22 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 variable27 if [ "$SECONDS" -eq 1 ]28 then29  units=second30 else31  units=seconds32 fi33 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 $INTERVAL37 done 38 exit 0

$HOME变量-------------用于引用Bash实例的全路径名。

$IFS变量--------IFS时内部字段分隔符的缩写。此变量决定当Bash解析字符串时将怎样识别字段,或单词分界线。变量$IFS的默认值是空格(空格/制表符/换行),但可以被修改。

1 $ set x y z2 $ 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 else11  echo "Exit!"12 fi 

$UID变量------当前用户的账号识别码(ID号),与/etc/passwd中记录的相同,$UID是一个只读变量,不支持修改。

1 root_id=02 if [ "$UID" -eq "$root_id" ]3 then4  echo "You are root."5 else6  echo "You are just an ordinary user."7 fi8 9 exit 0