0% found this document useful (0 votes)
25 views

System Programming

The document discusses various topics related to system programming in shell including control structures like if-else-fi and case-esac statements, loops like while, for and until loops, functions, vi editor basics, security concepts, file system organization, file permissions and the chmod command.

Uploaded by

4rgx7fmvg9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

System Programming

The document discusses various topics related to system programming in shell including control structures like if-else-fi and case-esac statements, loops like while, for and until loops, functions, vi editor basics, security concepts, file system organization, file permissions and the chmod command.

Uploaded by

4rgx7fmvg9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

SYSTEM PROGRAMMING OLUFEMI OLOLADE

OLAEWE
ASSISTANT SOFTWARE
DEVELOPER
[BSc. , M.PHIL]
The if...else...fi statement is the advanced form of the if...fi
control
statement
• It allows shell to execute statements in a more controlled
manner while making decision between two choices.

IF...ELSE...FI
STATEMENT
EXAMPLE
Write a program to test the maximum between two unequal numbers
IF...ELIF...ELSE...FI STATEMENT

The if...elif...fi statement is an advance form of control statement that enables Shell to make correct
decision out of several conditions.
EXAMPLE
Write a program to detect the type of triangle using its sides
THE CASE...ESAC STATEMENT

Multiple if...elif statements can be used to perform a multiway branch,


but this is not always the best solution, especially when all of the branches depend on the value of a
single variable.
The case...esac statement handles exactly this situation, and it does so more efficiently than repeated
if...elif statements.
SHELL LOOPS
In everyday programming, loops are tools used to execute a block of code
repeatedly.
In Shell programming it is not different, loops are used to execute a set of commands repeatedly or
iteratively.
TYPES OF LOOP
• The while loop
• The for loop
• The until loop

• The select loop


THE WHILE LOOP

The while loop allows programmers to execute a set of commands repeatedly until some condition
occurs.
It is usually used when you need to manipulate the value of a variable repeatedly.
EXAMPLE
Here is a loop that counts from 0 to 7
THE FOR LOOP
The for loop in shell programming operates on a list of items.
That is, it repeats a set of commands for each and every item in the list.
The following example is used to display all the files starting with .bash and available in your home
directorate.
THE UNTIL LOOP
The until loop comes in handy whenever the need arises for us to execute a set of commands until a
condition becomes true.
THE SELECT LOOP
The select loop makes it much easier to create a numbered list of items or menu from which users can
select.
It is most useful when you need to ask the user to choose one or more items from a list of choices.
NESTING LOOPS
• Nesting is a concept in which a loop can be put inside another similar or different
loop.
• This nesting can go up to unlimited number of times based on your requirements.
• The syntax below illustrates how to nest a while loop in another while loop. Other
loops can be nested in a similar way.
 In the example below, a countdown while loop is added to a loop that counts
from 0 – 9.
• Note how echo -n works in this example.
• The -n option prevents echo from printing a new line character.
FUNCTIONS
Enables us to break down the overall functionality of a script into smaller, logical
subsections.
It is an excellent way to facilitate code reuse
INTRODUCTION TO VI EDITOR
There are several ways files can be edited in UNIX and
• one of the best ways is using screen-oriented text editor vi.
• The vi editor can be used to edit an existing file or to create a new file from
scratch.
• Also, it can just be used to read a text file.
• Other editors: emacs, ed, ex etc..
• The vi editor is considered the de facto standard editor because:
It's usually available on all the flavours of UNIX system.
 Its implementations are very similar across board.
 It requires very few resources.

 It is more user friendly.


