OS Lab 03
OS Lab 03
Lab 03
Shell Programming
Lab Objectives:
Date:
Name:
Reg#: Group:
Marks: Signature:
Page 1 of 8
UNIVERSITY OF CHAKWAL
DEPARTMENT OF COMPUTER SCIENCE
& INFORMATION TECHNOLOGY
(Operating System)
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)
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
$ touch File1
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.
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)
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