Passing an indirect reference to a function.

 #!/bin/bash
 # ind-func.sh: Passing an indirect reference to a function.

 echo_var ()
 {
 echo "$1"
 }
message=Hello
Hello=Goodbye

echo_var "$message"        # Hello
# Now, let's pass an indirect reference to the function.
echo_var "${!message}"     # Goodbye

echo "-------------"

# What happens if we change the contents of "hello" variable?
Hello="Hello, again!"
echo_var "$message"        # Hello
echo_var "${!message}"     # Hello, again!
 exit 0

This function can able to use for dictionary (key-value)creating.

for instance:
Continue reading “Passing an indirect reference to a function.”

Good Advice & Practice of Shell Programming

C H A P T E R 2

Standard Function Library
After writing many scripts, I realized there were several functions and routines I was
using repeatedly. Sometimes I would write the code for these duplicated functions again,
and other times I would copy and paste the code from an earlier script. But the best coding
practice is to keep a file around that contains all of your already tested and working
functions. This creates a scriptwriter’s toolbox, or, in programming speak, a library. With
a library in place, many of the tools you need often are close at hand and you don’t have
to code the functions anew each time you need them. This chapter shows you how to set
up a standard function library and how to utilize the library in your scripts.

From : Expert shell scripting. Thank to the great author.

sed入门(转载)

sed入门(转载) 原出处:http://phi.sinica.edu.tw/aspac/reports/96/96005/index.html
1.Introduction
Sed(Stream EDitor)為 UNIX 系統上提供將編輯工作自動化的編輯器 , 使用者無需直接編輯資料。使用者可利用 sed 所提供 20 多種不同的函數參數 , 組合(註解 [1])它們完成不同的編輯動作。此外 , 由於 sed 都以行為單位編輯文件 , 故其亦是行編輯器(line editor)。
Continue reading “sed入门(转载)”

Shell expression operators. Need to review frequently.

String Comparison Result
string1 = string2 True if the strings are equal
string1 != string2 True if the strings are not equal
-n string True if the string is not null
-z string True if the string is null (an empty string)
Arithmetic Comparison Result
expression1 -eq expression2 True if the expressions are equal
expression1 -ne expression2 True if the expressions are not equal
expression1 -gt expression2 True if expression1 is greater than expression2
expression1 -ge expression2 True if expression1 is greater than or equal to
expression2
expression1 -lt expression2 True if expression1 is less than expression2
expression1 -le expression2 True if expression1 is less than or equal to
expression2
! expression True if the expression is false, and vice versa
File Conditional Result
-d file True if the file is a directory
-e file True if the file exists. Note that historically the -e option
has not been portable, so -f is usually used.
-f file True if the file is a regular file
-g file True if set-group-id is set on file
-r file True if the file is readable
-s file True if the file has nonzero size
-u file True if set-user-id is set on file
-w file True if the file is writable
-x file True if the file is executable