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

OS Lab 03

The document outlines a lab exercise for students in the Computer Science and Information Technology department at the University of Chakwal, focusing on basic Linux shell programming commands. It covers essential tasks such as logging in, creating, displaying, copying, renaming, and deleting files, as well as using command history and filename completion. The lab aims to provide students with a foundational understanding of the Linux environment and its command-line interface.

Uploaded by

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

OS Lab 03

The document outlines a lab exercise for students in the Computer Science and Information Technology department at the University of Chakwal, focusing on basic Linux shell programming commands. It covers essential tasks such as logging in, creating, displaying, copying, renaming, and deleting files, as well as using command history and filename completion. The lab aims to provide students with a foundational understanding of the Linux environment and its command-line interface.

Uploaded by

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

UNIVERSITY OF CHAKWAL

DEPARTMENT OF COMPUTER SCIENCE


& INFORMATION TECHNOLOGY
(Operating System)

Lab 03

Shell Programming

Lab Objectives:

 To introduces few of the basic commands of Linux


Date:
Name:
Reg#: Group:
Marks: Signature:

Page 1 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

Getting Started with Linux


The login prompt may be graphical or simple text
If text, logging in will present a shell
If graphical, logging in will present a desktop
o Some combination of mouse and keystrokes will make a terminal
window appear. Press .Ctrl+Alt+T. This will launch the Terminal.
o A shell runs in the terminal window
Linux Command Line
The shell is where commands are invoked
A command is typed at a shell prompt
o Prompt usually ends in a dollar sign ($)
After typing a command press Enter to invoke it
o The shell will try to obey the command
o Another prompt will appear
Example:
$ date
Sat March 01 11:59:05 BST 2008
$
o The dollar represents the prompt in this course, do not type it
Logging Out
To exit from the shell, use the exit command
Pressing Ctrl+D at the shell prompt will also quit the shell
o Quitting all programs should log you out
o If in a text-only single-shell environment, exiting the shell should be
sufficient
In a window environment, the window manager should have a log out command
for this purpose
After logging out, a new login prompt should be displayed

Page 2 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

Command Syntax
Most commands take parameters
o Some commands require them
o Parameters are also known as arguments
o For example, echo simply displays its arguments:
$ echo
$ echo Hello there
Hello there
Commands are case-sensitive
o Usually lower-case
$ echo whisper
whisper
$ ECHO SHOUT
bash: ECHO: command not found
Files
Data can be stored in a file
Each file has a filename
o A label referring to a particular file
o Permitted characters include letters, digits, hyphens (-), underscores (_),
and dots (.)
o Case-sensitive — NewsCrew.mov is a different file from NewScrew.mov
The ls command lists the names of file in current directory.

Page 3 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

Creating Files with cat

There are many ways of creating a file


1: Cat Command:
One of the simplest is with the cat command:
$ cat > shopping_list
cucumber
bread
fish fingers

Note the greater-than sign (>) — this is necessary to create the file
The text typed is written to a file with the specified name
Press Ctrl+D after a line-break to denote the end of the file
o The next shell prompt is displayed
ls demonstrates the existence of the new file

2: Touch Command

You can create empty file by using following command

$ touch File1

To create multiple empty files by using following


command

$ touch File2 File3 File4

3: Nano Command

If you plan to make some edits to the file right away, then you can create and open the
file in an editor with just command nano. The following command will create a new
file and then open it up so you can make edits.

$ nano Test.txt
To exit and save the file, start by pressing Ctrl + X on your keyboard. This will attempt
to exit nano. To remember this keyboard combination, check the bottom of the nano
menu. ^X just means Ctrl + X. You’ll now be asked if you want to save your changes to

Page 4 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

the file. You need to respond by entering either “Y” or “N”. Since we are trying to save
our changes, press “Y” and hit enter.

Displaying Files’ Contents with cat


There are many ways of viewing the contents of a file
One of the simplest is with the cat command:
$ cat shopping_list
cucumber
bread
fish fingers
Note that no greater-than sign is used
The text in the file is displayed immediately:
o Starting on the line after the command
o Before the next shell prompt

Deleting Files with rm

To delete a file, use the rm (‘remove’) command


Simply pass the name of the file to be deleted as an argument:
$ rm shopping_list
The file and its contents are removed
o There is no recycle bin
o There is no ‘unrm’ command
The ls command can be used to confirm the deletion

UNIX Command Feedback


Typically, successful commands do not give any output
Messages are displayed in the case of errors
The rm command is typical
o If it manages to delete the specified file, it does so silently
o There is no ‘File shopping_list has been removed’ message
o But if the command fails for whatever reason, a message is displayed
It is standard behavior, and doesn’t take long to get used to
Page 5 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

Copying and Renaming Files with cp and mv


To copy the contents of a file into another file, use the cp command:
$ cp CV.pdf old-CV.pdf
To rename a file use the mv (‘move’) command:
$ mv commitee_minutes.txt committee_minutes.txt
o Similar to using cp then rm
For both commands, the existing name is specified as the first argument and the
new name as the second
o If a file with the new name already exists, it is overwritten

Filename Completion
The shell can making typing filenames easier
Once an unambiguous prefix has been typed, pressing Tab will automatically
‘type’ the rest
For example, after typing this:
$ rm sho
Pressing Tab may turn it into this:
$ rm shopping_list
This also works with command names
o For example, da may be completed to date if no other commands start ‘da’

Command History
Often it is desired to repeat a previously-executed command
The shell keeps a command history for this purpose
o Use the Up and Down cursor keys to scroll through the list of previous
commands
o Press Enter to execute the displayed command
Commands can also be edited before being run
o Particularly useful for fixing a typo in the previous command
o The Left and Right cursor keys navigate across a command
o Extra characters can be typed at any point
o Backspace deletes characters to the left of the cursor
Page 6 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

o Del and Ctrl+D delete characters to the right


o Take care not to log out by holding down Ctrl+D too long

Skills Developed
By completing the lab, one should have basic understanding of Linuxenvironment and
few Linux commands.

Lab Exercises

Question 1
a. Log in. Open a terminal window, to start a shell.
b. Exit from the shell; the terminal window will close.
c. Start another shell. Enter each of the following commands in turn.
i. date
ii. whoami
iii. hostname
iv. uname
v. uptime
Question 2
a. Use the ls command to see if you have any files.
b. Create a new file using the cat command as follows:
$ cat > hello.txt
Hello world!
This is a text file.
c. Press Enter at the end of the last line, then Ctrl+D to denote the end of the file.
d. Use ls again to verify that the new file exists.
e. Display the contents of the file.
f. Display the file again, but use the cursor keys to execute the same command again
without having to retype it.

Page 7 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)

Question 3
a. Create a second file. Call it secret-of-the-universe, and put in whatever content
you deem appropriate.
b. Check its creation with ls.
c. Display the contents of this file. Minimize the typing needed to do this:
i. Scroll back through the command history to the command you used to
create the file.
ii. Change that command to display secret-of-the-universe instead of creating
it.
Question 4
After each of the following steps, use ls and cat to verify what has happened.
a. Copy secret-of-the-universe to a new file called answer.txt. Use Tab to avoid
typing the existing file’s name in full.
b. Now copy hello.txt to answer.txt. What’s happened now?
c. Delete the original file, hello.txt.
d. Rename answer.txt to message.
e. Try asking rm to delete a file called missing. What happens?
f. Try copying secret-of-the-universe again, but don’t specify a filename to which to
copy. What happens now?

Page 8 of 8

You might also like