Thursday, April 2, 2009

SHELL SCRIPTING -ADVANCED TIPS

Shell Script



A shell script is a list of

commands stored in a file that the shell executes noninteractively.


Scripting TIPS





1. Listing Files with F:


ls -F Will produce output different from

ls.

Now the output for the directory is

slightly different:
bin/ hosts lib/ res.03
ch07 hw1 pub/ test_results

ch07.bak hw2 res.01 users

docs/ hw3 res.02 work/

* As you can see, some of the items now

have a / at the end: each of these items is a directory. The other
items, such as hw1, have no character

appended to them. This indicates that they are ordinary files.

* When the -F option is specified to ls, it

appends a character indicating the file type of each of the items it
lists. The exact character depends on

your version of ls
. For ordinary files, no character is

appended.

*For special files, a character such as !,

@, or # is appended to the filename



2. Listing with -1:


In a shell script it is much easier to

manipulate the output when each file is listed on a separate line.
Fortunately ls supports the -1 option to

do this. For example,
$ ls -1

3. Numbering In Viewing:


The cat command also understands

several options. One of these is the -n option, which
numbers the output lines. You can use it

as follows:
$ cat -n hosts

4. Listing Only Directories:

If you don't want the

contents of the directory listed, specify the -d option to ls. This forces ls to display

only the name of the directory, not its contents:
$ ls -d /home/ranga
/home/ranga


5. Creating Necessary Directories in Mkdir:


you can specify the

-p (p as in parent) option to the mkdir command. It creates all the
necessary directories for you. For

example
$ mkdir -p /tmp/ch04/test1
creates all the required parent directories.

6. Quick Tutorial on File Attributes:


To determine a file's type, specify the -l

option to the ls. When this option is specified, ls lists the file type
for the specified files.
For example, the command
$ ls -l /home/ranga/.profile

produces the following output:
-rwxr-xr-x 1 ranga users 2368 Jul 11 15:57

.profile*

Here, you see that the very first character

is a hyphen (-). This indicates that the file is a regular file.

Special Characters for Different File

Types
Character File Type
- Regular file
l Symbolic link
c Character special file
b Block special file
p Named pipe
s Socket
d Directory file
I'll

To obtain file type information about a

directory, you must specify the -d option along with the -l option

NOTE:
Usually you need to know whether a

particular file is a binary program, a shell script, or a library. In these instances, the

file program is very useful.
It is invoked as follows:
file filename

Here, filename is the name of the file

you want to examine. As an example, on my system, the command
$ file /sbin/sh

produces the following output:

/sbin/sh: ELF 32-bit MSB executable

SPARC Version 1, statically linked,
stripped


7. LINK:

A symbolic link is a special

file that points to another file on the system.

ln -s source destination
* like a shortcut as in Windows

8.

You can access UNIX devices through

reading and writing to device files.
These device files are access points to

the device within the file systems.
Usually, device files are located under

the /dev directory.

The two main types of device files are

l Character special files
l Block special files

crw------- 1 ranga users 4, 0 Feb 7 13:47

/dev/tty0
The first letter in the output is c,

therefore you know that this particular file is a character special
file, but you also see two extra numbers

before the date. The first number is called the major number and
the second number is called the minor

number. UNIX uses these two numbers to identify the device driver
that this file communicates with.


9.Viewing Permissions

You can display the permissions of

a file using the ls -l command. For example, the following command
$ ls -l /home/ranga/.profile
produces the following output:
-rwxr-xr-x 1 ranga users 2368 Jul 11 15:57

.profile*
Because the first character is a hyphen (-),

you know that this is a regular file. Several characters appear
after this hyphen. The first three

characters indicate the permissions for the owner of the file, the next three
characters indicate the permissions for

the group the file is associated with, and the last three characters
indicate the permissions for all other

users


10. SID AND GID >>Additional permissions

are given to

programs via a mechanism known as the Set User ID ( SUID) and Set Group ID (

SGID) bits. When you execute a program that has the SUID bit enabled, you inherit

the permissions of that program's owner. Programs that do not have the SUID bit set

are run with the
permissions of the user who started the

program.

This is true for SGID as well. Normally

programs execute with your group permissions, but instead your
group will be changed just for this

program to the group owner of the program.
As an example, the passwd command,

used to change your password, is owned by the root and has the set SUID bit

enabled. When you execute it, you effectively become root while the command

runs.


The SUID and SGID bits will appear as

the letter "s" if the permission is available. The SUID "s"
bit will be located in the permission bits

where the owners execute permission would normally reside. For
example, the command
$ ls -l /usr/bin/passwd
produces the following output:
-r-sr-xr-x 1 root bin 19031 Feb 7 13:47

/usr/bin/passwd*

which shows that the SUID bit is set and

that the command is owned by the root. A capital letter S in the
execute position instead of a lowercase s

indicates that the execute bit is not set.


11. Setting Permissions -Quick Tutorial.


Letter Represents
u Owner
g Group
o Other
a All

actions

Symbol Represents
+ Adding permissions to the file
- Removing permission from the file
= Explicitly set the file permissions

