MCSL-204 Windows and Linux Lab
MCSL-204 Windows and Linux Lab
MCSL-204 Windows and Linux Lab
WINDOWS AND
Indira Gandhi National Open University
LINUX LAB
School of Computer & Information Sciences
LAB MANUAL
SECTION 1
WINDOWS 10 5
SECTION 2
LINUX 14
PROGRAMME/COURSE DESIGN COMMITTEE
1.0 INTRODUCTION
This is the lab course, wherein you will have the hands on experience. You have
studied the course material (MCS-203 Operating Systems). A list of tasks to be
performed (sessionwise) on WINDOWS 10 is given. Please go through the general
guidelines and the program documentation guidelines carefully.
1.1 OBJECTIVES
After completing this lab course you will be able to:
Apply the concepts that have been covered in the theory course;
Understand the features WINDOWS;
Gain experience in overall interface;
Make use of different functions/operations of WINDOWS;
Work with the utilities in WINDOWS;
Feel more confident of downloading and configuring APPS, applications etc..
Write simple C programs and simulate the functionalities for the given tasks
and;
Know the alternative ways of providing solution to a given problem.
With Windows 10, Microsoft is trying to keep some of the touch and tablet features it
created for Windows 8, combine them with the familiar Start menu and Desktop, and
run it all on top of an improved operating system with more security, a new browser,
the Cortana assistant, its own version of Office for on-the-go editing and plenty of
new features intended to make life simpler.
On top of that, Windows 10 is more than just a PC operating system; it's also what
will run on Windows phones – and on small tablets as well, because a 6-inch phone
and a 7-inch tablet aren't such very different devices. Microsoft is expecting people to
put Windows 10 on a billion devices.
Microsoft has said it will update Windows 10 continuously, rather than release a new,
full-fledged operating system as a successor. The screenshot of the Windows 10
Desktop is given in fig 1.
5
WINDOWS AND
LINUX LAB
Cortana
Task Switcher
Most Windows users don't know the Alt-Tab keyboard combination to see and
switch between all running Apps, so as well as having a redesigned task
switcher with bigger thumbnails, Windows 10 also puts a task view icon in the
taskbar to help them find it.
6
WINDOWS 10
Taskbar
The Cortana search bar and task switcher button take up a large chunk of the
taskbar, which is also rather more subtle about showing you which icons are
for open programs, with just an underlined bar rather than a full highlight. The
standard tools in the system tray all get updates to the new Windows 10 look,
with a new menu showing available Wi-Fi, a new volume slider and a power
monitor that also lets you change screen brightness.
Snap Assist
Because all your Apps and programs run in windows on the desktop, instead of
modern Apps from the Store being in their own space, you can no longer drag
across the left edge of the screen to bring another app on screen and get a split
view. Instead, you drag windows into the corners of the screen to get the
familiar Snap view. You can now use all four corners of your screen if you
want each window to take up a quarter of the screen instead of half, and the
space that isn't filled by the window you just dragged shows thumbnails of
your other windows to make it easier to snap the next one into place.
Command Prompt
Learner who use the command prompt have been stuck with pretty much the
same experience since the 1990s, but in Windows 10 you can finally resize the
command prompt window and use familiar keyboard shortcuts to copy and
paste at the command prompt.
Windows 10 gets a new Windows Store, where you can download desktop
programs as well as modern Windows Apps. Many of those apps will be
universal apps that are the same code on a PC, a Windows phone, an Xbox
One and even on HoloLens, with the interface changing to suit the different
screen sizes. The Office for Windows Apps like Word and Excel are universal
Apps, as are the Outlook Mail and Calendar Apps.
File Explorer is also known as Windows Explorer. It has a core part of the
operating system. You can use the shortcut “Windows + e” on your keyboard.
When file Explore opens, we can access the frequently used folders and
recently use files. The new “Home” view in Explorer shows you a Quick
Access list of useful locations and folders you visit frequently, with a list of
recently opened files underneath it, which is faster than having to go to the
Recent Places link in older versions of Windows. The Share tab on the ribbon
gets a makeover too.
7
WINDOWS AND
LINUX LAB Improved Multitasking
A new Multiple Desktops feature lets you run another set of windows as if on
another screen, but without the physical monitor. Instead of having multiple
windows open on top of each other on one desktop, you can set up a whole
other virtual desktop for those programs to reside in. Set up one specifically for
home and leave your Apps such as Netflix and Amazon open, and create
another desktop for work on which you keep Word, Excel and Internet
Explorer open. With the new desktops comes a new way to keep track of your
open Apps on Windows 10. On the new operating system, you can either hit
the new Task View button on the task bar or swipe in from the left edge of the
screen to pull up a one-page view of all your open Apps and files. It's not much
different from using the Alt-Tab combination shortcut on your keyboard, but
this presents a convenient way for touch-oriented users to get an overview of
what's running.
Action Center
If you've used Windows Phone 8.1 (or Android and/or iOS), you’re used to a
notification centre you can drag down from the top of the screen. Windows 10
puts that on the right of the screen, where the charms bar was in Windows 8,
with notifications from various Apps at the top and your choice of various
settings buttons at the bottom for quick access.
Microsoft developed the Xbox app for game streaming to Windows 10 back in
January; we can meet up with friends online, see what your friends are playing.
The Xbox app dashboard has been updated to support the new feature; the
Xbox one app for Windows 10 is not quite ready yet. This feature allows you
to leave your living room and play your favorite Xbox One games anywhere
with access to your home network.
Microsoft gives the features of upgraded task manager even manages a startup
program without third party software. Windows 10 includes Windows
Defender by default, Window Defender is just changed the version of
Microsoft Security Essentials. Window Defender has antivirus protection to
safe your system.
The Windows 8 Settings app has taken over many more of the settings that
used to be in Control Panel, and it has a Control Panel-style interface with
icons to navigate with. But the old Control Panel interface is still there, for
settings that aren't in the new Settings app (or if you're just used to finding
things there).
DirectX 12
Phone Companion
Windows 10 includes a new app to help you get your phone set up to work
with your PC and with any Microsoft services you use – like Cortana, Skype,
Office and OneDrive. So you can plug in an iPhone and set it up to back up
photos to OneDrive or get your Xbox Music tracks on an Android phone.
Other Features
9
WINDOWS AND
LINUX LAB can be stable • Private catalog: A feature that provides a list of applications that
users within the organization can download Apps from
Virtual desktops: A feature that allows you to run and switch between
multiple desktops
Windows Spotlight: An option that displays a new image on the lock screen
each day
Windows To Go: A feature that allows you to boot and run Windows from
USB mass storage devices such as USB flash drives and external hard drives
You may seek assistance in doing the lab exercises from the concerned lab
instructor. Since the assignments have credits, the lab instructor is obviously not
expected to tell you how to solve these, but you may ask questions concerning the
Windows Operating system.
10
WINDOWS 10
The comment block above the main function should describe the purpose of the
program. Proper comments are to be provide where and when necessary in the
programming.
The program should be interactive, general and properly documented with real
Input/ Output data.
If two or more submissions from different students appear to be of the same origin
(i.e. are variants of essentially the same program), none of them will be counted.
You are strongly advised not to copy somebody else's work.
As soon as you have finished a lab exercise, contact one of the lab instructor /
incharge in order to get the exercise evaluated and also get the signature from
him/her on the Observation book.
The total no. of lab sessions (3 hours each) are 10 and the list of tasks are
provided session-wise. It is important to observe the deadline given for each task.
In the next section, the lists of lab assignments to be performed sessionwise are given.
Practice them all in order to gain good hands-on experience.
13
WINDOWS AND
LINUX LAB SECTION 2 LINUX
Structure Page Nos.
2.0 Introduction 14
2.1 Objectives 14
2.2 History of UNIX and LINUX 15
2.3 Features of LINUX 16
2.4 Kernel and the Shell 17
2.4.1 Commands and Processes
2.4.2 LINUX File System
2.4.3 Wild Card Characters
2.4.4 Syntax of LINUX Commands
2.4.5 Getting Help
2.5 LINUX Commands 22
2.6 Description of Commonly Used LINUX Commands 26
2.7 Introduction to Shell Programming 34
2.7.1 LINUX Shells
2.7.2 Deciding on a Shell
2.8 Bourne Again Shell Programming 36
2.9 General Guidelines 42
2.10 Practical Sessions 44
2.11 Summary 48
2.12 Further Readings 48
2.13 Website References 48
2.14 Virtual Labs / IDEs / Online Lab Resources 49
2.0 INTRODUCTION
This is the lab course, wherein you will have the hands on experience. You have
studied the course material (MCS-203 Operating Systems). A list of tasks to be
performed (sessionwise) on LINUX is given along with some programming problems
towards the end. Please go through the general guidelines and the program
documentation guidelines carefully.
2.1 OBJECTIVES
After completing this lab course you will be able to:
Apply the concepts that have been covered in the theory course;
Understand the features LINUX;
Gain experience in overall command line interface as well as GUI;
Make use of different functions/operations of LINUX;
Work with the utilities in LINUX;
Write simple shell programs and simulate the functionalities for the given
tasks;
To understand and make effective use Shell scripting language Bash to
solve Problems.
To implement in C some standard Linux utilities such as ls, mv, cp etc.
using system calls.
To develop the skills necessary for systems programming including file
system programming, process and signal management, and interprocess
communication and;
Know the alternative ways of providing solution to a given problem.
14
LINUX
Let us study the features of LINUX operating system in the following section.
1. It helps in distributing the software carrying its source codes freely to the
users.
2. It supports the users to add new features, debug, and correct errors in the
source code.
3. It helps in redistributing the new, improved source code back again free of
cost to other users.
Linux Distributions
The main control program in a UNIX operating system is called the kernel. However,
the kernel does not allow the user to give its commands directly; instead when the user
types commands on the keyboard they are read by another program in the Operating
System called a shell which parses, checks, translates and then passes them to the
kernel for execution.
There are a number of different shells available, with names such as bash, zsh, Korn,
Tcsh, Fish each with different rules of syntax; these are partly though not completely
responsible for the diversity of LINUX. Later in our discussion we will see what the
criteria to select a shell are. Once the command has been interpreted and executed, the
kernel sends its reply, which may simply be a prompt for the next command to be
entered, either directly to the display monitor. This is a program responsible for
deciding where and in what form the output will appear on the display monitor. If for
any reason the kernel cannot perform the command requested (wrong syntax), for
example, the reply will be an error message; the user must then re-enter the corrected
command.
17
WINDOWS AND
LINUX LAB 2.4.1 Commands and Processes
The kernel and the shell programs running in the CPU are examples of processes;
these are self-contained programs that may take over complete control of the
CPU. Although there can only be one kernel process running in a particular CPU,
there may be any number of shell and other processes, subject of course to memory
limitations.
Some commands to the shell are internal (or built-in), that is, they only involve the
shell and the kernel. Others are external and may be supplied with the OS, or may be
user-written. An external command is the name of a file which contains either a
single executable program or a script. The latter is a text file, the first line of which
contains the name of a file containing an executable program, usually but not
necessarily a shell, followed by a sequence of commands for that program. A script
may also invoke other scripts – including itself. Its purpose is simply to avoid having
to re-type all the command it contains.
A command may also be an alias for an internal or external command (e.g., the user
may not like the LINUX name “rm” for the command which deletes files, and may
prefer to alias it to “delete”).
The external command may optionally cause execution of the shell process to be
temporarily suspended, and then run another program, which may then take over input
from the keyboard and mouse and send output for display. The shell may or may not
wait for the program to finish, before it wakes up again and cause its prompt to be
displayed. It is very important that the user be continuously aware of which process is
currently reading keyboard input: the shell or another program, because they usually
speak completely different languages.
The above is an example of a parent process – the shell, and a child process – the
external program. In fact the child could just as well have been, and often is, another
invocation of the same shell, or of a different shell, and the child process can be the
parent of other child and so on (almost) ad infinitum. Consequently a typical LINUX
system has many processes either waiting or running.
Like other Operating systems, LINUX organizes information into files, and related
files may be conveniently organized in directories. Files may contain text, data,
executable programs, scripts (which are actually just data for a scripting program such
as a shell), and may also be links to other files, or to physical devices or
communications channels.
Files Usability
/boot/vmlinuz The Linux Kernel file.
/dev/hda Device file for the first IDE HDD (Hard
Disk Drive).
/dev/hdc Device file for the IDE Cdrom,
commonly.
/dev/null A pseudo device, that don’t exist.
Sometime garbage output is redirected
to /dev/null, so that it gets lost, forever.
/etc/bashrc Contains system defaults and aliases used
by bash shell.
/etc/crontab A shell script to run specified commands
on a predefined time Interval.
/etc/exports Information of the file system available
on network
/etc/fstab Information of Disk Drive and their
mount point.
/etc/group Information of Security Group.
/etc/grub.conf
grub bootloader configuration file.
/etc/init.d
Service startup Script.
/etc/lilo.conf
lilo bootloader configuration file.
/etc/hosts
Information of Ip addresses and
corresponding host names
/etc/hosts.allow
List of hosts allowed to access services
on the local machine.
/etc/host.deny List of hosts denied to access services on
the local machine.
/etc/inittab
INIT process and their interaction at
various run level.
/etc/modules.conf Configuration files for system modules.
/etc/passwd Contains password of system users in a
shadow file, a security implementation.
/etc/printcap
Printer Information.
/etc/profile
Bash shell defaults.
/etc/resolv.conf
Domain Name Servers (DNS) being used
by System.
/etc/securetty Terminal List, where root login is
possible.
20
LINUX
/usr/bin
Normal user executable commands.
/usr/include
Contains include files used by
‘c‘ program.
/usr/sbin Commands for Super User, for System
Administration.
/proc/cpuinfo
CPU Information.
/usr/lib
Library files which are required during
program compilation.
/usr/share Shared directories of man files, info files,
etc..
/proc/filesystems : File-system Information being used
currently.
Always note that LINUX is always fussy about the case of letters in commands,
usernames, passwords and filenames; so Vvs.data is not the same file as vvs.data.
command [option(s)] [argument(s)] (the [ ] here indicate that the items they contain
are optional; they are not part of the syntax).
If a typing error is made the line may be changed using the left/right arrow keys to
move the cursor and the Backspace key to delete the character to the left of the cursor;
new characters are inserted before the cursor. After keying-in the desired command,
press the Enter key to execute the command. The command may be cancelled before
execution by using Ctrl-u (hold down the Ctrl key and press the u key). If an
incorrectly spelled command is entered and spelling correction is enabled in the shell,
the shell will attempt to correct the mistake and ask for verification.
If the shell has filename completion enabled, use of the Tab key after part of a
command or filename has been typed will cause the shell to attempt completion of the
name, up to the character where the result is unique. Use of the Ctrl-d key will cause
all names that match what has been typed to be listed.
The options and arguments if present must be separated from the preceding item by at
least one space. However, multiple options may or may not need to be separated from
each other by at least 1 space; multiple arguments are always separated from each
21
WINDOWS AND
LINUX LAB
other by at least 1 space. Options usually start with -, but occasionally it is a +, and
sometimes the – or + may be omitted.
A line may contain several commands (each possibly followed by options and/or
arguments) separated by semicolons (;). The commands are executed in sequence,
just as if they had been typed on separate lines. If it is necessary to continue a
command onto the next line, end the line with a backslash (\), press Enter and
continue typing on the next line.
22
LINUX
System Information
Information
23
WINDOWS AND
LINUX LAB
Devices
df summarize free space on disk device
du show disk space used by files or directories
fdisk –l to display disks partition sizes and types
Hardware Information
dmesg to display messages in kernel ring buffer
free –h to display free and used memory
lspci –tv to display PCI devices
lsusb –tv to display USB devices
dmidecode to display DMI/SMBIOS(h/w info) from the BIOS
hdparm – i/dev/sda to show information about the disk sda
badblocks –s/dev/sda to test for unreadable blocks on disk sda
Installing Packages
Practice all the above commands so as to gain good hands-on experience on LINUX.
The description for the most commonly used LINUX commands is given below in an
alphabetic order.
cat
cat allows you to read multiple files and then print them out. You can combine files
by using the > operator and append files by using >>.
Syntax: cat [argument] [specific file]
Example:
cat abc.txt
If you want to append three files (abc.txt, def.txt, xyz.txt), give the command as,
cat abc.txt def.txt xyz.txt > all
cd, chdir
cd (or chdir) stands for “change directory”. This command is the key command to
move around your file structure.
Syntax: cd [name of directory you want to move to]
When changing directories, start with / and then type the complete file path, like
cd /vvs/abc/xyz
chmod
chmod (which stands for “change mode”) changes who can access a particular file. A
“mode” is created by combining the various options from who, opcode, and
permission.
rwxrwxrwx
Each set of rwx represents user, group, and other respectively. Only the owner of a
file or a privileged user may change the permissions on a file. There are two ways to
change permissions on a file or directory, either numerically or by using lettered
commands. Both ways use the command chmod. To add permissions to a file, you use
+, to remove permissions you use-.
To allow a group (mony, in this case) “write” access, you would type:
chmod g+w vvs.txt
If you wanted to remove “read” ability from “other” you would type:
The first digit means readable and writable for the user (4+2+1), the second digit
means readable and writable for the group (4+2+0), and the third digit means readable
for other (4+0+0).
If you want to change the permissions on a directory tree use the -R option. chmod -R
will recursively change the permissions of directories and their contents.
27
WINDOWS AND
LINUX LAB
chown
chown changes who owns a particular file or set of files. New owner files refer to a
user ID number or login name that is usually located in the /etc/password directory.
The owner of a file or directory can be seen by using the command.
Only the owner of a file or a privileged user can change the permissions on a file or
directory. The following example changes the owner of vvs.txt to sridhar
The cp command copies files or directories from one place to another. You can copy a
set of files to another file, or copy one or more files under the same name in a
directory. If the destination of the file you want to copy is an existing file, then the
existing file is overwritten. If the destination is an existing directory, then the file is
copied into that directory.
If you want to copy the file favourites.html into the directory called laksh, you give
the command as:
cp favourites.html /vvs/laksh/
A handy option to use with cp is -r. This recursively copies a particular directory and
all of its contents to the specified directory, so you won’t have to copy one file at a
time.
date
The date command can be used to display the date or to set a date.
The first structure shows how date can be used to display the current date. A certain
format can be specified in which the date should be displayed. Check the LINUX
manual for specific formats and options. The second structure allows you to set the
date by supplying a numeric string. Only privileged users will be able to use this
second command structure.
diff
diff displays the lines that differ between two given files.
diff can be an extremely valuable tool for both checking errors and building new
pages. If you run a diff between two files, you'll be shown what differences the files
have line by line. The lines referring to file1 are marked with the < symbol. The lines
referring to file2 are marked by the > symbol. If the file is a directory, diff will list the
file in the directory that has the same name as file2. If both of the files are directories,
diff will list all the lines differing between all files that have the same name.
28
LINUX
If you have a file that is not working properly, it can be a great help to check it against
a similar file that is working. It will often quickly alert you to a line of code that’s
missing.
A handy option to use if you want to generally compare two files without noting the
complex differences between them is the -h option (h stands for half-hearted). Using -i
as an option will ignore differences in uppercase and lowercase characters between
files, and -b will ignore repeating blanks and line breaks.
exit
The exit command allows you to terminate a process that is currently occurring.
For example, if you wanted to leave a remote host that you were logged onto (see
rlogin also), you should type exit. This would return you to your home host.
find
find searches through directory trees beginning with each pathname and finds the files
that match the specified condition(s). You must specify at least one pathname and one
condition.
There are several handy conditions you can use to find exactly what you want. The -
name condition will find files whose names match a specified pattern. The structure
for the name condition is:
The condition -print will print the matching files to the pathname specified. -print can
also be used in conjunction with other conditions to print the output.
If you wanted to find all the files named favorites.html in the directory Ram, then
you’d do this:
This looks through the directory Ram and finds all the files in that directory that
contain favorites.html, then prints them to the screen. Your output would look like
this:
/Ram/sixteen_candles/favorites.html
/Ram/favorites.html
/Ram/breakfast_club/favorites.html
All meta-characters (!, *, ., etc.) used with -name should be escaped (place a \ before
the character) or quoted. Meta-characters come in handy when you are searching for a
pattern and only know part of the pattern or need to find several similar patterns. For
example, if you are searching for a file that contains the word “favorite”, then use the
meta-character * to represent matching zero or more of the preceding characters. This
will show you all files which contain favorite.
This looks through the directory Ram and finds all the files in that directory that
contain the word “favorite”. The output would look like this:
29
WINDOWS AND
LINUX LAB
/Ram/sixteen_candles/favorites.html
/Ram/favorites.html
/Ram/least_favorites.html
/Ram/breakfast_club/favorites.html
/Ram/favorite_line.html
The -user condition finds files belonging to a particular user ID or name.
finger
grep
The grep command searches a file or files for lines that match a provided regular
expression (“grep” comes from a command meaning to globally search for a regular
expression and then print the found matches).
To exit this command, type 0 if lines have matched, 1 if no lines match, and 2 for
errors. This is very useful if you need to match things in several files. If you wanted to
find out which files in our vvs directory contained the word “mca” you could use
grep to search the directory and match those files with that word. All that you have to
do is give the command as shown:
The * used in this example is called a meta-character, and it represents matching zero
or more of the preceding characters. In this example, it is used to mean “all files and
directories in this directory”. So, grep will search all the files and directories in vvs
and tell you which files contain “mca”.
head
head prints the first couple of lines of one or multiple files. -n is used to display the
first n lines of a file(s). The default number of lines is 10.
Syntax: head [-n] [files]
For example, the following command will display the first 15 lines of favourites.html.
head -15 favourites.html
kill
kill ends the execution of one or more process ID’s. In order to do this you must own
the process or be designated a privileged user. To find the process ID of a certain job
give the command ps.
Syntax: kill [options] PIDs
There are different levels of intensity to the kill command, and these can be
represented either numerically or symbolically. kill -1 or HUP makes a request to the
server to terminate the process, while kill -9 or kill KILL forces a process to terminate
30
LINUX
absolutely. Most politely, LINUX users will attempt to kill a process using -1 first
before forcing a process to die.
less
less is similar to more in that it displays the contents of files on your screen. Unlike
more, less allows backward and forward movement within the file. It does not read
the whole file before displaying its contents, so with large files less displays faster
than more. Press h for assistance with other commands or q to quit.
lprm
The lprm command will remove a job or jobs from a printer’s queue. If lprm is used
without any arguments, it will delete the active job if it is owned by the user. If the
command is used with -, then all the jobs owned by the user will be removed. To
remove a specific job, use the job number.
ls
ls will list all the files in the current directory. If one or more files are given, ls will
display the files contained within “name” or list all the files with the same name as
“name”. The files can be displayed in a variety of formats using various options.
ls is a command you'll end up using all the time. It simply stands for list. If you are in
a directory and you want to know what files and directories are inside that directory,
type ls. Sometimes the list of files is very long and it flies past your screen so quickly
you miss the file you want. To overcome these problems give the command as shown
below:
ls | more
The character | (called pipe) is typed by using shift and the \ key. | more will show as
many files as will fit on your screen, and then display a highlighted “more” at the
bottom. If you want to see the next screen, hit enter (for moving one line at a time) or
the spacebar (to move a screen at a time). | more can be used anytime you wish to
view the output of a command in this way.
A useful option to use with ls command is -l. This will list the files and directories in a
long format. This means it will display the permissions (see chmod), owners, group,
size, date and time the file was last modified, and the filename.
There are several other options that can be used to modify the ls command, and many
of these options can be combined. -a will list all files in a directory, including those
31
WINDOWS AND
LINUX LAB
files normally hidden. -F will flag filenames by putting / on directories, @ on
symbolic links, and * on executable files.
man
The man command can be used to view information in the online LINUX manual.
man searches for information about a file, command, or directory and then displays it
on your screen. Each command is a subject in the manual. If no subject is specified,
you must give either a keyword or a file. You can also search for commands that serve
a similar purpose. For example, if you want more information about the chmod
command, you should type:
man chmod
A screen will then appear with information about chmod. Type q to quit.
mkdir
For example, to create a directory called parkhyath in the present working directory,
give the command as,
mkdir prakhyath
more
To have the next line displayed, hit the return key, otherwise press the spacebar to
bring up the next screen. Press h for assistance with other commands, n to move to the
next file, or q to quit.
mv
mv moves files and directories. It can also be used to rename files or directories.
If you wanted to rename vvs.txt to vsv.txt, you should give the command as:
mv vvs.txt vsv.txt
After executing this command, vvs.txt would no longer exist, but a file with name
vsv.txt would now exist with the same contents.
passwd
32
LINUX
The passwd command creates or changes a user’s password. Only the owner of the
password or a privileged user can make these changes.
ps
The ps command prints information about active processes. This is especially useful if
you need to end an active process using the kill command. Use ps to find out the
process ID number, then use kill to end the process.
Syntax: ps [options]
pwd
pwd prints the pathname of the current directory. If you wanted to know the path of
the current directory you were in you give the command as pwd. You will get the
complete path.
rlogin
The rlogin command, which stands for remote login, lets you connect your local host
to a remote host.
If you wanted to connect to the remote host vsmanyam and you were on sree, you
would do this:
In order to remove a file, you must have write permission to the directory where the
file is located. While removing a which does’t have write permission on, a prompt
will come up asking you whether or not you wish to override the write protection.
The -r option is very handy and very dangerous. -r can be used to remove a directory
and all its contents. If you use the -i option, you can possibly catch some disastrous
mistakes because it’ll ask you to confirm whether you really want to remove a file
before going ahead and doing it.
rmdir
rmdir allows you to remove or delete directories but not their contents. A directory
must be empty in order to remove it using this command.
If you wish to remove a directory and all its contents, you should use rm -r.
su 33
WINDOWS AND
LINUX LAB
su stands for superuser (a privileged user), and can be used to log in as another user. If
no user is specified and you know the appropriate password, su can be used to log in
as a superuser.
tail
The tail command will print the last ten lines of a file. tail is often used with the
option -f, which tells tail not to quit at the end of file and instead follow the file as it
grows.
telnet
You can communicate with other computers by using the telnet protocol. The host
must be a name or an Internet address. telnet has two modes: the command mode,
which is indicated by the telnet > prompt, and an input mode which is usually a
session where you would log on to the host system. The default mode is command
mode, so if no host is given it will automatically go into this mode. If you need help
while in the command mode, type? or help.
who
The who command prints out information about the most recent status of the system.
If no options are listed, then all of the usernames currently logged onto the system are
displayed.
The option am i will print the name of the current user. The -u option will display
how long the terminal has been idle.
Bash
Bash, or the Bourne-Again Shell, is by far the most widely used choice and it comes
installed as the default shell in the most popular Linux distributions. It was developed
from the original UNIX Bourne shell (also known as sh) and was designed to be fully
compatible with the old scripts, while adding multiple improved features.
Bash is a very solid shell option, since it has been used for a long time and there is
ample documentation for it. In fact, most tutorials will assume that you’re using bash.
As a result, it is recommended for most users and works great for most common
system administration tasks. However, if you need more powerful scripting options or
other advanced tools, it is time to explore some of the newer shells available.
Zsh
Zsh or the Z-shell was designed from the onset to be interactive and incorporate some
of the best features of older shells. It provides unique scripting features; it is highly
customizable and is easy to use, with spelling correction, command completion or
filename globbing.
Korn
KornShell (also known as ksh) is a very old bash alternative that has been developed
in the 1980s. It is very similar to bash but doubles as a complete and powerful
programming language, so it has a number of passionate fans among sysadmins. It is
not widely used, so it’s a bit more difficult to find online documentation or help.
Tcsh
Tcsh is a better version of the C shell (csh), which was developed in the UNIX era. It
is favored by programmers because its syntax is very similar to the C programming
language, so they can use its scripting features without having to learn bash. It is also
the default shell in operating systems from the BSD family. It offers several other
useful features, such as job control, a command-line editor or a configurable
command-line completion tool. Tcsh is installed with yum from the standard
repositories.
Fish
Fish, or the friendly interactive shell, aims to be simpler to use and more user
friendly than its competitors. It is a great choice for Linux beginners, because it uses
colors to help the user. For example, commands with incorrect syntax are displayed in
red, while correct ones are blue. In addition, fish provides very useful auto-complete
suggestions and even parses the man pages of any new installed package and suggests
command completions based on them.
Bash is the GNU Project's shell—the Bourne Again SHell. This is an sh-compatible
shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh).
It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools
standard. It offers functional improvements over sh for both programming and
interactive use. In addition, most sh scripts can be run by Bash without modification.
The improvements offered by Bash include:
command-line editing
unlimited size command history
job control
shell functions and aliases
indexed arrays of unlimited size
integer arithmetic in any base from two to sixty-four.
A shell script is a plain-text file that contains shell commands. It can be executed by
typing its name into a shell, or by placing its name in another shell script.
#!/bin/bash
If this example doesn't work, you will need to find out where your
Bash shell executable is located and substitute that location in the
above example. Here is one way to find out:
$ whereis bash
A shell script file may optionally have an identifying suffix, like ".sh". This
only helps the user remember which files are which. The command processor
responsible for executing the file uses the executable bit, plus the file's first
line, to decide how to handle a shell script file.
$ ./scriptname.sh
This special entry is a way to tell the command processor that the desired
36 script is located in the current directory. Always remember: if you cannot get
LINUX
your shell script to run, remember this trick to provide its location as well as
its name.
This will get you past the details of writing and launching a simple script.
1. Choose a text editor you want to use. It can be a command-line editor
like emacs, pico or vi, or an X Windows editor if you have this
option.
2. Run your choice of editor and type the following lines:
#!/bin/bash
echo "Hello, world"
$ chmod +x myscript.sh
$ ./myscript.sh
Hello, world
Echo Command
You can use echo command with various options. Some useful options are mentioned
in the following example. When you use ‘echo’ command without any option then a
newline is added by default. ‘-n’ option is used to print any text without new line
and ‘-e’ option is used to remove backslash characters from the output.
Create a new bash file with a name, ‘ec_example.sh’ and add the following script.
#!/bin/bash
echo "Printing text with newline"
echo –n "Printing text without newline"
echo –e "\nRemoving \t backslash \t characters\n"
‘#’ symbol is used to add single line comment in bash script. Create a new file named
‘com_example.sh’ and add the following script with single line comment.
#!/bin/bash
#Add two numeric values
((sum=30+25))
While Loop
Create a bash file with the name, ‘wh_example.sh’, to know the use of while loop. In
the example, while loop will iterate for 5 times. The value of count variable will
increment by 1 in each step. When the value of count variable will 5 then
the while loop will terminate.
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
For Loop
The basic for loop declaration is shown in the following example. Create a file named
‘for_example.sh’ and add the following script using for loop. Here, for loop will
iterate for 10 times and print all values of the variable, counter in single line.
#!/bin/bash
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"
User Input
‘read’ command is used to take input from user in bash. Create a file named
‘user_input.sh’ and add the following script for taking input from the user. Here, one
string value will be taken from the user and display the value by combining other
string value.
#!/bin/bash
echo "Enter Your Name"
read name
echo "Welcome $name to LinuxHint"
You can use if condition with single or multiple conditions. Starting and ending block
of this statement is define by ‘if’ and ‘fi’. Create a file named ‘simple_if.sh’ with the
following script to know the use if statement in bash. Here, 10 is assigned to the
variable, n. if the value of $n is less than 10 then the output will be “It is a one digit
number”, otherwise the output will be “It is a two digit number”. For comparison, ‘-
lt’ is used here. For comparison, you can also use ‘-eq’ for equality, ‘-ne’ for not
equality and ‘-gt’ for greater than in bash script.
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
Different types of logical conditions can be used in if statement with two or more
conditions. How you can define multiple conditions in if statement using AND logic is
shown in the following example. ‘&&’ is used to apply AND logic of if statement.
Create a file named ‘if_with_AND.sh’ to check the following code. Here, the value
of username and password variables will be taken from the user and compared with
‘admin’ and ‘secret’. If both values match then the output will be “valid user”,
otherwise the output will be “invalid user”.
!/bin/bash
If statement with OR
39
WINDOWS AND
LINUX LAB
#!/bin/bash
if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi
Else if Statement
The use of else if condition is little different in bash than other programming language.
‘elif’ is used to define else if condition in bash. Create a file named, ‘elif_example.sh’
and add the following script to check how else if is defined in bash script.
#!/bin/bash
if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"
else
echo "Sorry, try for the next time"
fi
Case Statement
Case statement is used as the alternative of if-elseif-else statement. The starting and
ending block of this statement is defined by ‘case’ and ‘esac’. Create a new file named,
‘cs_example.sh’ and add the following script. The output of the following script will
be same to the previous else if example.
#!/bin/bash
Bash script can read input from command line argument like other programming
language. For example, $1 and $2 variable are used to read first and second command
line arguments. Create a file named “com_line.sh” and add the following script. Two
argument values read by the following script and prints the total number of arguments
and the argument values as output.
#!/bin/bash
echo "Total arguments : $#"
echo "1st Argument = $1"
echo "2nd argument = $2"
How you can read command line arguments with names is shown in the following
script. Create a file named, ‘cmd_line_names.sh’ and add the following code. Here,
two arguments, X and Y are read by this script and print the sum of X and Y.
#!/bin/bash
for arg in "$@"
do
index=$(echo $arg | cut -f1 -d=)
val=$(echo $arg | cut -f2 -d=)
case $index in
X) x=$val;;
Y) y=$val;;
*)
esac
done
((result=x+y))
echo "X+Y=$result"
41
WINDOWS AND
LINUX LAB
String Concatenation
You can easily combine string variables in bash. Create a file named “st_combine.sh”
and add the following script to check how you can combine string variables in bash by
placing variables together or using ‘+’ operator.
#!/bin/bash
string1="IGNOU"
string2="PGDCA"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good programme"
echo $string3
Like other programming language, bash has no built-in function to cut value from any
string data. But you can do the task of substring in another way in bash that is shown
in the following script. To test the script, create a file named ‘subst_example.sh’ with
the following code. Here, the value, 6 indicates the starting point from where the
substring will start and 5 indicates the length of the substring.
#!/bin/bash
Str="Learn Linux from IGNOU"
subStr=${Str:6:5}
echo $subStr
You can send email by using ‘mail’ or ‘sendmail’ command. Before using these
commands, you have to install all necessary packages. Create a file named,
‘email_example.sh’ and add the following code to send the email.
#!/bin/bash
Recipient=”[email protected]”
Subject=”Greeting”
Message=”Welcome to IGNOUs PGDCA Programme”
‘mail -s $Subject $Recipient <<< $Message’
You may seek assistance in doing the lab exercises from the concerned lab
instructor. Since the assignments have credits, the lab instructor is obviously not
expected to tell you how to solve these, but you may ask questions concerning the
Operating system and C programs.
For each C program you should add comments (i.e. text between /* ... */
delimiters) above each function in the code, including the main function. This
should also include a description of the function written, the purpose of the
function, meaning of the argument used in the function and the meaning of the
return value (if any). These descriptions should be placed in the comment block
immediately above the relevant function source code.
The comment block above the main function should describe the purpose of the
program. Proper comments are to be provide where and when necessary in the
programming.
The program written for the problem given should conform to the ANSI standard
for the C language.
The program should be interactive, general and properly documented with real
Input/ Output data.
If two or more submissions from different students appear to be of the same origin
(i.e. are variants of essentially the same program), none of them will be counted.
You are strongly advised not to copy somebody else's work.
As soon as you have finished a lab exercise, contact one of the lab instructor /
incharge in order to get the exercise evaluated and also get the signature from
him/her on the Observation book.
The total no. of lab sessions (3 hours each) are 10 and the list of assignments is
provided session-wise. It is important to observe the deadline given for each
assignment.
12) Use the man command to obtain further information on the finger command.
13) List all the processes that are presently running.
14) List the text files in your current directory.
15) Make a copy of any text file.
16) Rename one of your text files in the current directory.
17) Delete an unneeded copy of a file.
18) Print out any file on paper.
19) Send a message to another user on your LINUX system, and get them to reply.
20) Create a small text file and send it to another user.
Session 3
21) When you receive a message, save it to a file other than your mailbox.
22) Send a message to a user on a different computer system.
23) Try to move to the home directory of someone else in your group. There are
several ways to do this, and you may find that you are not permitted to enter
certain directories. See what files they have, and what the file permissions are.
24) Try to copy a file from another user’s directory to your own.
25) Set permissions on all of your files and directories to those that you want. You
may want to give read permission on some of your files and directories to
members of your group.
26) Create a number of hierarchically related directories and navigate through them
using a combination of absolute pathnames (starting with "/") and relative
pathnames.
27) Try using wildcards (“*” and possibly “?”).
28) Put a listing of the files in your directory into a file called filelist. (Then delete
it!)
44
LINUX
29) Create a text file containing a short story, and then use the spell program to
check the spelling of the words in the file.
30) Redirect the output of the spell program to a file called errors.
Session 4
31) Type the command ls -l and examine the format of the output. Pipe the output of
the command ls -l to the word count program wc to obtain a count of the
number of files in your directory.
32) Use cut to strip away the reference material and leave just the text field.
33) Use tr to strip away any tags that are actually in the text (e.g., attached to the
words), so that you are left with just the words.
34) Set a file to be read-only with the chmod (from change mode) command.
Interpret the file permissions displayed by the ls -l command.
35) Delete one or more directories with the rmdir (from remove directory)
command. See what happens if the directory is not empty. Experiment
(carefully!) with the rm -r command to delete a directory and its content.
36) Experiment with redirecting command output (e.g., ls -l >file1). Try ">> "
instead of " >" with an existing text file as the output.
37) See whether upper-case versions of any of these commands work as well as the
lower-case versions.
38) Use the who command to see users logged into the system.
39) Pipe the output of the who command to the sort command
40) Search for your login name in whofile using the grep command.
Session 5
a) Write an awk command to print the lines and line number in the given
input file.
b) Write an awk command to print first field and second field only if third
field value is >=25 in the given input file.
(Hint: input field separator is “:” and output field separator is “,”).
c) Consider the marks.txt is a file that contains one record per
line( comma separate fields) of the student data in the form of SRollNo,
45
WINDOWS AND
LINUX LAB SName, Hindi marks, English marks, Maths Marks, Science marks,
Social Marks. Write an awk script to generate result for every students
in the form of SRollNo, SName, Total Marks, Average and Grade.
Grade is PASS if marks is >=30 in Hindi and English respectively, and
if marks >= 40 in other subjects. Result is fail otherwise.
d) Write an awk program to print the fields 1 and 4 of a file that is passed
as command line argument. The file contains lines of information that
is separated by “,” as delimiter. The awk program must print at the end
the total and average of all 3rd field data.
e) Write an awk program to demonstrate user defined functions and
system command.
f) Write an awk script to count the number of lines in a file that do not
contain vowels.
g) Write an awk script to find the number of characters, words and lines in
a file.
Session 6
43) Use the grep command to search the file example1 for occurrences of the string
“water”.
44) Write grep commands to do the following activities:
a) Write a grep command that selects the lines from the file1 that have
exactly three characters
b) Write a grep command that selects the lines from the file1 that have at
least three characters.
c) Write a grep command that selects the lines from the file1 that have
three or fewer characters
d) Write a grep command that count the number blank lines in the file1
e) Write a grep command that count the number nonblank lines in the
file1
f) Write a grep command that selects the lines from the file1 that have the
string LINUX.
g) Write a grep command that selects the lines from the file1 that have
only the string LINUN.
h) Write a grep command that copy the file to the monitor, but delete the
blank lines.
i) Write a grep command that selects the lines from the file1 that have at
least two digits without any other characters in between
j) Write a grep command that selects the lines from the file1 that do not
start with A to G
45) Make a sorted wordlist from the file.
Session 7
Note: The basic salary is entered interactively through the key board.
54) Write a shell script that accepts a file name, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
55) Write a shell script that deletes all lines containing a specified word in one or
more files supplied as arguments to it.
56) Write a shell script to encrypt any text file.
Session 8
57) Write a shell script to translate all the characters to lower case in a given text
file.
58) Write a shell script to combine any three text files into a single file (append
them in the order as they appear in the arguments) and display the word count.
59) Write a shell script that, given a file name as the argument will write the even
numbered line to a file with name evenfile and odd numbered lines to a file
called oddfile.
60) Write a shell script which deletes all the even numbered lines in a text file.
61) Write a script that will count the number of files in each of your subdirectories.
62) Write a shell script like a more command. It asks the user name, the name of the
file on command prompt and displays only the 15 lines of the file at a time on
the screen. Further, next 15 lines will be displayed only when the user presses
the enter key / any other key.
63) Write a shell script that counts English language articles (a, an, the) in a given
text file.
64) Write the shell script which will replace each occurrence of character c with the
characters chr in a string s. It should also display the number of replacements.
Session 9
65) Write a shell script which reads the contents in a text file and removes all the
blank spaces in them and redirects the output to a file.
66) Write a shell script that accepts a list of file names as its arguments,
counts and reports the occurrence of each word that is present in the first
argument file on other argument files.
67) Write a shell script to list all of the directory files in a directory.
68) Write a shell script to implement menu driven program to display list of
users who are currently working in the system, copying files (cp
command), rename a file, list of files in the directory and quit
option.( Hint: use case structure)
47
WINDOWS AND
LINUX LAB 69) Write a shell program to simulate ‘cat’ command
2.11 SUMMARY
LINUX is a popular operating System, which is mostly using the C language, making
it easy to port to different configurations. LINUX programming environment is
unusually rich and productive. It provides features that allow complex programs to be
built from simpler programs.
LINUX uses a hierarchical file system that allows easy maintenance and efficient
implementation. It uses a consistent format for files, the byte stream, making
application programs easier to write. It is a multi-user, multitasking system. Each user
can execute several processes simultaneously. It hides the machine architecture from
the user, making it easier to write programs that run on different hardware
implementation. It is highly secured system.
1) www.linux.org/
2) www.kernel.org/
3) www.linux.com/
4) www.unix.org/
5) unixhelp.ed.ac.uk
48 6) www.unix.org/resources.html
LINUX
7) www.osnews.com/
8) www.levenez.com/unix/
9) cnc.k12.mi.us/websites/bsdtree.html/
LINUX
1) http://copy.sh/v86/?profile=linux26
2) https://www.webminal.org/
3) https://www.tutorialspoint.com/codingground.htm
4) https://www.tutorialspoint.com/execute_bash_online.php
5) https://www.masswerk.at/jsuix/index.html
6) http://cb.vu/
C Compilers / IDEs
1) https://www.onlinegdb.com/online_c_compiler
2) https://www.programiz.com/c-programming/online-compiler/
3) https://www.codechef.com/ide
4) http://codeblocks.org/
5) https://www.jdoodle.com/c-online-compiler/
6) http://codelite.org/
7) https://www.onworks.net/programs/ubuntu-emulator-online
Spoken Tutorials
1) https://spoken-tutorial.org/tutorial-
search/?search_foss=Linux&search_language=English
2) https://spoken-tutorial.org/tutorial-
search/?search_foss=Linux+AWK&search_language=English
3) https://spoken-tutorial.org/tutorial-
search/?search_foss=BASH&search_language=English
4) https://spoken-tutorial.org/tutorial-search/?search_foss=Linux+for+Sys-
Ads&search_language=English
5) https://spoken-tutorial.org/tutorial-
search/?search_foss=Advance+C&search_language=English
49