You are on page 1of 10

Operating Systems and Networking Lab

What is UNIX? UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. The UNIX operating system The UNIX operating system is made up of three parts; the kernel, the shell and the programs. The shell The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems). The shell is UNIX's command interpreter. It waits patiently for you to type a command, then figures out what you want to do. It either performs the action itself, or more likely will find and launch whatever you asked for. The shell takes care of finding your commands in the proper directory, maintaining your custom environment, interpreting command-line wildcards, and maintaining a history of your commands for editing and recall. Different types of shells Korn Shell (ksh), C shell (csh), Bourne Shell (bash) etc. How do I know what shell I am using ? Type echo $0 at the prompt. The kernel The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls. As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands. Files and processes Everything in UNIX is either a file or a process.

The Directory Structure All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )
/

(root) -- The / notes the "root" of the filesystem, where the entire system is contained. Unlike Microsoft Windows, where each drive has its own root directory named by a letter, such as C:\ or F:\, Unix holds the entire system in this single top-level directory, including each device and document. Thus, it is commonly said that in Unix, "everything is a file." Note that Unix uses the forward slash ( / ) rather than the backslash ( \ ) commonly used in Windows. -- Stands for "binaries"; Contains some fundamental utilities needed by a system administrator. -- Contains configuration files and some system databases.

/bin /etc

/dev

-- short for devices. Contains file representations of every peripheral device attached to the system. -- contains the home directories for the users.

/home /mnt

-- This is the default location to mount external devices like hard disk drives, pen drives etc. -- This is the depository of all integral UNIX system libraries. -- the home directory for the superuser root.

/lib

/root

UNIX Commands
1. ls -- to list files in a directory. 2. mkdir - make directory 3. cd change directory. Cd .. will take to the directory one level up in the hierarchy. 4. Pwd present working directory to find out where you are in the tree structure. 5. Cp copy ; cp file1 file2

6. Mv (move) rename files ; mv file1 file2 7. Rm remove files 8. Rmdir remove directories 9. Clear - Before you start the next section, you may like to clear the terminal window of

the previous commands so the output of the following commands can be clearly understood.
10. Cat - The command cat can be used to display the contents of a file on the screen.

cat

science.txt
11. More -- Displays text one screen at a time.

Cat science.txt | more


12. Grep -- It searches files for specified words or patterns.;

grep science science.txt


13. Wc word count to find word count of a file.

wc -w science.txt wc -l science.txt

( word count) (line count)

14. Chmod -- To set/modify a file's permissions

If we type at the prompt, ls l (l stands for long/detailed file listing), we see the following output:

Access rights on files.


r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate

Access rights on directories.


r allows users to list files in the directory; w means that users may delete files from the directory or move files into it; x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.

Eg of chmod usage : chmod go-rwx biglist -------- to remove read write and execute permissions on the file biglist for the group and others chmod a+rw biglist ----------- To give read and write permissions on the file biglist to all

15. ps - To see information about your processes, with their associated PID and status. To

background a process, type an & at the end of the command line.


16. Kill - to kill a process. ; Eg: kill 20077 where 20077 is the process id. If a process

refuses to be killed, uses the -9 option, i.e. type, kill -9 20077. ^C is used to kill the job running in the foreground.
17. Who to see list of users logged in 18. Man to get help

Shell Programming
Shell program is series of Linux commands. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. Shell script can take input from user, file and output them on screen. Useful to create our own commands that can save our lots of time and to automate some task of day today life.

Writing your first script To successfully write a shell script, you have to do three things: 1. Write a script 2. Give the shell permission to execute it 3. Put it somewhere the shell can find it Writing a script A shell script is a file that contains ASCII text. To create a shell script, you use a text editor, like vi, emacs,gedit etc. Type vi first.sh and put the following code into it. #!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!" Essential Vi Commands

Open a file:

vi filename

To go into edit mode:

press ESC and type I

To go into command mode:

press ESC

To save a file

press ESC and type :w fileName

To save a file and quit:

press ESC and type :wq OR press ESC and type :x

To jump to a line:

press ESC and type the line number

To Search for a string:

Press ESC and type /wordToSearch

To quit vi:

Press ESC and type :q

Setting permissions Chmod u+rwx first.sh Putting it in your path When you type in the name of a command, the system does not search the entire computer to find where the program is located. the shell maintains a list of directories where executable files (programs) are kept, and just searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message. his list of directories is called your path. You can view the list of directories with the following command: echo $PATH

You can add directories to your path with the following command, here directory is the name of the directory you want to add: export PATH=$PATH:directory

A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in. Executing the script: ./first.sh or sh first.sh Shell Variables There are two types of variable:
1. 2.

System Variables User Defined Variables

System Variables Created and maintained by Linux bash shell itself. This type of variable s defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG,HISTSIZE,and DISPLAY etc. Commonly Used Shell Variables The following variables are set by the shell: System Variable Meaning To View Variable Value Type echo $BASH_VERSION echo $HOSTNAME echo $CDPATH echo $HISTFILE

BASH_VERSION Holds the version of this instance of bash. HOSTNAME CDPATH HISTFILE The name of the your computer. The search path for the cd command. The name of the file in which command history is saved. The name of the file in which command history is saved.

HISTFILE

echo $HISTFILE

HISTFILESIZE

The maximum number of lines contained in the history file. The number of commands to remember in the command history. The default value is 500. The home directory of the current user.

echo $HISTFILESIZE

HISTSIZE HOME

echo $HISTSIZE echo $HOME

IFS

The Internal Field Separator that is used for word splitting after expansion and to split lines into words echo $IFS with the read builtin command. The default value is <space><tab><newline>. Used to determine the locale category for any category not specifically selected with a variable starting with LC_.

LANG

echo $LANG

PATH

The search path for commands. It is a colonseparated list of directories in which the shell looks echo $PATH for commands. Your prompt settings. echo $PS1

PS1

TMOUT

The default timeout for the read builtin command. Alsom in an interactive shell, the value is interpreted as the number of seconds to wait for input after echo $TMOUT issuing the command. If not input provided it will logout user. Your login terminal type. Set path to login shell. Set X display name echo $TERM export TERM=vt100 echo $SHELL echo $DISPLAY export DISPLAY=:0.1 export EDITOR=/usr/bin/vim

TERM SHELL DISPLAY

EDITOR

Set name of default text editor.

How Do I Display The Value Of a Variable?

Use echo command to display variable value. To display the program search path, type: echo "$PATH" To display your prompt setting, type: echo "$PS1" User Defined Variables Created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by the shell. Creating and setting variables within a script is fairly simple. Use the following syntax: varName=someValue You can display the value of a variable with echo $varName or echo ${varName}: echo "$varName"

Example shell scripts


Eg1 Write Script to see current date, time, username, and current directory #Eg1 echo "Hello, $LOGNAME" echo "Current date is `date`" echo "User is `who i am`" echo "Current direcotry `pwd`" Eg2: Write script called sayHello, put this script into your startup file called .bash_profile, the script should run as soon as you logon to system, and it print any one of the following message: Good Morning Good Afternoon Good Evening , according to system time. # Eg2 temph=`date | cut -c12-13` dat=`date +"%A %d in %B of %Y (%r)"` if [ $temph -lt 12 ]

then echo "Good Morning $LOGNAME, Have nice day!" fi if [ $temph -gt 12 -a $temph -le 16 ] then echo "Good Afternoon $LOGNAME" fi if [ $temph -gt 16 -a $temph -le 18 ] then echo "Good Evening $LOGNAME" fi