Looking for a Tutor Near You?

Post Learning Requirement » x
Ask a Question
x

Choose Country Code

x

Direction

x

Ask a Question

x

Hire a Tutor

Operating System Notes For Polytechnic Students(Computer Science)

Published in: Computer Science
6,983 Views

Here to resolve your queries regarding shell programming.... introduction and editors knowledge.

Komal / Kanpur

5 years of teaching experience

Qualification: M.Tech

Teaches: Algebra, Computer Science, Mathematics, B.Tech Tuition, M.Tech Tuition, Polytechnic, Computer, IT, BCA Subjects, MCA Subjects

Contact this Tutor
  1. 1. 2. Shell (definition) The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions. Shell Prompt: The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words. Following is a simple example of date command which displays current date and time: $date Thu Jun 25 MST 2009 Shell Types: In UNIX there are two major types of shells: The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character. The C shell. If you are using a C-type shell, the default prompt is the % character. There are again various subcategories for Bourne Shell which are listed as follows: Bourne shell ( sh) Korn shell ( ksh) Bourne Again shell ( bash) POSIX shell ( sh) The different C-type shells follow: C shell ( csh) TENEX/TOPS C shell ( tcsh) The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell". The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX.
  2. In this tutorial, we are going to cover most of the Shell concepts based on Borne Shell. Shell loop Loops are a powerful programming tool that enable you to execute a set of commands repeatedly. In this tutorial, you would examine the following types of loops available to shell programmers — The while loop The for loop The until loop The select loop You would use different loops based on dfferent situation. For example while loop would execute given commands until given condition remains true where as until loop would execute until a given condition becomes true. Once you have good programming practice you would start using appropriate loop based on situation. Here while and for loops are available in most of the other programming languages like C, C++ and PERL etc. Nesting Loops All the loops support nesting concept which means you can put one loop inside another similar or different loops. This nesting can go upto unlimited number of times based on your requirement. Here is an example of nesting while loop and similar way other loops can be nested based on programming requirement — Nesting while Loops It is possible to use a while loop as part of the body of another while loop. Syntax
  3. while commandl ; # this is loopl, the outer loop do Statement(s) to be executed if commandl is true while command2 ; # this is loop2, the inner loop do Statement(s) to be executed if command2 is true done Statement(s) to be executed if commandl is true done Example Here is a simple example of loop nesting, let's add another countdown loop inside the loop that you used to count to nine — #!/bin/sh while do while do echo -It 10 ] "$b" -ge 0 ] # this is loopl # this is loop2 expr $b - done echo done This will produce following result. It is important to note how echo -n works here. Here -n option let echo to avoid printing a new line character.
  4. 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 1 2 3 4 5 6 7 2 3 4 5 6 far 2 3 4 5 2 3 4 2 3 So you accomplish 21 e have looked at creating loops and working with loops to different tasks. Sometimes you need to stop a loop or skip iterations of the loop. In this tutorial you will learn following two statements used to control shell loops The break statement The continue statement The infinite Loop All the loops have a limited life and they come out once the condition is false or true depending on the loop. A loop may continue forever due to required condition is not met. A loop that executes forever without terminating executes an infinite number of times. For this reason, such loops are called infinite loops. Example Here is a simple example that uses the while loop to display the numbers zero to nine #!/bin/sh
  5. until do echo $a -It 10 ] done This loop would continue forever because a is alway greater than or equal to 10 and it would never become less than 10. The break statement The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop. Syntax The following break statement would be used to come out of a loop break The break command can also be used to exit from a nested loop using this format — break n Here n specifies the nth enclosing loop to exit from. Example Here is a simple example which shows that loop would terminate as soon as a becomes 5: #!/bin/sh while do echo $a -It 10 ]
  6. if [ $a -eq 5 ] then break done This will produce following result — 1 2 3 4 5 Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from a place called standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is also your terminal by default. Output Redirection The output from a command normally intended for standard output can be easily diverted to a file instead. This capability is known as output redirection: If the notation > file is appended to any command that normally writes its output to standard output, the output of that command will be written to file instead of your terminal — Check following who command which would redirect complete output of the command in users file. $ who > users
  7. into the specified file. complete content — $ cat users Notice that no output appears at the terminal. This is because the output has been redirected from the default standard output device (the terminal) 07:30 13:32 10:10 13:07 13:03 If you would check users file then it would have Oko ai ruth pat steve tty01 tty15 tty21 tty24 tty25 Sep Sep Sep Sep Sep 12 12 12 12 12 If a command has its output redirected to a file and the file already contains some data, that data will be lost. Consider this example — $ echo line 1 > users $ cat users line 1 You can use > > operator to append the output in an existing file as follows $ echo line 2 >> users $ cat users line 1 line 2 Input Redirection Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.
  8. The commands that normally take their input from standard input can have their input redirected from a file in this manner. For example, to count the number of lines in the file users generated above, you can execute the command as follows $ wc -1 users 2 users Here it produces output 2 lines. You can count the number of lines in the file by redirecting the standard input of the wc command from the file users $ wc -1 < users 2 Note that there is a difference in the output produced by the two forms of the wc command. In the first case, the name of the file users is listed with the line count; in the second case, it is not. In the first case, wc knows that it is reading its input from the file users. In the second case, it only knows that it is reading its input from standard input so it does not display file name. Here Document A here document is used to redirect input into an interactive shell script or program. We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script. The general form for a here document is command
  9. delimiter Here the shell interprets the < < operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command. The delimiter tells the shell that the here document has completed. Without it, the shell continues to read input forever. The delimiter must be a single word that does not contain spaces or tabs. Following is the input to the command wc -I to count total number of line — $wc -1 EOF This is a simple lookup program for good (and bad) restaurants in Cape Town. EOF 3 You can use here document to print multiple lines using your script as follows — #!/bin/sh cat EOF This is a simple lookup program for good (and bad) restaurants in Cape Town. EOF This would produce following result — This is a simple lookup program for good (and bad) restaurants in Cape Town.
  10. The following script runs a session with the vi text editor and save the input in the file test. txt. #!/bin/sh filename-test . txt vi $filename
  11. Here command is the name of the command you want to execute. The file /dev/null is a special file that automatically discards all its input. To discard both output of a command and its error output, use standard redirection to redirect STDERR to STDOUT - $ command > /dev/null Here 2 represents ST DERR and 1 represents STDOUT. You can display a message on to STDERR by redirecting STDOUT into STDERR as follows - $ echo message 1>&2 Redirection Commands Following is the complete list of commands which you can use for redirection — Command pgm > file pgm < file pgm > > file n >> file < < tag Description Output of pgm is redirected to file Program pgm reads its input from file. Output of pgm is appended to file. Output from stream with descriptor n redirected to file. Output from stream with descriptor n appended to file. Merge output from stream n with stream m. Merge input from stream n with stream m. Standard input comes from here through next tag at start of
  12. line. Takes output from one program, or process, and sends it to another. Note that file descriptor O is normally standard input (ST DIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR). Editors Vi editor . There are many ways to edit files in Unix and for me one of the best ways is using screen-oriented text editor vi. This editor enable you to edit lines in context with other lines in the file. Now a days you would find an improved version of vi editor which is called VIM. Here VIM stands for Vi 1Mproved. The vi is generally considered the de facto standard in Unix editors because It's usually available on all the flavors of Unix system. Its implementations are very similar across the board. It requires very few resources. It is more user friendly than any other editors like ed or ex. You can use vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file. Starting the vi Editor There are following way you can start using vi editor — Command Description
  13. vi filename vi -R filename view filename Creates a new file if it already does not exist, otherwise opens existing file. Opens an existing file in read only mode. Opens an existing file in read only mode.