User System Interaction Done via O'S' Commands - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

User System Interaction Done via O'S' Commands

Description:

Shell scripts can be executed with only the O.S. Installed. Shell commands / scripts are Interpreted i.e. as soon as a command is recognized , ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 23
Provided by: psd1
Category:

less

Transcript and Presenter's Notes

Title: User System Interaction Done via O'S' Commands


1
User System Interaction (Done via
O.S. Commands)
  • File System Commands .
  • Editor Invocation Commands.
  • Process Creation Manipulation Commands.
  • Control Flow Commands / Control Constructs.
  • Derived Commands.
  • User Utility Commands.

2
The Unix/ Linux Shell - 1 ( An Introduction)
  • As soon as any UNIX/LINUX O.S. is
    installed the following things are also installed
    automatically
  • a) The System Library (Helps to
    implement System Calls).
  • b) At least one command interpreter
    /shell that helps any user to interact with the
    system. There are several types of shells
    normally in use Cshell (the oldest) , bash
    (Bourne Again Shell) which is the most powerful.
    We shall use bash shell.
  • c) Some screen Editor like vi, emacs.

3
The Unix/ Linux Shell - 2 ( An Introduction)
  • Shell ? Command Interpreter A Programming
    Language
  • A Command Programming Language
  • Provides terminal mode Interface between any
    user and the underlying LINUX/UNIX Operating
    System. This includes the following
  • a) Helps user to issue any System
    Commands like ls, cat etc.
  • b) Enables an user to make use of System
    Calls like fork().
  • Enables an user to create of new tailor-made
    commands by suitably sequencing the existing
    commands and function calls through the use of
    some in built command language (The Shell
    Scripts).

4
The Unix/ Linux Shell ( The facilities)
  • System Commands and their Interpretation along
    with Command Line Parameters.
  • Two way communication is possible between the
    shell and commands.
  • Following control flow constructs are available
    (to be illustrated later)
  • a) Two Way branching like if-then-else.
  • b) Multi Way branching like case switch.
  • c) Looping constructs like for , while.
  • String Valued Parameters like file names or
    flags can be passed to a command.
  • A RETURN CODE is set by commands that may be used
    to determine
  • future / subsequent control-flow.
  • The standard output from a command may be used
    as Input to another shell command.
  • Enables use of Variables String substitution.

5
The Unix/ Linux Shell ( The facilities) - 2
  • The shell can modify the environment in which
    commands run.
  • Input and Output can be redirected to files.
  • Processes that communicates through pipes can be
    invoked.
  • Commands are found by searching directories in
    the file system in a sequence that can be defined
    by the user.
  • Commands can be read either from the terminal or
    from a user created file which allows command
    procedures to be stored for later use.
  • Shell scripts can be executed with only the O.S.
    Installed.
  • Shell commands / scripts are Interpreted i.e. as
    soon as a command is recognized , it gets
    executed . THEY ARE NOT COMPILED ? ( Why)?

6
A Few Simple Commands
  • It consists of one or more words separated by
    blanks.
  • Format
  • lt Commandgt ltblankgtltArgument1gtltblankgtltArgument2gt
    .
  • Display the current user who
  • Display the current Directory pwd
  • Display the contents of the current Directory
  • ls ( Short Display)
  • ls -l (Detailed Display)
  • N.B Each of these commands creates a new process
    and waits for it to finish before any other
    command is taken up.

7
Back ground Commands
  • Commands may be run without waiting for such
    background commands to finish . This actually
    forces sharing of the CPU time between the
    foreground and the background commands.
  • e.g. cc myfile.c ? Background Directive
  • This implies Compile the C Program Source myfile
    in the background. The Process Number of this
    created process is reported .
  • The List of running processes can be obtained
    using the ps command.

8
Input Output Redirection
  • ls l gt outfile ? Output the details Directory
    content to the existing file outfile after
    ERASING its current content OR create the file
    outfile and then write the Directory content
    there.
  • ls l gtgt outfile ? The details of the current
    Directory is appended to the existing file
    outfile.
  • wc -l lt infile ? The command wc l (Line
    counter) reads its standard input from the
    redirected input file infile.

