Friday, October 2, 2009

Single quote, double quote and comma in shell script

- Single quotes are called the full quoting mechanism. Anything you write within single quote it preserves all special characters within single quote. So within single quote it does not do any variable substitution.
Example of single quote:

$ cat >single_quote.sh
echo 'Process ID variable is defined by $$'
echo 'Exit status is defined by $?'

$ sh single_quote.sh
Process ID variable is defined by $$
Exit status is defined by $?

Here the special characters are preserved as they are written inside single quote.

- Double quotes are called partial quoting mechanism. Anything you write within double quote it interprets most of the special characters within double quote.
Example of double quote:

$ cat >double_quote.sh

echo "Process ID variable is defined by $$"
echo "Exit status is defined by $?"

$ sh double_quote.sh
Process ID variable is defined by 5256
Exit status is defined by 0

Note that $$ and $? is interpreted.

- Comma(,) operator chains together two or more arithmetic operations. All the operations are evaluated but only the last one is returned.
Example:

let "t1 = ((a = 9, 3 + 2, 2 - 1, 15 - 4))"
echo "t1 = $t1" # t1 = 11
# Here t1 is set to the result of the last operation that is 11. variable a=9

let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2".
echo "t2 = $t2 a = $a" # t2 = 5 a = 9

- In case of parameter substitution single comma is used to make first character as lowercase and double comma is used to make all characters are lowercase.

Example:

$ cat >lower_upper.sh
var=mixEdCasecharacteR
echo ${var}
echo ${var,}
# * First char --> lowercase.
echo ${var,,}
# ** All chars --> lowercase.
echo ${var^}
# * First char --> uppercase.
echo ${var^^}
# ** All chars --> uppercase.

Note that above example is application only for on and after of version 4 of Bash.
Related Documents

Dot (.) in linux and shell script

