Bash

Basic Linux BASH Scripting Tutorial

If you are using Linux, are you frustrated about the terminal? You copied and pasted commands from other forums, and it may or may not work, however, it is important to learn how to script.

I’m not teaching you how to fix your computer, I’m teaching you the syntax of BASH.

A command will be some strings seperated by spaces such as this:

$ echo "Hello, world!"

(please note, don’t type the $ before the command, it just the input prompt sign like [user@host]$)

This will run the program echo with string Hello, world! (without the quotes) as the first command line argument. The quotes will not be part of the command line argument, because it is a token.

What is a command line argument? It is a string that passed to a program to tell the program what you are doing. The command echo takes infinity amount of arguments and print them separated by spaces.
You don’t need quotes to enclose command line arguments if they are words such as hello, but you may need to if it contains operators such as brackets, semicolons, or other signs.

You can define variables in BASH, the variable will disappear after the end of the session unless you export it. To define a variable, put an identifier on the left side and the value on the right side.

$ A="A string"

There should be no whitespaces between the equal sign.
After you defined a variable, you can reference it by using the dollar sign($).

$ echo $A

It outputs “A string”.
Double quotes can interpolate variables, it means you can use dollar sign in double-quoted strings like this:

$ echo "The value is $A"

If you want to display a literal dollar sign, use single quotes or escape sequence. The character for escape sequence is backslash().

$ echo "This is a dollar sign: $"

or

echo 'This is a dollar sign: $'

If you want to display backslash, simply use double backslash like “\” in double-quoted string or you don’t need it at all for “=” or other invalid escapes. You can use backslashes to escape unquoted strings such as spaces.

$ cd /media/AAA/Program Files/

There is a backslash before the space, so it will not be separated into 2 arguments.
By the way, the cd command changes the working directory.

Most commands has help feature, add a –help after command to see help message.
Some useful commands includes ls, grep and cat. ls and dir lists all entries in the working directory, cat prints content of a file(or from standard input), grep takes a regular expression and an optional filename, and it outputs lines that matches the regular expression.
An example use of grep will be like this:

$ grep 03-01 log.txt
2009-03-01 5:00: .............
2009-03-01 6:13: ...........
2009-03-01 9:12: ........

The first argument of grep is the regular expression of be matches, the second one is the filename of input file, it is optional, if there is no input file, the program will ask you to input and repeats your input if that string has a match. An example of grep session is like this:

$ grep file
ABCDEFG
filename
filename
test
a

To end the program, type Ctrl+D to insert End-of-File(EOF) or Ctrl+Z to terminate.
However, you can input and output from a different stream, that is called redirection.

$ php test.php | grep "1 + 1"
1 + 1 = 2
15 - 1 + 1 = 15
35 / 1 + 1 = 36 

That command may be hard to understand, that is where redirection is used.
To understand redirection, you must understand the input and output streams.
A program opens three streams, standard input (stdin), standard output (stdout), and standard error (stderr).
When standard input is being read, the terminal will pause and ask you to input, you can type something with keyboard and press Enter. Standard input is normally be read, not written.

Standard output and standard error are two output streams, when they are written, new text will be shown to the terminal. stderr is used for error messages, and some programs highlights stderr red or they ignore stderr completely.
Program can open/close other streams such as file streams.

You can see the diagram in http://en.wikipedia.org/wiki/File:Stdstreams-notitle.svg .

Let’s go back to that command:

$ php test.php | grep "1 + 1"

The output of command php will be redirected as input of command grep.

Redirection diagram

The < and > operator controls input and output with files. > operator redirects and appends output to a file. For example, the following command line appends output of command ls to filename output.txt.

$ ls > output.txt

The >> operator will overwrite the file.

$ ls >> output.txt

The < operator puts a file’s content as input of a file.
The following command finds all TODO from file test.c

$ grep TODO < test.c

Of course, you can use cat and vertical pipe.

$ cat test.c | grep TODO

Many C/C++ programmers do that to find TODO, FIXME, XXX markers.

I will put some more complex and useful command here.

$ cat file.js | js | grep 180 | less

First, it puts contents of file.js as input to command js (a JavaScript interpreter), then put the output of that command to grep 180, and put output of grep to command less (shows scrollable text viewer).

$ ls | grep .php

Find all .php files in the working directory.

cat file.txt | grep ERROR

Finds string ERROR in file file.txt and output it.

That’s all for today. For more info about redirection, visit http://en.wikipedia.org/wiki/Redirection_(computing).

Tags: , ,

Friday, April 10th, 2009 Programming, Tutorial Comments Off