0% found this document useful (0 votes)
12 views56 pages

Lab 1

This document provides an overview of command line interface basics, including directory trees, file paths, and terminal commands. It explains the structure of command lines, common commands like pwd, ls, cd, mkdir, and permissions management using chmod. Additionally, it covers operators in programming, including types and precedence, along with conditional statements and logical operators in C programming.

Uploaded by

Harshit Raj
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)
12 views56 pages

Lab 1

This document provides an overview of command line interface basics, including directory trees, file paths, and terminal commands. It explains the structure of command lines, common commands like pwd, ls, cd, mkdir, and permissions management using chmod. Additionally, it covers operators in programming, including types and precedence, along with conditional statements and logical operators in C programming.

Uploaded by

Harshit Raj
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/ 56

Command Line Interface Basics

Lab 1 - CS F111 (Computer Programming)

Arnav Adivi, Nilay Borkar, Aditya Birla


Preliminary: Directory trees
Let’s explore a new, more formal way of thinking about how your computer stores files in folders (directories). To
visualise this, we use a special kind of graph called a tree.

A tree is made up of labelled points called nodes or vertices (shown in


green in the figure at the left) and lines connecting some of them, called
edges.

A tree has a single uppermost vertex called its root node.

The root node is connected to one or more nodes in the level


immediately below it, called its children. Each of these children may
have its own children. These children may also have their own children,
and so on.

Image courtesy: https://www.geeksforgeeks.org/binary-tree-data-structure/ Nodes without children are called leaf nodes or leaves.
Preliminary: Directory trees

Now consider a tree where every node


represents a directory (folder) or file in your
storage, and every child of a directory is
contained in it.
This kind of a tree is called a directory tree.
The root node of this tree is called the root
directory.
What do you observe?
What do you observe?
Preliminary: Directory trees
● Every directory or file except the
root directory has a unique
parent
● But not every directory has a
unique child
● Every file is a leaf node
● But not every leaf node is a file!
● Most importantly, for every file
or directory there is a unique
path starting from the root
directory and ending at it.
Preliminary: File Paths
This unique path starting at the
root node and ending at the
file/directory is called its
absolute file path.

The root directory is common


for all files on your disk, and its
name depends on the
operating system.

The absolute file path is then


expressed in the form

root/directory1/directory2/…/fil
ename

You might have come across these on your computer in the form
C:\Users\Person1\ilovecp.txt

The root directory for Windows is in the form drive:\ but for Linux it is simply /
Preliminary: File Paths
But sometimes when you
are already in a directory,
you might want the path Absolute filepath of
from the current directory Handout.pdf
to the file you are looking
for. This is called the
relative file path of the
file to the current
directory.
Relative filepath of Handout.pdf
to Courses

Why the period?


Preliminary: . .. and ~
In a file path, a single period (.) refers to the current directory. For example,
when you are in Courses, . refers to Courses, ./CP refers to its child CP,
./CP/Handout.pdf refers to CP’s child Handout.pdf, and so on.

A double period (..) refers to the parent directory of the current directory. For
example, when your current directory is Courses, .. refers to My_second_sem,
../Clubs refers to My_second_sem/Clubs, and so on.

A tilde (~) refers to the user’s home directory.

In Linux, the root directory is given by /


Terminal, Shell and Kernel
A terminal application (or console) is a program you use to interact with your
computer using a text-based interface.

This is done using text-based inputs called command lines that achieve some
specific task.

An active terminal running on a computer


Terminal, Shell and Kernel
The terminal itself does not know what to do with the command lines you
enter!

It passes them to another program called a shell, which ‘translates’ these


command lines into a form that can be understood by yet another program
called the kernel.

The kernel then works closely with your computer’s hardware to perform the
specific task specified by the command line.

For this session you only need to be concerned with the terminal.
The structure of a command line
When you first open a new terminal, it gives you a command prompt, a piece
of text usually ending with a $, > or % (depending on your shell) that indicates
it is ready to accept a new command line. A prompt is also given after every
subsequent command line is executed.
Recall that a tilde (~) refers to the
logged in user’s home directory.
The tilde in the image shows that
your current directory is the
home directory. Your command
Command Prompt
goes here
The structure of a command line
Every command line follows a standard structure.