- Dot(.) is equivalent the source command in shell script and linux command line. From command line we can execute a shell script using dot (.). Within a script, a "source file_name" or ". file_name" loads the file file_name. The command "source" or "." just imports code into the script, appending to the script (it is same like include command in php or #include in c). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This technique is useful in situations when multiple scripts use a common data file or function library.

Let's assume that my welcome.sh is like below which later included using . within main.sh file.

$ cat >welcome.sh
echo "Welcome to shell script programing"

$ cat >main.sh
. welcome.sh
echo "This is main file"

$ sh main.sh
Welcome to shell script programing
This is main file


- Dot (.) is the part of the hidden file name. A leading dot is the prefix of a "hidden" file.

$ ls
main.sh newfile semicolon_test.sh welcome.sh

$ touch .hidden_file

$ ls
main.sh newfile semicolon_test.sh welcome.sh

$ ls -a
. .. .hidden_file main.sh newfile semicolon_test.sh welcome.sh


In the example ls -a shows the hidden file .hidden_file

- When working with directory, a single dot (.) represents the current working directory, and two dots (..) represent the parent directory.
Example:

$ pwd
/home/Arju/t_dir

$ cd .

$ pwd
/home/Arju/t_dir

$ cd ..

$ pwd
/home/Arju


- While copying files we can use dot (.) to represent current directory.
The following example will copy the file /home/Arju/f to present working directory.

$ cp /home/Arju/f .


- In case of regular expression while matching characters, a "dot" matches a single character. About dot (.) it is discussed in http://arjudba.blogspot.com/2009/07/list-of-metacharacters-in-regular.html

Related Documents

Thursday, October 1, 2009

Semicolon (;) and double semicolon (;;) in shell script

- Semicolon in shell script often called command separator. In shell script each separate command need to be placed in each separate line. However, if we want to put two or more separate commands into one line then between them we need to place semicolon (;).
Following is a script which will explain the usage of semicolon. If there file exist then script delete the file and if file does not exist then it simply create the file.

$ cat >semicolon_test.sh
echo first command; echo second command
if [ -f newfile ]; then
echo "file exists"; rm newfile
else
echo "File not exists"; touch newfile
fi; echo "Script is done."

$ sh semicolon_test.sh
first command
second command
File not exists
Script is done.

$ ls
newfile semicolon_test.sh

$ sh semicolon_test.sh
first command
second command
file exists
Script is done.

$ ls
semicolon_test.sh


- Note that sometimes while using semicolon we need to escape semicolon. An example is,

find $PROCESSED_DIR -type f -mtime +30 -exec rm {} \;

The '\' ensures that the ';' is interpreted literally, as end of command.
Combination of "\;" at the end tells "find" where the end of the -exec command is. It can't just be the end of the line because the find command syntax allows further tests and actions after the -exec and it can't be just ; because the shell would see it as the end of a shell command and remove it. The \ "escapes" it from being seen by the shell as the end of a shell command.

- Double semicolon (;;) is used as a terminator of case option which is exampled in the topic http://arjudba.blogspot.com/2009/04/case-statement-in-shell-script.html.

- Double semicolon + ampersand (;;&, ;&) is used as terminators in a case option in version 4+ of Bash.

Related Documents

Hash sign (#) in shell script

- With the exception of #! if the line is started by the hash sign then that line in shell script is considered as comment and hence that line is ignored during execution of shell script.

So, a simple example of shell script comment is,

# This line is considered as comment in shell script.


Note that if the first line of the shell script begins with #! then the script tells your system that this file is a set of commands to be fed to the command interpreter indicated and it is not a comment then which is discussed inside topic What is sharp-bang appear at first inside shell script.

- Comment can also appear at the end of the command provided that there is a whitespace before the hash. If there is no whitespace before the hash then comment becomes part of the command.
Following is an example.

$ cat comment_test.sh
echo "This is a comment"#comment

$ sh comment_test.sh
This is a comment#comment

$ cat comment_test.sh
echo "This is a comment" #comment

$ sh comment_test.sh
This is a comment

- Comment also allow whitespace before starting a line.

# This comment is preceded with tab


- Comment can also be embedded within a pipe.

var=( `cat "$filename" | sed -e '/#/d' | tr -d '\n' |\
# Delete lines containing '#' comment character.
sed -e 's/\./\. /g'` )


- In the same line after comment commands are not allowed. As in a line there is no way to terminate comment once it is started so any command after comment will be treated as comment. So for command we need to put it in the new line.

- Note that if hash sign appears within quotes (may be single quote ' or double quote ") or after escape characters ( that is after backslash \ ) then that is not considered as comment.

Also in case of parameter-substitution constructs and in numerical constant expressions. So if we write hash in the way of ${#*}, ${#@}, ${#var} or $(( 2#101 ))
then # is not considered as comment.

echo "The # does not begin a comment."
echo 'The # does not begin a comment.'
echo The \# does not begin a comment.
echo The # begins a comment here.

echo ${PAR#*:} # Parameter substitution, not a comment.
echo $(( 2#1010 )) # Base conversion, not a comment.


- In case of pattern matching operations like ${var/#Pattern/Replacement} (If prefix of var matches Pattern, then substitute Replacement for Pattern.) # is not considered as comment rather it is matched at prefix (beginning) of string.
Related Documents
1. Introduction -What is kernel, shell, shell script.
2. Basic Steps to write a shell script
3. Variables in shell script
4. Output text with echo command
5. Arithmetic operation and expression in shell script with expr
6. Basic shell programming commands quote, exit and read
7. Wildcard characters in linux
8. Assignment and comparison variable in shell script
9. Command writing, command line arguments in shell script
10. Input-Output rediection on linux
11. Pipe with example in linux


Sunday, September 20, 2009

Greetings of Eid Ul-Fitr

To everyone wishing you a very happy EID. May all the joys of life be showered on you.

Eid greetings from Arju 01

Eid greetings from Arju 02

Eid greetings from Arju 03

Eid greetings from Arju 04

Eid greetings from Arju 05

EID Mubarak to Everyone from Arju

To all of my blog readers, my wish wisher as well as to everyone of the world EID MUBARAK. May everyone in this world keep in peace. I attached some EID greeting cards here.


EID Mubarak from Arju 01















EID Mubarak from Arju 02











EID Mubarak from Arju 03















EID Mubarak from Arju 04

Monday, September 7, 2009

HTML special character reference

Sometimes you face problems while entering symbols in you HTML browser. For example you entered © symbol in your browser but it displayed as ? in the browser. The reason is you may not be able to enter certain symbols or characters into your web page using a single key or character.

To enter such a special characters or symbols you need to use 4 characters using the associated "numeric character reference" code or the "character entity reference" code.

However you browser may not support all the character reference.

Numeric character reference : It is the numeric representation of a given character. To use the numeric character reference in your HTML code, use the following format:
&#Numeric character reference;

For example, to display the © symbol using the numeric character reference, enter:

©

Character entity reference : It is the standard name of a given character. To use the character entity reference in your HTML code, use the following format instead:
&Character entity reference;

For example, to display the © symbol using the character entity reference:

©

Following lists contain both Character entity reference as well as Numeric character reference :













































































































































































































































































































































































































































































































































































































































































































Punctuation HTML Entity
(case sensitive)
ISO Latin-1 code name or meaning
     
en space
     
em space
‚ ‚ ‚
single low-9 quotation mark
„ „ „
double low-9 quotation mark
† † †
dagger
‡ ‡ ‡
double dagger
‰ ‰ ‰
per mille sign
• • •
bullet = black small circle
… … …
horizontal ellipsis = three dot leader
′ ′ ′
prime = minutes = feet
– – –
en dash
— — —
em dash
¡ ¡ ¡ inverted exclamation
¿ ¿ ¿ inverted question mark
" " " quotation mark
“ “ “ left double curly quote
” ” ” right double curly quote
‘ ‘ ‘ left single curly quote
’ ’ ’ right single curly quote
«
»
«

»
«

»
guillemets (European-style quotation marks)

(Its
there, but you can't see it!)
    non-breaking space
Symbols
& & & ampersand
¢ ¢ ¢ cent
© © © copyright
× × × multiplication
÷ ÷ ÷ divide
> > > greater than
< &lt; &#60; less than
µ &micro; &#181; micron
· &middot; &#183; middle dot
&para; &#182; pilcrow (paragraph sign)
± &plusmn; &#177; plus/minus
€ &euro; &#8364; Euro
£ &pound; &#163; British Pound Sterling
® &reg; &#174; registered
§ &sect; &#167; section
™ &trade; &#153; trademark
¤ &curren; &#164; currency
¥ &yen; &#165; Japanese Yen
¦ &brvbar; &#166; broken vertical bar
¨ &uml; &#168; spacing diaeresis
ª &ordf; &#170; feminine ordinal indicator
¬ &not; &#172; negation
­ &shy; &#173; soft hyphen
¯ &macr; &#175; spacing macron
° &deg; &#176; degree
² &sup2; &#178; superscript 2
³ &sup3; &#179; superscript 3
¸ &cedil; &#184; spacing cedilla
¹ &sup1; &#185; superscript 1
º &ordm; &#186; masculine ordinal indicator
¼ &frac14; &#188; fraction 1/4
½ &frac12; &#189; fraction 1/2
¾ &frac34; &#190; fraction 3/4
Diacritics
á

Á
&aacute;

&Aacute;
&#225;

&#193;
lower-case "a" with acute accent


upper-case "A" with acute accent
à

À
&agrave;

&Agrave;
&#224;

&#192;
lower-case "a" with grave accent


upper-case "A" with grave accent
â

Â
&acirc;

&Acirc;
&#226;

&#194;
lower-case "a" with circumflex


upper-case "A" with circumflex
å

Å
&aring;

&Aring;
&#229;

&#197;
lower-case "a" with ring


upper-case "A" with ring
ã

Ã
&atilde;

&Atilde;
&#227;

&#195;
lower-case "a" with tilde


upper-case "A" with tilde
ä
Ä
&auml;

&Auml;
&#228;

&#196;
lower-case "a" with diaeresis/umlaut


upper-case "A" with diaeresis/umlaut
æ

Æ
&aelig;

&AElig;
&#230;

&#198;
lower-case "ae" ligature


upper-case "AE" ligature
ç

Ç
&ccedil;

&Ccedil;
&#231;

&#199;
lower-case "c" with cedilla


upper-case "C" with cedilla
é

É
&eacute;

&Eacute;
&#233;

&#201;
lower-case "e" with acute accent


upper-case "E" with acute accent
è

È
&egrave;

&Egrave;
&#232;

&#200;
lower-case "e" with grave accent


upper-case "E" with grave accent
ê

Ê
&ecirc;

&Ecirc;
&#234;

&#202;
lower-case "e" with circumflex


upper-case "E" with circumflex
ë
Ë
&euml;

&Euml;
&#235;

&#203;
lower-case "e" with diaeresis/umlaut


upper-case "E" with diaeresis/umlaut
í

Í
&iacute;

&Iacute;
&#237;

&#205;
lower-case "i" with acute accent


upper-case "I" with acute accent
ì

Ì
&igrave;

&Igrave;
&#236;

&#204;
lower-case "i" with grave accent


upper-case "I" with grave accent
î

Î
&icirc;

&Icirc;
&#238;

&#206;
lower-case "i" with circumflex


upper-case "I" with circumflex
ï
Ï
&iuml;

&Iuml;
&#239;

&#207;
lower-case "i" with diaeresis/umlaut


upper-case "I" with diaeresis/umlaut
ñ

Ñ
&ntilde;

&Ntilde;
&#241;

&#209;
lower-case "n" with tilde


upper-case "N" with tilde
ó

Ó
&oacute;

&Oacute;
&#243;

&#211;
lower-case "o" with acute accent


upper-case "O" with acute accent
ò

Ò
&ograve;

&Ograve;
&#242;

&#210;
lower-case "o" with grave accent


upper-case "O" with grave accent
ô

Ô
&ocirc;

&Ocirc;
&#244;

&#212;
lower-case "o" with circumflex


upper-case "O" with circumflex
ø

Ø
&oslash;

&Oslash;
&#248;

&#216;
lower-case "o" with slash


upper-case "O" with slash
õ

Õ
&otilde;

&Otilde;
&#245;

&#213;
lower-case "o" with tilde


upper-case "O" with tilde
ö
Ö
&ouml;

&Ouml;
&#246;

&#214;
lower-case "o" with diaeresis/umlaut


upper-case "O" with diaeresis/umlaut
ß &szlig; &#223; ess-tsett
ú

Ú
&uacute;

&Uacute;
&#250;

&#218;
lower-case "u" with acute accent


upper-case "U" with acute accent
ù

Ù
&ugrave;

&Ugrave;
&#249;

&#217;
lower-case "u" with grave accent


upper-case "U" with grave accent
û

Û
&ucirc;

&Ucirc;
&#251;

&#219;
lower-case "u" with circumflex


upper-case "U" with circumflex
ü
Ü
&uuml;

&Uuml;
&#252;

&#220;
lower-case "u" with diaeresis/umlaut


upper-case "U" with diaeresis/umlaut
ÿ &yuml; &#255; lower-case "y" with diaeresis/umlaut
´
`
&#180;

&#96;
acute accent with no letter


grave accent/reversed apostrophe with no letter
Letter like Symbols
℘ &weierp; &#8472; script capital P = power set = Weierstrass p
ℑ &image; &#8465; blackletter capital I = imaginary part
ℜ &real; &#8476; blackletter capital R = real part symbol
Mathematical Operators
∅ &empty; &#8709; empty set = null set = diameter
∑ &sum; &#8721; sum sign
− &minus; &#8722; minus sign
∗ &lowast; &#8727; asterisk operator
√ &radic; &#8730; square root
∞ &infin; &#8734; infinity
∩ &cap; &#8745; intersection = cap
∼ &sim; &#8764; tilde operator
≈ &asymp; &#8776; almost equal to = asymptotic to
≡ &equiv; &#8801; identical to
≤ &le; &#8804; less-than or equal to
≥ &ge; &#8805; greater-than or equal to




Relared Documents