9
Pipelines / Pipes
  • ls l gt outfile wc -l lt outfile ?
  • a) Output the details Directory content to
    the existing file outfile after ERASING its
    current content OR create the file outfile and
    then write the Directory content there.
  • b) Then use the file outfile as a parameter
    to the line counter command wc to count the
    number of lines generated by the ls l command.
  • Same effect can be achieved by the following
    combination of commands through pipes
  • ls l gt wc -l lt outfile (?)
  • Here is the pipe command that causes the
    output generated by ls l to be fed as input to
    the next command wc -l .
  • N.B 1) Any number of processes/commands can be
    piped in this way. Each process / command will
    run in parallel. When the Pipe becomes empty then
    ALL the subsequent successor commands will halt.
    Consequently at any point of time whenever the
    Pipe becomes full the feeder processes are halted
    automatically.

10
Filters
  • It is a command that reads its standard Input,
    transforms it in some way and prints the result
    as output.
  • e.g. ls grep old ? Here grep is the filter
    that selects from its input (here the output
    generated by the ls command) those lines that
    contain the specified parameter string old
  • who sort ? The filter sort here sorts its
    input here the logged in users.

11
Wild Cards File Name Generation
  • ls l main.c ? Prints details about the file
    main.c
  • ls l .c ? Prints details about ALL the C
    source files in the current directory.

12
Quoting Meta Characters ( Characters with a
special meaning)
  • Any character preceded by a \ is quoted and loses
    its special meaning , if any. The \ is elided so
    that
  • echo \? Will echo a single ?
  • Quoting a string is achieved by enclosing within
    single quotes .
  • Double quotes are also employed sometimes.

13
Changing the Prompt
  • Default first prompt . Can be changed using
    PS1ltNew Promptgt
  • Default Subsequent prompt after pressing newline
    (when more inputs are needed) happens to be gt
    which can be changed using PS2 ltNew Promptgt
  • e.g. PS1CAOS2K7 will cause all the Subsequent
    prompts to appear as
  • CAOS2K7

14
Shell and login
  • After login if the user directory contains the
    file .profile then it is assumed to contain
    commands and is read by the shell before reading
    any commands from the terminal (?).

15
Shell Procedures(Reading Executing Commands
stored in a File)
  • sh ltFile Name gt ltArgument Listgt
  • The file myfile contains
  • who grep 1
  • Then it can be executed by
  • sh myfile fred ? who grep fred
  • Alternately one can make this file executable by
    using the command chmod x myfile
  • Subsequently ./myfile fred ? sh myfile fred

16
Hello World!!!
  • My first shell program hello.sh
  • echo Hello world
  • OR
  • echo Hello world
  • allows you to put spaces between the words
  • To run the shell program
  • sh hello.sh
  • OR
  • chmod x hello.sh
  • ./hello.sh
  • Note for printing quotes use the escape
    sequence \

17
Using Variables in Shell
  • shell program using variables
  • var.sh
  • stringNice to meet you
  • num5
  • numexpr num 1 use back quotes for
    executing commands
  • echo string num
  • Variables have no type and need not be
    declared.
  • Variables only exist within the scope of the
    program, unless specified otherwise.

18
Special Variables
  • - number of parameters in the command line
  • 1, .., 9 Nine command line parameters
  • _at_ - all command line parameters
  • shell program using command line parameters
    add.sh
  • echo expr 1 2

19
If then.
  • shell program showing If then structure if.sh
  • if test 1 gt 2
  • then
  • echo 1
  • else
  • fi
  • echo 2
  • Common operators eq, -ne, -lt, -le, -gt, and
    ge

20
For loops
  • shell program for loops for.sh
  • for i in hello I am PSDEY
  • do
  • echo i done
  • OR
  • for i in
  • do
  • echo i done

21
While loops
  • shell program while loop while.sh
  • str
  • while str ! quit
  • do
  • echo Please type in a string (quit to exit)
  • read str
  • echo Input String str
  • done

22
While loops
  • shell program for printing a file line by line
    line.sh
  • file1
  • while read f do
  • echo f done lt file
Write a Comment
User Comments (0)
About PowerShow.com