Add variables to a program's environment without
permanently
affecting the environment of the shell or subsequent commands.
PATH=/bin:/usr/bin awk '...' file1 file2
This changes the value of PATH only for the execution of the single
awk command.
Any subsequent commands, however, see the current value of PATH
in their environment.
Used only by interactive shells upon invocation; the value
of $ENV is
parameter-expanded. The result should be a full pathname for a
file to be
read and executed at startup. This is an XSI requirement.
$HOME
Home (login) directory.
使用者自己的目錄
$IFS
Internal field separator; i.e., the list of characters that
act as word
separators. Normally set to space, tab, and newline.
$LANG
Default name of current locale; overridden by the other
LC_* variables.
$LC_ALL
Name of current locale; overrides LANG and the other LC_*
variables.
$LC_COLLATE
Name of current locale for character collation
(sorting) purposes.
$LC_CTYPE
Name of current locale for character class
determination during
pattern matching.
$LC_MESSAGES
Name of current language for output messages.
$LINENO
Line number in script or function of the line that just
ran.
$NLSPATH
The location of message catalogs for messages in
the language given by $LC_MESSAGES (XSI).
$PATH
Search path for commands. 執行命令時所搜尋的目錄
$PPID
Process ID of parent process.
$PS1
Primary command prompt string. Default is "$ ".
在命令列時的提示號
$PS2
Prompt string for line continuations. Default is "> ".
當命令尚未打完時,Shell 要求再輸入時的提示號
$PS4
Prompt string for execution tracing with set -x. Default is
"+ ".
$PWD
Current working directory.
其他幾個有用的環境變數
$TZ
時區
$MAILCHECK
每隔多少秒檢查是否有新的信件
$MANPATH
man 指令的搜尋路徑
Thu Sep 7 16:18:51 CST 2023
Untitled Document
Env
The env command
may be used to remove variables from a program's environment, or
to temporarily change environment variable values:
The -i option initializes the environment; i.e., throws away any
inherited
values, passing in to the program only those variables named on
the command line.
Thu Sep 7 16:18:51 CST 2023
Untitled Document
Unset
Removes variables and functions from the running shell.
unset full_name #Remove the full_name variable
unset -v first middle last #Remove the other variables
Use unset -f to remove functions:
who_is_on ( ) { #Define a function
who | awk '{ print $1 }' | sort -u #Generate sorted list of users
}
unset -f who_is_on #Remove the function
Early versions of the shell didn't have functions or the unset command.
POSIX added the -f option for removing functions, and then added the
-v option for symmetry with -f.
#本程式讀取第一個txt檔,將之後的所有csv 檔內的逗號改成空白,全部輸出到 STDOUT
#=========================================================================
cat $1
shift #捨棄第一個位置參數
for i in $*
do
sed 's/,/ /g' $i #利用sed將逗號改成空白
done
#-----------------------------------
Some version of Shell
#-----------------------------------
$ echo -n "Enter your name: "
#-------------------------------------
結果: Enter your name: _ Enter data
within the arguments.
#-----------------------------------
System V version
#-----------------------------------
$ echo "Enter your name: \c" Print prompt
#-------------------------------------
結果: Enter your name: _ Enter data
echo 認得的特殊符號
符號
說明
\a
Alert character, usually the ASCII BEL character.
\b
Backspace.
\c
Suppress the final newline in the output. Furthermore, any characters
left in the argument, and any following arguments, are ignored (not printed).
\f
FormFeed.
\n
NewLine.
\r
CarriageReturn.
\t
Horizontal tab.
\v
Vertical tab.
\\
A literal backslash character.
\0ddd
Character represented as a 1- to 3-digit octal value.
#-----------------------------------------------
$ printf "This script prints '%s, %s!'\n" Hello world
#-----------------------------------------------
#結果: This script prints 'Hello, world!'
What is the value of $i the first time through the loop?
What is the value of $i when the loop complete?
將迴圈的輸出轉向到檔案
功能
利用迴圈合併數個檔案,各檔案之間以分隔線及檔名隔開,存到 outfile
用法
concatgefile file-list
Script XX
#-------------------------------------
# Script: concatefile v1
#-------------------------------------
for i in $*
do
echo "====== FILE NAME: $i ========"
cat -n $i # -n 這個 option 會將讀入的資料加上 line number
done > outfile
這樣的一個小script,就可以遠遠超過視窗系統的效率,節省很多時間。
假設一個使用者利用C 語言在開發一個複雜的程式,而原始檔分散在數十甚至
上百個小檔案中,在開發過程中經常要反覆的檢查各原始碼,單純依靠終端機
是很痛苦的事,此時就可以利用 concatefile 這個script
將所有原始檔加上 line number 集中到一個檔案中,再印出來,方便
檢視。如果環境許可,甚至可貼在牆上那就更方便了。
Thu Sep 7 16:18:54 CST 2023
Untitled Document
流程控制 - for
如果 for 迴圈沒有 arg-list, 就會把所有 position variable 當成是
arg-list。
number=0
while [ $number -lt 10 ]
do
echo "$number\c"
number=`expr $number + 1` #將變數 number 加一
done
echo
0123456789
Thu Sep 7 16:18:55 CST 2023
Untitled Document
流程控制 - break and continue
這兩者是用於for, while, until 等迴圈控制下。break 會跳至done後方執行
,而continue會跳至done執行,繼續執行迴圈。
例
while
echo "Please enter data ('done' to exit):"
read response
do
if [ "$response" = "done" ]
then
break
fi
if [ "$response" = "" ]
then
continue
fi
_......
_......
process data
_......
_......
done
Thu Sep 7 16:18:55 CST 2023
Untitled Document
流程控制 - Case
Case 的格式如下:
case str in
pat1) command(s);;
pat2) command(s);;
pat3) command(s);;
esac
case string in
pat1) command(s) ;; #若 $string = pat1, 則執行 command(s)
pat2|pat3) command(s) ;; #若 $string = pat1 或 pat2, 則執行 command(s)
*) command(s) ;; #default case
esac
其中供比對的 pattern 可以是以下三種形式中的任一種字串:
使用者給的固定字串;
執行指令得到的結果 (Command Substitution)
含有萬用字元( wildcards 或 meta characters)的字串如下:
*
任意字串,包括 null
?
任意字元
[abc]
a, b, 或c三字元其中之一
[a-n]
從a到n的任一字元
[!abc]
a, b, 或c以外的字元
|
多重選擇
以下是兩個例子:
case $# in
0) echo Usage: xxxxxx ;;
1|2) process data ;;
*) echo Usage: xxxxx ;;
esac
echo 'Enter A, B, or C: \c'
read letter
case $letter in
A|a) echo 'You entered A.';;
B|b) echo 'You entered B.';;
C|c) echo 'You entered C.';;
*) echo 'Not A, B, or C';;
esac
Thu Sep 7 16:18:55 CST 2023
Untitled Document
Function ( 函數 )
函數 (Function)
Thu Sep 7 16:18:55 CST 2023
Untitled Document
Function ( 函數/函式 )
幾乎所有的高階程式語言都必須提供副程式的功能,讓程式設計者可以將重複性的
任務寫成副程式,俾便重複使用,既能節省空間,程式也比較清爽,有助於對邏輯的
分析與理解。副程式在不同的程式語言中,有不同的名稱,在 C 與 Shell 中,叫做
函數或函式 (Function)、在物件導向(Objet-Oriented)程式中,稱為方法 (Method)。
#-------------------------------------
# Script: wait_for --- wait for a user to log in
# 用法: wait_for user [ sleeptime ]
#-------------------------------------
wait_for ( ) {
until who | grep "$1" > /dev/null
do
sleep ${2:-100}
done
}
wait_for clinton #Wait for clinton, check every 100 sec
wait_for obama 60 #Wait for obama, check every 60 sec
equal ( ) {
test "$1" -eq "$2" && return 0 || return 1
}
equal "$a" "$b" && echo "$a and $b are equal"
equal "$c" "$d" || echo "$c and $d are not equal"
#-------------------------------------
測試看一個檔案是否含有某些字串
#-------------------------------------
if grep pattern myfile > /dev/null
then
commands for true (Pattern is there)
else
commands for false (Pattern is not there)
fi