NB
• Each line must begin with a tilde.
• If a line does not begin with a tilde and appears to be blank, then there is either a space, tab, newline,
or some other non-viewable character present.
VI EDITOR OPERATION MODES
Command mode
This mode enables you to perform administrative tasks such as
• saving files, executing commands, moving the cursor, cutting and pasting lines or
words, and finding and replacing.
• In this mode, whatever you type is interpreted as a command.
INSERT MODE
This mode enables you to insert text into the file.
• Everything that's typed in this mode is interpreted as input and finally it is put
in the file.
NOTES
The vi editor always starts in command mode and one needs to be in the insert mode
in order to enter text.
• To change to the insert mode, simply type i from the keyboard.
• To move from insert mode back to command mode, press the Esc key.
• If you are not sure which mode you are in, press the Esc key twice, to enter
command mode.
SAVING AND EXITING THE VI
• Once in the editor, it means a new file has been created or opened for editing
or viewing.
• In both cases you might want to exit the file saving possible modifications or
without saving changes.
• The command to exit the vi is :q
• If the file has been modified in any way, the editor will warn you of this, and
will not allow you to exit.
• to exit without saving changes, use :q!
• The command to save the content of the editor is :w
SAVING AND EXITING THE VI
To save and exit, combine the save and exit commands as :wq
• An easiest way to save changes and exit vi is using the ZZ command in
command mode. It is equivalent to :wq
• After creating or modifying a file you can save it with a different file name
using the command :w newfilename
MOVING WITHIN A FILE
• To move around within a file without affecting your text, you must be in command
mode (press Esc twice).
• Here are some of the commands you can use to move around one character at a
time:
NOTES
Note that vi is case-sensitive, attention should be paid when using capitalisation.
• Also, most commands in vi can be prefaced by the number of times you want the
action to occur.
• For example, 2j moves the cursor two lines down from the current cursor location.
Some advanced commands that are also used to move within the vi is
presented in the table below:
EDITING FILES
• In order to edit the content of a vi file, you need to be in the insert mode.
• There are many ways to enter insert mode from the command mode,
depending on the part of the file you want to modify.
• The table below summarizes the various entry techniques.
DELETING
CHARACTERS/LINES
NOTES
• As mentioned above, most commands in vi can be prefaced by the number of
times you want the action to occur.
• For example, 2x deletes two characters under the cursor location and 2dd
deletes two lines the cursor is on
CHANGE COMMANDS
You can also change characters, words, or lines in vi without deleting them.
COPY AND PASTE COMMANDS
vi enables us to copy lines or words from one place and past them at another
place.
ADVANCED COMMANDS
There are some advanced commands that simplify day-to-day editing and
allow for more efficient use of vi:
RUNNING COMMANDS
• It is possible to run commands from within the editor when the need arise.
• To run a command, simply go into command mode and type :! followed by
command.
• For example, if you want to check whether a file exists before you try to save
your file to that filename, you can type :! ls
• and you will see the output of ls on the screen.
• To return to your vi session, press any key (or the command's escape
sequence.)
SECURITY IN UNIX
• The first line of security in UNIX is at the login stage where a user is asked to
enter a valid user id and password.
• The userid and password are normally created by system’s superuser or
administrator,
• but the password can be changed by the user after he/she logs in.
• The passwd command can be used by a user to change his password. To do
so:
SECURITY IN UNIX CONTD
1.type passwd in the command prompt.
2.the system will ask you to enter the password you are currently using.
3.You will be asked to enter the new password
4.You will be asked to enter the new password again to confirm.
FILE SYSTEM
• User accounts are created for every user of a UNIX system so that we can
regulate which files or directories can be accessed and used by other users.
• All data in a UNIX System are organized into files, and these files are also
organized into directories.
• These directories are further organized into a tree-like structure called the
filesystem.
TYPES OF FILES IN UNIX.
1. Ordinary Files: An ordinary file is a file on the system that contains data,
text, or program instructions.
2. Special Files: Some special files provide access to hardware such as hard
drives, CD-ROM drives, modems, and Ethernet adapters.
Other special files are similar to aliases or shortcuts and enable you to access
a single file using different names.
3. Directories: Directories store both special and ordinary files.
For users familiar with Windows or Mac OS, UNIX directories are equivalent
to folders.
FILES PERMISSION
• Every file in UNIX belongs to a user or the superuser, and thus
• have the following attributes:
 Owner permissions:
 Group permissions
 Other (world) permissions
• Each file/directory has three permission modes for each of the above
mentioned categories of users:
• Read (r), Write (w), and Execute (x)
CHANGING PERMISSIONS
• The chmod (change mode) command is used to change file and directory
permissions.
• There are two main ways the chmod command can be used: symbolic mode and
absolute mode.
USING CHMOD IN SYMBOLIC
MODE
In the Symbolic Mode you can add, delete, or specify the permission set of a
file using the operators below.
To change the permission of a file named myfile from rwxrwxr-- to rwr-xrwx.
• The change is done using chmod in the manner shown below.
• u for user (owner of file), o stands for other users, and g for group.
USING CHMOD WITH
ABSOLUTE PERMISSIONS
• The absolute mode uses numbers to specify each set of permissions for a file.
• Each permission is assigned a value, as shown in the table, and
• the total of each set of permissions provides a number for that set.

You might also like