<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>BASH Programming - Introduction HOW-TO: Tables</TITLE> <LINK HREF="Bash-Prog-Intro-HOWTO-12.html" REL=next> <LINK HREF="Bash-Prog-Intro-HOWTO-10.html" REL=previous> <LINK HREF="Bash-Prog-Intro-HOWTO.html#toc11" REL=contents> </HEAD> <BODY> <A HREF="Bash-Prog-Intro-HOWTO-12.html">Next</A> <A HREF="Bash-Prog-Intro-HOWTO-10.html">Previous</A> <A HREF="Bash-Prog-Intro-HOWTO.html#toc11">Contents</A> <HR> <H2><A NAME="s11">11. Tables</A> </H2> <H2><A NAME="ss11.1">11.1 String comparison operators</A> </H2> <P> (1) s1 = s2 <P> (2) s1 != s2 <P> (3) s1 < s2 <P> (4) s1 > s2 <P> (5) -n s1 <P> (6) -z s1 <P> <P> (1) s1 matches s2 <P> (2) s1 does not match s2 <P> (3) __TO-DO__ <P> (4) __TO-DO__ <P> (5) s1 is not null (contains one or more characters) <P> (6) s1 is null <H2><A NAME="ss11.2">11.2 String comparison examples</A> </H2> <P> Comparing two strings. <BLOCKQUOTE><CODE> <PRE> #!/bin/bash S1='string' S2='String' if [ $S1=$S2 ]; then echo "S1('$S1') is not equal to S2('$S2')" fi if [ $S1=$S1 ]; then echo "S1('$S1') is equal to S1('$S1')" fi </PRE> </CODE></BLOCKQUOTE> <P> I quote here a note from a mail, sent buy Andreas Beck, refering to use <I>if [ $1 = $2 ]</I>. <P> This is not quite a good idea, as if either $S1 or $S2 is empty, you will get a parse error. x$1=x$2 or "$1"="$2" is better. <P> <H2><A NAME="ss11.3">11.3 Arithmetic operators</A> </H2> <P> + <P> - <P> * <P> / <P> % (remainder) <H2><A NAME="ss11.4">11.4 Arithmetic relational operators</A> </H2> <P> -lt (<) <P> -gt (>) <P> -le (<=) <P> -ge (>=) <P> -eq (==) <P> -ne (!=) <P> C programmer's should simple map the operator to its corresponding parenthesis. <H2><A NAME="ss11.5">11.5 Useful commands</A> </H2> <P> This section was re-written by Kees (see thank to...) <P> Some of these command's almost contain complete programming languages. From those commands only the basics will be explained. For a more detailed description, have a closer look at the man pages of each command. <P><B>sed</B> (stream editor) <P> <P> Sed is a non-interactive editor. Instead of altering a file by moving the cursor on the screen, you use a script of editing instructions to sed, plus the name of the file to edit. You can also describe sed as a filter. Let's have a look at some examples: <P> <BLOCKQUOTE><CODE> <PRE> $sed 's/to_be_replaced/replaced/g' /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> <P> Sed replaces the string 'to_be_replaced' with the string 'replaced' and reads from the /tmp/dummy file. The result will be sent to stdout (normally the console) but you can also add '> capture' to the end of the line above so that sed sends the output to the file 'capture'. <P> <BLOCKQUOTE><CODE> <PRE> $sed 12, 18d /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> <P> Sed shows all lines except lines 12 to 18. The original file is not altered by this command. <P><B>awk</B> (manipulation of datafiles, text retrieval and processing) <P> <P> Many implementations of the AWK programming language exist (most known interpreters are GNU's gawk and 'new awk' mawk.) The principle is simple: AWK scans for a pattern, and for every matching pattern a action will be performed. <P> Again, I've created a dummy file containing the following lines: <P> <I>"test123</I> <P> <I>test</I> <P> <I>tteesstt"</I> <P> <BLOCKQUOTE><CODE> <PRE> $awk '/test/ {print}' /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> test123 <P> <P> test <P> <P> The pattern AWK looks for is 'test' and the action it performs when it found a line in the file /tmp/dummy with the string 'test' is 'print'. <P> <BLOCKQUOTE><CODE> <PRE> $awk '/test/ {i=i+1} END {print i}' /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> <P> 3 <P> <P> When you're searching for many patterns, you should replace the text between the quotes with '-f file.awk' so you can put all patterns and actions in 'file.awk'. <P><B>grep</B> (print lines matching a search pattern) <P> <P> We've already seen quite a few grep commands in the previous chapters, that display the lines matching a pattern. But grep can do more. <BLOCKQUOTE><CODE> <PRE> $grep "look for this" /var/log/messages -c </PRE> </CODE></BLOCKQUOTE> <P> 12 <P> The string "look for this" has been found 12 times in the file /var/log/messages. <P> <P> [ok, this example was a fake, the /var/log/messages was tweaked :-)] <P><B>wc</B> (counts lines, words and bytes) <P> <P> In the following example, we see that the output is not what we expected. The dummy file, as used in this example, contains the following text: <I>"bash introduction</I> <I> howto test file"</I> <P> <BLOCKQUOTE><CODE> <PRE> $wc --words --lines --bytes /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> <P> 2 5 34 /tmp/dummy <P> <P> Wc doesn't care about the parameter order. Wc always prints them in a standard order, which is, as you can see: . <P><B>sort</B> (sort lines of text files) <P> <P> This time the dummy file contains the following text: <P> <I>"b</I> <P> <I>c</I> <P> <I>a"</I> <BLOCKQUOTE><CODE> <PRE> $sort /tmp/dummy </PRE> </CODE></BLOCKQUOTE> <P> <P> This is what the output looks like: <P> <P> <I>a</I> <P> <I>b</I> <P> <I>c</I> <P> <P> Commands shouldn't be that easy :-) <B>bc</B> (a calculator programming language) <P> <P> Bc is accepting calculations from command line (input from file. not from redirector or pipe), but also from a user interface. The following demonstration shows some of the commands. Note that <P> I start bc using the -q parameter to avoid a welcome message. <P> <BLOCKQUOTE><CODE> <PRE> $bc -q </PRE> </CODE></BLOCKQUOTE> <P> <P> <I>1 == 5</I> <P> <I>0</I> <P> <I>0.05 == 0.05</I> <P> <I>1</I> <P> <I>5 != 5</I> <P> <I>0</I> <P> <I>2 ^ 8</I> <P> <I>256</I> <P> <I>sqrt(9)</I> <P> <I>3</I> <P> <I>while (i != 9) {</I> <P> <I>i = i + 1;</I> <P> <I>print i</I> <P> <I>}</I> <P> <I>123456789</I> <P> <I>quit</I> <P><B>tput</B> (initialize a terminal or query terminfo database) <P> <P> A little demonstration of tput's capabilities: <BLOCKQUOTE><CODE> <PRE> $tput cup 10 4 </PRE> </CODE></BLOCKQUOTE> <P> The prompt appears at (y10,x4). <BLOCKQUOTE><CODE> <PRE> $tput reset </PRE> </CODE></BLOCKQUOTE> <P> Clears screen and prompt appears at (y1,x1). Note that (y0,x0) is the upper left corner. <BLOCKQUOTE><CODE> <PRE> $tput cols </PRE> </CODE></BLOCKQUOTE> <I>80</I> <P> Shows the number of characters possible in x direction. <P> It it higly recommended to be familiarized with these programs (at least). There are tons of little programs that will let you do real magic on the command line. <P> [some samples are taken from man pages or FAQs] <HR> <A HREF="Bash-Prog-Intro-HOWTO-12.html">Next</A> <A HREF="Bash-Prog-Intro-HOWTO-10.html">Previous</A> <A HREF="Bash-Prog-Intro-HOWTO.html#toc11">Contents</A> </BODY> </HTML>