Home > bash scripting > Own Notes – Shell Script

Own Notes – Shell Script

Filename Wildcards

The following characters are interpreted by the shell as filename wildcards, and any word containing them is replaced by a sorted list of all the matching files.

Wildcards may be used in the directory parts of a pathname as well as the filename part. If no files match the wildcard, it is left unchanged. Wildcards are not full regular expressions. Sed, grep, awk etc. work with more flexible (and more complex) string matching operators.

*
Match zero or more characters.
?
Match any single character
[…]
Match any single character from the bracketed set. A range of characters can be specified with [ – ]
[!…]
Match any single character NOT in the bracketed set.

An initial “.” in a filename does not match a wildcard unless explicitly given in the pattern. In this sense filenames starting with “.” are hidden. A “.” elsewhere in the filename is not special.

Pattern operators can be combined

Example:
chapter[1-5].* could match chapter1.tex, chapter4.tex, chapter5.tex.old. It would not match chapter10.tex or chapter1

Basic sh script syntax

The most basic shell script is a list of commands exactly as could be typed interactively, prefaced by the #! magic header. All the parsing rules, filename wildcards, $PATH searches etc., which were summarized above, apply.
In addition:

# as the first non-whitespace character on a line
flags the line as a comment, and the rest of the line is completely ignored. Use comments liberally in your scripts, as in all other forms of programming.

\ as the last character on a line
causes the following line to be logically joined before interpretation. This allows single very long commands to be entered in the script in a more readable fashion. You can continue the line as many times as needed.

This is actually just a particular instance of \ being to escape, or remove the special meaning from, the following character.

; as a separator between words on a line
is interpreted as a newline. It allows you to put multiple commands on a single line. There are few occasions when you must do this, but often it is used to improve the layout of compound commands.

Shell Variables

When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:

$$ = The PID number of the process executing the shell.
$? = Exit status variable.
$0 = The name of the command you used to call a program.
$1 = The first argument on the command line.
$2 = The second argument on the command line.
$n = The nth argument on the command line.
$* = All the arguments on the command line.
$# The number of command line arguments.

The “shift” command can be used to shift command line arguments to the left, i.e $1 becomes the value of $2, $3 shifts into $2, etc. The command, “shift 2” will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.

Tests

There is a function provided by bash called test which returns a true or false value depending on the result of the tested expression. Its syntax is:

test expression

It can also be implied as follows:

[ expression ]

The tests below are test conditions provided by the shell:

-b file = True if the file exists and is block special file.
-c file = True if the file exists and is character special file.
-d file = True if the file exists and is a directory.
-e file = True if the file exists.
-f file = True if the file exists and is a regular file
-g file = True if the file exists and the set-group-id bit is set.
-k file = True if the files’ “sticky” bit is set.
-L file = True if the file exists and is a symbolic link.
-p file = True if the file exists and is a named pipe.
-r file = True if the file exists and is readable.
-s file = True if the file exists and its size is greater than zero.
-s file = True if the file exists and is a socket.
-t fd = True if the file descriptor is opened on a terminal.
-u file = True if the file exists and its set-user-id bit is set.
-w file = True if the file exists and is writable.
-x file = True if the file exists and is executable.
-O file = True if the file exists and is owned by the effective user id.
-G file = True if the file exists and is owned by the effective group id.
file1 –nt file2 = True if file1 is newer, by modification date, than file2.
file1 ot file2 = True if file1 is older than file2.
file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
-z string = True if the length of the string is 0.
-n string = True if the length of the string is non-zero.
string1 = string2 = True if the strings are equal.
string1 != string2 = True if the strings are not equal.
!expr = True if the expr evaluates to false.
expr1 –a expr2 = True if both expr1 and expr2 are true.
expr1 –o expr2 = True is either expr1 or expr2 is true.

The syntax is :

arg1 OP arg2

where OP is one of –eq, -ne, -lt, -le, -gt, or –ge. Arg1 and arg2 may be positive or negative integers or the special expression “–l string” which evaluates to the length of string.

getopts command

This command is used to check valid command line argument are passed to script. Usually used in while loop.
Syntax:
getopts {optsring} {variable1}

getopts is used by shell to parse command line argument.
As defined in man pages:
“optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable variable1, When an option requires an argument, getopts places that argument into the variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen, getopts places ? into variable1.”

Example:
We have script called ani which has syntax as
ani -n -a -s -w -d
Options: These are optional argument
-n name of animal
-a age of animal
-s sex of animal
-w weight of animal
-d demo values (if any of the above options are used their values are not taken)

Above ani script is as follows:
$ vi ani
#
# Usage: ani -n -a -s -w -d
#
#
# help_ani() To print help
#
help_ani()
{
echo “Usage: $0 -n -a -s -w -d”
echo “Options: These are optional argument”
echo ” -n name of animal”
echo ” -a age of animal”
echo ” -s sex of animal ”
echo ” -w weight of animal”
echo ” -d demo values (if any of the above options are used ”
echo ” their values are not taken)”
exit 1
}
#
#Start main procedure
#
#
#Set default value for variable
#
isdef=0
na=Moti
age=”2 Months” # may be 60 days, as U like it!
sex=Male
weight=3Kg
#
#if no argument
#
if [ $# -lt 1 ]; then
help_ani
fi
while getopts n:a:s:w:d opt
do
case “$opt” in
n) na=”$OPTARG”;;
a) age=”$OPTARG”;;
s) sex=”$OPTARG”;;
w) weight=”$OPTARG”;;
d) isdef=1;;
\?) help_ani;;
esac
done
if [ $isdef -eq 0 ]
then
echo “Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)”
else
na=”Pluto Dog”
age=3
sex=Male
weight=20kg
echo “Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)”
fi

Save it and run as follows
$ chmod +x ani
$ ani -n Lassie -a 4 -s Female -w 20Kg
$ ani -a 4 -s Female -n Lassie -w 20Kg
$ ani -n Lassie -s Female -w 20Kg -a 4
$ ani -w 20Kg -s Female -n Lassie -a 4
$ ani -w 20Kg -s Female
$ ani -n Lassie -a 4
$ ani -n Lassie
$ ani -a 2

See because of getopts, we can pass command line argument in different style. Following are invalid options for ani script
$ ani -nLassie -a4 -sFemal -w20Kg
No space between option and their value.

$ ani -nLassie-a4-sFemal-w20Kg
$ ani -n Lassie -a 4 -s Female -w 20Kg -c Mammal
-c is not one of the valid options.

gdb debugger commands

See the gdb(1) man page for more information on the gdb program. Below are listed some commands:

file – Loads the exe file.
kill – Terminates the program being debugged.
list – List sections of the source code.
next – Advances one line of source code in the current function and steps into other functions.
run – Executes the program that is being debugged.
quit – Ends gdb.
watch – Enables you to tell the value of a variable when it changes.
break – Set a break point.
make – Remake the exe program without quitting gdb.
shell – Run a UNIX shell command without leaving gdb.

References

http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html
http://www.freeos.com/guides/lsst/ch04sec14.html
http://www.linuxcommand.org/wss0130.php

Advertisements
Categories: bash scripting
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: