Showing posts with label linux shell scripting. Show all posts
Showing posts with label linux shell scripting. Show all posts

Monday, July 26, 2010

Linux Shell Scripting part 2

Wild cards * and ?

The character * is a wildcard, and matches against one or more character(s) in a file (or directory) name.

Examples:

ls * will show all the files inside the current directory

ls *.s will show only the files which has .s extension

ls a* will show only the files which begin with a

ls ab* will show only the files which begin with ab

ls ab*.c will show only the files which begin with ab and have the .c extension

The character ? is also a wild cars which matches exactly one character in a file(or directory)name.

Examples:

ls ? will show only the files where their file name is one character long

ls bo? Will show only the files where their file name is 3 character long and starts with bo

Command line arguments

Consider the following command

cp ~/abc ~/Desktop

In this command there are two arguments. What if someone try to run this command without any of those arguments? the shell will probably give an error indicating arguments are missing.This point is crucial when someone uses commands which necessarily need arguments in their shell script. In that case they should run their shell script provided with those arguments.

Example :

Suppose our shell script name is comLineArgs.

Refresh memory on how to run it.

./comLineArgs

If someone needs to give the inputs to their shell scripts externally they can give them as command line arguments.

./comLine Args aaa bbb

The command line arguments which are used at a time can be also be accessed inside the sell script simply as variables.

Shell script name can be accessed as $0

Suppose there is n number of arguments. They can be accessed as $1, $2, $3……. $n

If someone needs to find the number of arguments $# can be used.

$* represents all the arguments.

Example Shell script describing how to access command line arguments.

echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

Input output redirection

Redirecting the output

> symbol is used to redirect the output of a command.

Example:

cat > file1

The above command creates a new file file1 or overwrites the existing file file1. Cat command reads the inputs given by the keyboard and redirects its output which is normally printed on the screen into the file1.

>> symbol is used to redirect the output of a command as well.

Example:

cat >> file1

Same thing as the > symbol but won’t overwrite the file adds more lines to the end of the file.

Redirecting the input

sort command sorts the contents of list1. Note that here inputs for the sort command are provided by the list1 file.

Pipes

Pipe is a temporary storage that output of a one command is stored and passes to another command as its input.

Example:

ls -l | wc -l

Output of ls command is given as input to wc command So that it will print number of files in current directory.

Saturday, July 24, 2010

Linux Shell Scripting part 1

Shell is a command line interpreter that executes commands read from the keyboard or from a file. It is not part of system kernel, but uses the system kernel to execute programs.

To find all available shells in your system type following command:

$ cat /etc/shells

To find your current shell type following command

$ echo $SHELL

Shell Scripting is series of command written in a plain text file and executing this text file without entering the commands one by one. Any text editor can be used for shell scripting.

Writing a simple shell script “first”

#
# My first shell script
#
clear
echo "My first shell script"

Explanation

1. # is used to represent a comment, which can be used to further explain what is happening in the script.

2. clear is clear command which clears the screen

3. echo is used to print on console

Setting execute permission for the script “first”

Syntax:
chmod permission script-name

Example:
$ chmod 755 first

Running the script “first”

$ ./first

Vriables in shell

1. System variables

Created and maintained by linux itself. Defined in capital letters.

Example: $HOME, $SHELL, $USERNAME

Using system variables

echo $HOME

2. User defined variables

Created and maintained by the user. Defined in simple letters.

Syntax: variable name=value

Example: name=abc

using user defined variables

name=abc

echo $name – this will print abc

echo name – this will print name

To access the user defined variables it is essential to write them followed by the $ sign.

Rules for Naming variable name

1. Variable name must begin with a letter or a number or an underscore, followed by one or more letters or numbers. Never use ?, * etc, for naming variable names.

2. When assigning value to variable never put spaces on either side of the equal sign
age=10 correct
age =10 incorrect
age= 10 incorrect
age = 10 incorrect

3. Variables are case-sensitive

Shell Arithmetic

Use to perform arithmetic operations.

Syntax:
expr operand1 operator operand2

Always put a space between the operand and the operator.

Examples:
expr 1 + 3
expr 2 - 1
expr 10 / 2
expr 20 % 3
expr 10 \* 3 multiplication use \* and not * since its wild card.
echo `expr 6 + 3` should be within back quotes(find back quote under ~) or it won’t print 9. If one uses single or double quotes it will print a + b instead of 9.