>

Test command

1
2
3
4
5
6
7
8
9
10
test condition
test condition && true-command
test condition || false-command

test 5 -gt 2 && echo "Yes"
test 1 -lt 2 && echo "Yes"

result:
Yes
Yes
1
2
test -f /etc/resolv.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."
test -f /etc/resolv1.conf && echo "File /etc/resolv1.conf found." || echo "File /etc/resolv1.conf not found."

String Operators

There are following string operators supported by Bourne Shell.

Assume variable a holds “abc” and variable b holds “efg” then −

Operator | Description | Example
-|-|
= | Checks if two strings are equal. | [ $a = $b ] is true.
!= | Checks if two strings are not. | [ $a != $b ] is true.
-z | Checks if the given string is zero length. | [ -z $a ] is not true.
-n | Checks if the given string is not empty. | [ -n $a ] is not false.
str | Check if str is not the empty string. | [ $a ]

Default value

${parameter-default}, ${parameter:-default}

return parameter’s value if it was set, otherwise return the default value.

e.g. echo ${username-`whoami`}

Echoes the result of `whoami`, if variable $username is still unset.

${parameter=default}, ${parameter:=default}

If parameter not set, set it to default.

e.g.

site=$1
site=${site:-www.google.com}
delay=`curl -o /dev/null -s -w %{time_namelookup} $site'

${parameter+alt_value}, ${parameter:+alt_value}

If parameter set, use alt_value, else use null string.

Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null.

${parameter?err_msg}, ${parameter:?err_msg}

If parameter set, use it, else print err_msg and abort the script with an exit status of 1.

Expression Meaning
${#string} Length of $string
${string:position} Extract substring of $string from $position to the end
${string:position:length} Extract substring of $string from $position with $length
${string#substring} Deletes shortest match of $substring from begining of $string
${string##substring} Deletes longest match of $substring from begining of $string
${string%substring} Deletes shortest match of $substring from end of $string
${string%%substring} Deletes shortest match of $substring from end of $string
${string/substring/replacement} Replace first match of $substring with $replacement.
${string//substring/replacement} Replace all matches of $substring with $replacement.
${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring.
${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring.

String Length

${#string}

Length of Matching Substring at Beginning of String

expr match “$string” ‘$substring’

Index

expr index $string $substring

Substring Extraction

${string:position}

Extracts substring from $string at $position.

position can be negative, but must add a prefix space or enbraced with Parentheses otherwise will match the whole string.

${string:position:length}

Extracts $length characters of substring from $string at $position.

expr substr $string $position $length

expr match “$string” ‘($substring)‘
expr “$string” : ‘($substring)‘

Extracts $substring at beginning of $string, where $substring is a regular expression.

expr match “$string” ‘.($substring)‘
expr “$string” : ‘.($substring)‘

Extracts $substring at end of $string, where $substring is a regular expression.

Substring Removal

${string#substring}

Deletes shortest match of $substring from front of $string.

${string##substring}

Deletes longest match of $substring from front of $string.

${string%substring}

Deletes shortest match of $substring from back of $string.

Useful in stripping the file extention from a filename, e.g. filename=”test.tmp”, ${filename%.*} will get “test” out.

${string%%substring}

Deletes longest match of $substring from back of $string.

Substring Replacement

${string/substring/replacement}

Replace first match of $substring with $replacement.

${string//substring/replacement}

Replace all matches of $substring with $replacement.

${string/#substring/replacement}

If $substring matches front end of $string, substitute $replacement for $substring.

${string/%substring/replacement}

If $substring matches back end of $string, substitute $replacement for $substring.

Manipulating strings using awk

Bash numbers first character of string as 0.
Awk numbers first character of string as 1.

The awk equivalent of ${string:pos:length} is substr(string,pos,length).

$ echo | awk '{ print substr("12good9day",3,4)}
good

PS: Piping an empty “echo” to awk gives it dummy input, and thus makes it unnecessary to supply a filename.

Reference
http://justcoding.iteye.com/blog/1963463
http://www.tldp.org/LDP/abs/html/string-manipulation.html