permissions
Letter Represents
r Read
w Write
x Execute
s SUID or SGID

To give the "world" read access to all

files in a directory, you can use one of the following commands:
$ chmod a=r *
or
$ chmod guo=r *

To set the SUID and SGID bits for your

home directory, try the following:
$ cd ; chmod ug+s .



12. OCTAL METHOD:


Read permission has a value of 4
l Write permission has a value of 2
Execute permission has a value of

1
Adding the value of the permissions

that you want to grant will give you a number between 0 and 7.

This number will be used to specify the

permissions for the owner, group, and finally the other category.
Setting SUID and SGID using the octal

method places these bits out in front of the standard permissions.
The permissions SUID and SGID take on

the values 4 and 2, respectively.


In order to set the "world" read access to

all files in a directory, do this:
chmod 0444 *

13.Chown & Chgrp:


chown ranga:

/home/httpd/html/users/ranga
changes the owner of the given directory

to the user ranga.

The chown command will recursively

change the ownership of all files when the -R option is included. For
example, the command
chown -R ranga:

/home/httpd/html/users/ranga
changes the owner of all the files and

subdirectories located under the given directory to be the user ranga.

14.Background Processes That Require Input


If you run a background process that

requires input and do not redirect it to read a file instead of the
keyboard, the process stops. If you have

process and job monitoring enabled, pressing Enter at an empty
command prompt or starting a command

returns a message.

The following is an example of running a
command in the background that needs

input (using a simple program I created that is not part of UNIX):
$ i_need_input &

[1] + Stopped (SIGTTIN) i_need_input &
On some systems the message looks like

the following:
[1] + Stopped (tty input) i_need_input &
SIGTTIN (seen in the first example) is a

signal ( SIG) that tells me the program is waiting for terminal ( TT)
input ( IN)


15.
You can determine which key performs

which function by using the stty command. By entering
$ stty -a
you are shown the following, along with

a lot of other information:
intr = ^C; quit = ^\; erase = ^H; kill = ^U;

eof = ^D; eol = ^@
eol2 = ^@; start = ^Q; stop = ^S; susp =

^Z; dsusp = ^Y; reprint = ^R
discard = ^O; werase = ^W; lnext = ^


16.
When a foreground process is suspended,

a command prompt enables you to enter more commands; the
original process is still in memory but is

not getting any CPU time.

To resume the foreground process, you
have two choices--background and

foreground.

The bg command enables you to resume

the suspended
process in the background; the fg

command returns it to the foreground

Eg:
$ long_running_process
^Z[1] + Stopped (SIGTSTP)

long_running_process
$ long_running_process2
^Z[2] + Stopped (SIGTSTP)

long_running_process2
$
To move the first one to the background,

I use the following:
$ bg %1
[1] long_running_process &
$
The second process is still suspended

and can be moved to the background as follows:
$ bg %2
[2] long_running_process2 &



17. NoHup:


You can prevent a background process

from terminating, which is the default action, when you sign off or
are disconnected. The nohup command

prevents your process from getting the HUP (Hang UP) signal and
enables it to continue processing.


18:You can use exec to change your

shell interpreter completely without creating a subshell. To convert from ksh to csh,

you can use the following:

exec csh

19.Daemons--Daemons are system-related

background processes that often run with the permissions of root and services

requests from other processes.


Background Processes --Background

processes are autonomous processes that run under UNIX without requiring user

interaction.

20._FRUIT : Valid Variable Name in

SHELL SCRIPTS.

21. You can access all the items in an array in

one of the following ways:

${name[*]}
${name[@]}

22.The shell provides a way to mark variables as

read-only by using the readonly command. After a variable is marked read-only, its

value cannot be changed.



Consider the following commands:
$ FRUIT=kiwi
$ readonly FRUIT

23.Both scalar and array variables are unset

using the unset command:

unset name
// Unset ->Forget the Value Which is no

longer Needed...

24.Any characters within single quotes are

quoted just as if


25.File Test Options:


-b file True if file exists and is a block

special file.
-c file True if file exists and is a character

special file.
-d file True if file exists and is a

directory.
-e file True if file exists.
-f file True if file exists and is a regular

file.
-g file True if file exists and has its SGID

bit set.
-h file True if file exists and is a symbolic

link.
-k file True if file exists and has its

"sticky" bit set.
-p file True if file exists and is a named

pipe.
-r file True if file exists and is readable.
-s file True if file exists and has a size

greater than zero.
-u file True if file exists and has its SUID

bit set.
-w file True if file exists and is writable.
-x file True if file exists and is

executable.
-O file True if file exists and is owned by

the effective user ID.

26.Until loop ..


27. The basename command takes an absolute

or relative path and returns the file or directory name. Its basic

syntax is
basename file

28.Because the filenames and the column

headings are

both strings, you need the %s

format sequence for formatting. Then you need to pick a maximum size for the
filenames. If you pick 32 as the

maximum size of your format sequence, the first column becomes %32s.
Because you don't really care about the

size of the second column, you can stick with %s for that column.
With these changes your script becomes
#!/bin/sh
printf "%32s %s\n" "File Name" "File Type"


29.