Posts

Showing posts from April, 2011

Single and Double quotes.

Guidelines for quoting command arguments \ prevent the shell from expanding any single special character. ' ' prevent the shell from expanding all special characters enclosed in this string except  ! . " " prevent the shell from expanding any enclosed special character except  $ ,  `  and  ! . Single quotes limit substitution . We can place variables in double quoted text and the shell still performs substitution. [me@linuxbox me]$ echo "My host name is $HOSTNAME." My host name is linuxbox. [me@linuxbox me]$ echo 'My host name is $HOSTNAME.' My host name is $HOSTNAME. Double quotes do not suppress the substitution of words that begin with "$" but they do suppress the expansion of wildcard characters. For example, try the following: [me@linuxbox me]$ echo * anaconda-ks.cfg clear.sh config.txt date_marker ftpftp1 list1 list2 list3 list4 list5 list6 post-install post-install.log script status textfile ...

The backslash ( \ ) escape character

ashish#: echo "\as shdd" \as shdd ashish#: echo "Well, isn't that \"special\"?" Well, isn't that "special"? ashish#: echo "Well, isn't that \special\?" Well, isn't that \special\? ashish#: echo "Well, isn't that \$special\$?" Well, isn't that $special$? ashish#: echo "Well, isn't that \$ special \$?" Well, isn't that $ special $? ashish#: echo "Well, isn't that \#special\#?" Well, isn't that \#special\#? ashish#: echo "Well, isn't that \'special\'?" Well, isn't that \'special\'? ashish#: echo "Well, isn't that \' special \'?" Well, isn't that \' special \'? ashish#: echo "Well, isn't that \\' special \'?" Well, isn't that \' special \'? ashish#: echo "Well, isn't that \\\' special \'?"...

SED

Sed sed is a program used for editing data. It stands for stream editor . Unlike ed, sed cannot be used interactively. However, its commands are similar. The general form of the sed command is sed command file     [root@jaguar ~/Desktop] variable=ashi [root@jaguar ~/Desktop] echo $variable ashi [root@jaguar ~/Desktop] echo $variable | sed 's/$variable/ashish/' ashi [root@jaguar ~/Desktop] echo $variable | sed "s/$variable/ashish/" ashish   [root@server1 ~]# sed 's/Unix/UNIX/' file9 The UNIX operating system was pioneered by Ken Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s. One of the primary goals in the design of the UNIX system was to create an environment that promoted efficient program development. "or" $ sed 's/Unix/UNIX/' file9 > temp                             Make the changes    $ mv temp intro         ...

CUT

This command comes in handy when you need to extract (that is, "cut out") various fields of data from a data file or the output of a command. The general format of the cut command is cut -c chars file where chars specifies what characters you want to extract from each line of file . This can consist of a single number, as in -c5 to extract character 5; a comma-separated list of numbers, as in -c1,13,50 to extract characters 1, 13, and 50; or a dash-separated range of numbers, as in -c20-50 to extract characters 20 through 50, inclusive. To extract characters to the end of the line, you can omit the second number of the range; so  cut -c5- data extracts characters 5 through the end of the line from each line of data and writes the results to standard output. [root@server1 ~]# cat file9 The Unix operating system was pioneered by Ken Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s. One of the primary goals in the design of the Unix ...

Regular Expressions

Matching the Beginning of the Line: The Caret ( ^ ) When the caret character ^ is used as the first character in a regular expression, it matches the beginning of the line. So the regular expression ^Ashish : matches the characters Ashish  only if they occur at the beginning of the line . Matching the End of the Line: The Dollar Sign ( $ ) Just as the ^ is used to match the beginning of the line, so is the dollar sign $ used to match the end of the line. So the regular expression contents$ : matches the characters contents only if they are the last characters on the line . What do you think would be matched by the regular expression .$ ? Would this match a period character that ends a line? No. This matches any single character at the end of the line (including a period) recalling that the period matches any character. So how do you match a period? In general, if you want to match any of the characters that have a special meaning in forming regular e...

Standard Error

In  addition to standard input and standard output, there is another place known as  standard error. This is where most Unix commands write their error messages. And as with the other two "standard" places, standard error is associated with your terminal by default. In most cases, you never know the difference between standard output and standard error: [root@server1 ~]# ls n* ls: n*: No such file or directory  [root@server1 ~]# ls n* > foo ls: n*: No such file or directory You can also redirect standard error to a file by using the notation command 2> file No space is permitted between the 2 and the > . Any error messages normally intended for standard error will be diverted into the specified file , similar to the way standard output gets redirected. [root@server1 ~]# ls n* 2> errors [root@server1 ~]# cat errors ls: n*: No such file or directory

Standard Input/Output and I/O Redirection

[root@server1 ~]# who > users [root@server1 ~]# cat users root     pts/1        2011-04-12 17:29 (192.168.1.2) root     pts/2        2011-04-12 18:20 (192.168.1.2) [root@server1 ~]# wc -l < users 2

Asterisk

Filename Substitution The Asterisk [root@server1 myfirstdir]# touch chapt{1,2,3,4,5,6} [root@server1 myfirstdir]# ls chapt1  chapt2  chapt3  chapt4  chapt5  chapt6 The same substitution occurs if you use * with the echo command: [root@server1 myfirstdir]# echo * chapt1 chapt2 chapt3 chapt4 chapt5 chapt6 [root@server1 myfirstdir]# echo * : * chapt1 chapt2 chapt3 chapt4 chapt5 chapt6 : chapt1 chapt2 chapt3 chapt4 chapt5 chapt6 [root@server1 myfirstdir]# touch a b c [root@server1 myfirstdir]# ls a  b  c  chapt1  chapt2  chapt3  chapt4  chapt5  chapt6 [root@server1 myfirstdir]# echo *t1 chapt1 [root@server1 myfirstdir]# echo *t* chapt1 chapt2 chapt3 chapt4 chapt5 chapt6 [root@server1 myfirstdir]# touch aa bb cc [root@server1 myfirstdir]# touch aaa bbb ccc [root@server1 myfirstdir]# touch abc bca cba [root@server1 myfirstdir]# ls a  aa  aaa  abc  b  bb  bbb  bca  c  cba ...