$ command --flag1 --flag2 . . . -opt1 optarg -opt optarg . . . arg1 arg2 . . .

flags and options are used to modify the An argument is


A command specifies
behaviour of the command. an input taken by
the broad task you
flags are preceded by a double hyphen -- the command
want to do.
options are preceded by a single hyphen - and
sometimes take arguments (input values) that
must be placed immediately after, separated by
a space

To autocomplete the command line, use the Tab key.


To return to the start of the command line, use the Home key.
Let’s get started
For this course, we’ll be working in the terminal that’s included in VSCode.
No need to worry - this works just like the system terminal, but it directly opens in
your working folder and saves the effort of having to navigate all the way to it
through a series of commands.
To start, use VSCode ‘Open Folder…’ menu item to open the folder where you’ll be
adding files.
Then, go to the Menu Bar > Terminal > Create New Terminal to open a new terminal.
You will be greeted with a command prompt.
Let’s get familiar with a few common terminal commands.
Commands: pwd
pwd (short for print working directory) prints the absolute file path to the
current directory. For example, entering pwd immediately after opening
My_second_sem in VSCode prints the absolute file path to My_second_sem
on the terminal.
Commands: ls
The ls command lists all the items in the working directory.

The ls command can also be used to list the items in a directory other than the working directory.
For this, you need to also provide the relative path of the other directory to the working directory as
an argument (input) separated by a whitespace.
For example, the relative path of Courses to My_second_sem is ./Courses. Using ls ./Courses,
you can list the items in Courses while still being in My_second_sem!
Commands: cd
Sometimes, you might want to change the working directory itself. For this, you
need to use the cd command. The argument is the relative path of the directory
you want to change to. For example, here, we will change the directory from
My_second_sem to Clubs.

Here the . refers to the current directory (My_second_sem).


What if you want to go ‘out’ of the working (current) directory? To do this, you need to go
to the parent directory of the working directory. Recall that a double period .. refers to
the parent directory.
Commands: mkdir
To make a new directory inside the working directory, use the mkdir (make
directory) command. The argument is the name of the new directory.

There is a problem: When there is a space in the name of the directory, the two words
separated by the space are read as separate arguments.

To fix this precede every space with a backslash or enclose the name in double
quotes
Commands: rmdir
The rmdir command (short for remove directory) is used to remove an
empty directory from the working directory. The name of the directory to be
removed is the argument.
Commands: touch, cat and rm
To make a new file inside the working directory, use the touch command. The
argument is the name of the new file with its extension.
To view the file, use the cat command (short for concatenate) followed by the
complete filename.
To remove the file, use the rm command (short for remove) followed by the complete
filename. Be very careful while using rm!
Commands: chmod
chmod is a command that lets you change the permissions of a file or directory to all
types of users.
Here’s the syntax of the chmod command:
chmod <Operations> <File/Directory Name>
You can grant or revoke the permission by replacing the Operations in the above
command.
These operations need to be preceded with a '+' or '-' operator.

'+' indicates adding a new permission,

'-' indicates removing an existing permission.


Permissions
● u – Grant permission to a user
● g – Grant permission to a group (A Group of users)
● o – Grant permission to others (who do not come under either of the above).
● r – Grants read permission
● w – Grant write permission
● x – Grant execute permission

Here's an example: chmod +r sample.txt

The above command adds read permission for the sample.txt file.
Commands: gcc
GCC, the GNU Compiler Collection, works together with a few other programs to compile the C
source code file that you can understand into an executable file (Windows - .exe; Linux, macOS -
.out) specific to your computer’s architecture.

The name of the executable file can be chosen by adding the option -o followed by a complete
name for the file.

To run an executable, use ./ followed by its name (no spaces).


Some examples of flags/options
- We have seen ls already. Now try
running ls -l on your terminal. It is used
to display a detailed listing of the files
and directories in the current directory.
- rm -r will recursively delete a directory
and all its contents (normally rm will not
delete directories, while rmdir will only
delete empty directories)
- When you run the command ls --help,
it displays a help message with details
about how to use the ls command and
its various options.
Practice exercise 😎
Without changing your working
directory from CP:

1. create a new directory ‘My


Vertical’ in Clubs
2. create a file in My Vertical
called ‘documentation.txt’

touch ../../Clubs/Club1/My\ Vertical/documentation.txt 1.


mkdir ../../Clubs/Club1/”My Vertical”
OR
mkdir ../../Clubs/Club1/My\ Vertical 1.
Operators
Lab 01 - CS F111 (Computer Programming)
Operators
An operator is a character or symbol that instructs your computer to perform
some actions on one or more values. These values are called operands.

For example, the + operator is used to add two numbers.


Types of Operators
● Unary operators
● Arithmetic operators
● Relational operators
● Assignment operators
● Logical operators (will be covered later)
● Bitwise operators (will be covered later)
● Ternary conditional operator (will be covered later)
Unary Operator
A unary operator is an operator with only one operand.

Common examples of unary operators are:

● Increment operator ‘++’: - increases the value of its operand by one.


● Decrement operator ‘--’: decreases the value of its operand by one.
● Logical NOT operator ‘!’: flips the truth value of its operand (changes true
to false and vice-versa)

For example, !false evaluates to true.


Prefix and postfix increment/decrement
The pre-increment and pre-decrement operators increment/decrement the operand by
1, and the value of the expression is the incremented/decremented value.

The post-increment and post-decrement operators increment/decrement the operand


by 1, and the value of the expression is the original value of the operand.
Arithmetic Operator
Arithmetic operators are used to perform mathematical operations on
numeric values.

The main arithmetic operators are the addition (+), subtraction (-),
multiplication (*), division (/) and modulo (%) operators.

Note: a % b is the remainder of the division of a by b. In C, the remainder is


signed and has the same sign as the dividend (a).
Relational Operator
Relational operators check some relationship between two expressions and
they evaluate to true or false.
Some relational operators include:
● Equality ‘==’: Checks if the values of two operands are equal (evaluates to
true if they are)
● Not equal to ‘!=’: Checks if the values of two operands are not equal.
● Inequalities: <, <=, >, >=
Assignment Operator
The assignment operator = is used to copy values into variables.

There are other assignment operators known as compound assignments.


These are generally used to replace statements where the expression being
assigned to a variable contains that variable.
Operator Precedence
Operator precedence determines the way in which operations are performed
in C. More specifically, it deals with the order in which operations in an
expression are evaluated.
Associativity in Operator Precedence
Associativity is a property that determines the order in which operations are
performed when multiple operators of the same precedence appear in an
expression.
Parentheses in Operator Precedence
Since parentheses have the highest precedence, we can use them to
control the order in which operators are evaluated.
What is the output?
Answer
Answer
A new sort of problem
Given an integer n, write a program that determines whether it is odd or even.
Specifically, the program should print “n is odd” if n is odd, and “n is even” if n
is even.
A flowchart representing a program to determine if a number is even or odd
Conditional Statements
*in C, else goes with a
small case ‘e’ as in ‘else’
EXAMPLES

Here both statements


will be executed if and
only if x<=10
Here only the 1st
statement is
conditional and
depends on the if
statement

The 2nd statement is


executed always
Common mistake of
using ‘=’ instead of ‘==’ as
the conditional
statement

The assignment
statement x=2 is always
true, so the statement
y=5 will always be
executed
Understanding
nested if else
statements
EXAMPLE

What if I want to use multiple if else statements instead of nested if else statements?
Use of logical operators in if-else statements
1. LOGICAL AND (&&)
OPERATOR

if (condition-1 && condition-2)


{
statement-1;
}
else
{ If and only if both the conditions,
statement-2; i.e.
} Condition-1 and Condition-2
are true

Statement-1 will be executed


else statement-2 will be
executed
Use of logical operators in if-else statements
2. LOGICAL OR (||)
OPERATOR

if (condition-1 || condition-2)
{
statement-1;
}
else
If either one of the conditions, i.e.
{
statement-2; Condition-1 and Condition-2 is
} true

Statement-1 will be executed

When both conditions are false


statement-2 will be executed
If else if Ladder

Working of if else if ladder


Here’s the implementation

Using logical operators


and multiple if-else
statements to find
maximum of 3 numbers
Another Method to ponder upon

Using a max variable and


altering it every time
another variable is greater
than it

You might also like