Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
152 views

1 Introduction Module 1

Here are the key points about the echo command: - The echo command displays a line of text on the terminal. It is commonly used in shell scripts. - Syntax: echo [option] [string] - Without any options, it simply displays the string. - Common options: - -e: Enable interpretation of backslash escapes like \n, \t etc. - \n: Inserts a new line - \t: Inserts a tab - \b: Removes spaces - \c: Suppresses the trailing newline - Backslash escapes allow formatting the output, like inserting new lines, tabs, removing spaces etc. - Echo is very useful for simple text
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

1 Introduction Module 1

Here are the key points about the echo command: - The echo command displays a line of text on the terminal. It is commonly used in shell scripts. - Syntax: echo [option] [string] - Without any options, it simply displays the string. - Common options: - -e: Enable interpretation of backslash escapes like \n, \t etc. - \n: Inserts a new line - \t: Inserts a tab - \b: Removes spaces - \c: Suppresses the trailing newline - Backslash escapes allow formatting the output, like inserting new lines, tabs, removing spaces etc. - Echo is very useful for simple text
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

UNIX PROGRAMMING

18CS56
Text Books
Textbooks:
1. Sumitabha Das., Unix Concepts and Applications., 4thEdition., Tata
McGraw Hill ( Chapter 1,2,3,4,5,6,8,13,14)
2. W. Richard Stevens: Advanced Programming in the UNIX Environment,
2nd Edition, Pearson Education, 2005 ( Chapter 3,7,8,10,13,15)
3. Unix System Programming Using C++ - Terrence Chan, PHI, 1999.
( Chapter 7,8,9,10)

Reference Books:
1. M.G. Venkatesh Murthy: UNIX & Shell Programming, Pearson Education.
2. Richard Blum , Christine Bresnahan : Linux Command Line and Shell
Scripting Bible,2ndEdition, Wiley,2014.

2
Introduction
In this session Students will learn
Unix Components/Architecture.
Features of Unix.
The UNIX Environment and UNIX Structure,
 Posix and Single Unix specification.
General features of Unix commands/ command
structure.
Command arguments and options.

3
Introduction
In-order to introduce the concept of UNIX OS, we
should go through the topic:
A. Components of Computer System
B. What is an Operating System?

4
Introduction

5
Introduction

6
Introduction

7
Operating System

8
Operating System
The operating system (OS) is the most
important program that runs on a
computer. Every general-purpose
computer must have an operating system
to run other programs and applications.
Computer operating systems perform basic
tasks, such as recognizing input from the 
keyboard, sending output to the display
screen, keeping track of files and
directories on the storage drives, and
controlling peripheral devices, such as
printers. 

9
Animation-OS
https://www.youtube.com/watch?v=GjNp0bBrjmU

10
History of Unix

11
THE UNIX OPERATING SYSTEM
• Like DOS and Windows, there's another operating system called UNIX.
• It arrived earlier than the other two, and stayed back late enough to give us
the Internet.
• It has practically everything an operating system should have, and several
features which other OS never had.
• It runs practically on every Hardware and provided inspiration to the Open
Source movement.
• You interact with a UNIX system through a command interpreter called
the shell.
• A command may already exist on the system or it could be one written by
user.
• However, the power of UNIX lies in combining these commands in the
same way the English language lets you combine words to generate
meaningful idea.

12
Architecture

13
Components of UNIX

14
Features

15
UNIX structure
Unix is a multiuser, multitasking operating system
that was developed by Bell Laboratories in 1969.
In a multiuser system, many users can use the system
simultaneously.
A multitasking system is capable of doing multiple
jobs.
Each user interacts with their own shell instance in
this type of operating system and can start
applications as required

16
UNIX structure

As seen in the image, the main components


of the Unix operating system structure are
the kernel layer, the shell layer and the
application layer.

17
Kernel

The kernel provides a bridge between the hardware


and the user. It is a software application that is central
to the operating system. The kernel handles the files,
memory, devices, processes and the network for the
operating system. It is the responsibility of the kernel
to make sure all the system and user tasks are
performed correctly.

18
Shell
The program between the user and the kernel is known
as the shell. It translates the many commands that are
typed into the terminal session. These commands are
known as the shell script. There are two major types of
shells in Unix. These are Bourne shell and C Shell. The
Bourne shell is the default shell for version 7 Unix.
The character $ is the default prompt for the Bourne
shell. The C shell is a command processor that is run in
a text window. The character % is the default prompt for
the C shell.

19
Applications

The applications and utility layer in Unix includes the


word processors, graphics programs, database
management programs, commands etc. The application
programs provide an application to the end users.
For example, a web browser is used to find information
while gaming software is used to play games. The
requests for service and application communication
systems used in an application by a programmer is
known as an application program interface (API)

20
POSIX AND THE SINGLE UNIX SPECIFICATION

Different subsets of POSIX that were introduced

POSIX – 1 – Specifies the C application program


POSIX – 1b - the system calls (Kernel).
POSIX – 1c – Deals with the Shell and utilities.

The “Write once, adopt everywhere” approach to this


development means that once software has been
developed on any POSIX machine it can be easily ported
to another POSIX compliant machine with minimum or
no modification.

21
COMMAND STRUCTURE
To understand power of UNIX, user must know syntax of
important UNIX commands.
The general syntax of UNIX command is -
command arguments
 Commands and arguments have to be separated by spaces
or tabs to enable the system to interpret them as words.
 UNIX arguments range from the simple to the complex.
 Arguments may consist of options, expressions,
instructions and filenames etc.

22
Options
Options are special type of arguments mostly used with a
minus(-) sign.
 An option is normally preceded by a minus(-) sign to
distinguish it from filenames or other arguments.
 Example:
$ ls -l note # -l option list all the attributes of the file note
-rwxrwxrwx 1 mahesh mahesh 811 Jan 27 12:20 note
Note:
Options can normally be combined with only one (-) sign, i.e.,
instead of using $ ls -l -a -t -d you might as well use $ ls -latd.

23
Filename Arguments
Many UNIX commands use a filename as argument so the command can take
input from the file.
If a command uses a filename as argument, it will generally be its last argument.
It's also quite common to see many commands working with multiple filenames
as arguments.
The command with its arguments and options is known as the command line.
This line can be considered complete only after the user has hit [Enter].
The complete line is then fed to the shell as its input for interpretation and
execution.
Examples:
 $ ls -lat chap01 chap02 chap03 # Multiple filenames as arguments
 $ rm chap01 chap02
$ cp chap01 chap01.bak

24
Exceptions
There are some commands that don't accept any arguments.
There are also some commands that may or may not be specified
with arguments.
The ls command can run without arguments (ls), with only options
(ls -l) and also with only filenames like- (ls chap01 chap02)
Examples:
 $ pwd # pwd prints the current working directory
/root
 $ who # who lists currently logged in users

mahesh tty7 2020-01-30 09:08 mahesh pts/1 2020-01-30 10:20 (:0)

25
Unix files

26
Session Objectives
In this session you will learn the following sub topics:

Basic Unix commands such as echo, printf, ls

27
Basic Unix commands
A command is an instruction given by a user telling
a computer to do something, such a run a single program or a
group of linked programs.

echo command
echo command in linux is used to display line of text/string that
are passed as an argument . This is a built in command that is
mostly used in shell scripts and batch files to output status text to
the screen or a file.

28
Basic Unix commands
Syntax :
echo [option] [string]

Displaying a text/string :
Syntax :
echo [string]
Example:

29
Basic Unix commands
Options of echo command
NOTE :- -e here enables the interpretation of backslash
escapes
1. \b : it removes all the spaces in between the text
Example:
echo -e "Geeks \bfor \bGeeks“

30
Basic Unix commands
Options of echo command
2. \c : suppress trailing new line with backspace interpretor ‘-
e‘ to continue without emitting new line.
Example:
echo -e "Geeks \cfor Geeks"

31
Basic Unix commands
Options of echo command
3. \n : this option creates new line from where it is used.
echo -e "Geeks \nfor \nGeeks“
Example:

32
Basic Unix commands
Options of echo command
4. \t : this option is used to create horizontal tab spaces.
echo -e "Geeks \tfor \tGeeks“
Example:

33
Basic Unix commands
Options of echo command
5. \r : carriage return with backspace interpretor ‘-e‘ to have
specified carriage return in output.
echo -e "Geeks \rfor Geeks“
Example:

34
Basic Unix commands
Options of echo command
6. \v : this option is used to create vertical tab spaces.
echo -e "Geeks \vfor \vGeeks
Example:

35
Basic Unix commands
Options of echo command
7. \a : alert return with backspace interpretor ‘-e‘ to have
sound alert.
echo -e "\aGeeks for Geeks
Example:

36
Basic Unix commands
Options of echo command
8. echo * : this command will print all files/folders, similar to
ls command .
echo *
Example:

37
Basic Unix commands
Options of echo command
9. -n : this option is used to omit echoing trailing newline.
echo -n "Geeks for Geeks“
Example:

38
Basic Unix commands
printf command in Linux with Examples
“printf” command in Linux is used to display the given string,
number or any other format specifier on the terminal window. It
works the same way as “printf” works in programming languages
like C.
Syntax:
$printf [-v var] format [arguments]
Note: printf can have format specifiers, escape sequences or
ordinary characters.
Format Specifiers: The most commonly used printf
specifiers are %s, %b, %d, %x and %f.
39
Basic Unix commands
Example:
%s specifier: It is basically a string specifier for string output.

$printf "%s\n" "Hello, World!"


Output:

40
Basic Unix commands
Example:
%b specifier: It is same as string specifier but it allows us to
interpret escape sequences with an argument.
Input: printf "%s\n" "Hello, World! \n" "From Geeks For Geeks\n"
Output: Hello, World! \n From Geeks For Geeks\n
Note: The escape sequence “\n” in the above lines has no special
meaning when used with “%s” specifier and is treated just like
other characters. If we replace “%s” with “%b“, Then it will be
interpreted as a newline character.
Input: printf "%b\n" "Hello, World! \n" "From Geeks For Geeks\n"
Output: Hello, World! From Geeks For Geeks

41
Basic Unix commands
Example:
$printf "%b\n" "Geeks" "for" "\nGeeks\n"
Output:

42
Basic Unix commands
%d specifier: It is an integer specifier for showing the integral
values.
Example:
$printf "%d\n" "213" "109"
Output:

43
Basic Unix commands
%f specifier: It is used for output of floating point values.
Example:
$printf "%f\n" "1.32" "3.14“
Output:

44
Basic Unix commands
%x specifier: It is used for output of lowercase hexadecimal
values for integers and for padding the output
Example:
$printf "%08x\n" "1024".
Output:

45
Basic Unix commands
ls command in Linux with Examples
ls is a Linux shell command that lists directory contents of
files and directories.
Syntax:
$ls [option] [filename]

46
Basic Unix commands
1. Open Last Edited File Using ls -t
ls -t : It sorts the file by modification time, showing the last
edited file first. head -1 picks up this first file. To open the last
edited file in the current directory use the combination of ls and
head commands as shown below.

47
Basic Unix commands
2. Display One File Per Line Using ls -1

48
Basic Unix commands
3. Display All Information About Files/Directories Using
ls -l
$ ls -l : To show long listing information about the
file/directory.

49
Basic Unix commands
-rw-rw-r– 1 maverick maverick 1176 Feb 16 00:19 1.c
1st Character – File Type: First character specifies the type of
the file.
In the example above the hyphen (-) in the 1st character
indicates that this is a normal file. Following are the possible
file type options in the 1st character of the ls -l output.

Field Explanation
– normal file
d : directory
s : socket file
l : link file 50
Basic Unix commands
Field 1 – File Permissions: Next 9 character specifies the files
permission. The every 3 characters specifies read, write, execute
permissions for user(root), group and others respectively in
order. Taking above example, -rw-rw-r– indicates read-write
permission for user(root) , read permission for group, and no
permission for others respectively. If all three permissions are
given to user(root), group and others, the format looks like
-rwxrwxrwx
Field 2 – Number of links: Second field specifies the number
of links for that file. In this example, 1 indicates only one link to
this file.
Field 3 – Owner: Third field specifies owner of the file. In this
example, this file is owned by username ‘maverick’. 51
Basic Unix commands
Field 4 – Group: Fourth field specifies the group of the file.
In this example, this file belongs to ”maverick’ group.
Field 5 – Size: Fifth field specifies the size of file in bytes. In
this example, ‘1176’ indicates the file size in bytes.
Field 6 – Last modified date and time: Sixth field specifies
the date and time of the last modification of the file. In this
example, ‘Feb 16 00:19’ specifies the last modification time of
the file.
Field 7 – File name: The last field is the name of the file. In
this example, the file name is 1.c.

52
Basic Unix commands
4. Display File Size in Human Readable Format Using ls
-lh
ls -lh (h stands for human readable form) : To display file size
in easy to read format. i.e. M for MB, K for KB, G for GB.

53
Basic Unix commands
5. Display Directory Information Using ls –ld
When you use “ls -l” you will get the details of directories content. But if
you want the details of the directory then you can use -d option as., For
example, if you use ls -l /etc will display all the files under
the etc directory. But, if you want to display the information about the
/etc/ directory, use -ld option as shown below.
$ ls -l /etc

54
Basic Unix commands
6. Order Files Based on Last Modified Time Using ls -lt
ls -lt : To sort the file names displayed in the order of last
modification time.You will be finding it handy to use it in
combination with -l option.

55
Basic Unix commands
7. Order Files Based on Last Modified Time (In Reverse
Order) Using ls -ltr
$ ls -ltr : To sort the file names in the last modification time in reverse
order. This will be showing the last edited file in the last line which will
be handy when the listing goes beyond a page.

56
Basic Unix commands
8. Display Hidden Files Using ls -a (or) ls -A
$ ls -a : To show all the hidden files in the directory, use ‘-a option’.
Hidden files in Unix starts with ‘.’ in its file name.It will show all the files
including the ‘.’ (current directory) and ‘..’ (parent directory).

57
Basic Unix commands
9. Display Files Recursively Using ls -R
$ ls /etc/apt

58
Basic Unix commands
10. Display File Inode Number Using ls -i
Sometimes you may want to know the inone number of a file for internal
maintenance. Use -i option as shown below to display inone number.
Using inode number you can remove files that has special characters in
it’s name.
$ ls -i

59
Basic Unix commands
11. Hide Control Characters Using ls -q
ls -q : To print question mark instead of the non graphics
control characters.

60
Basic Unix commands
12. Display File UID and GID Using ls -n
$ ls -n ~/kv : Lists the output like -l, but shows the uid and
gid in numeric format instead of names.

61
Basic Unix commands
13. Visual Classification of Files With Special Characters
Using ls -F
$ ls -F : Instead of doing the ‘ls -l’ and then the checking
for the first character to determine the type of file. You
can use -F which classifies the file with different special
character for different kind of files.
/ – directory.
nothing – normal file.
@ – link file.
* – Executable file
62
Basic Unix commands
13. Visual Classification of Files With Special Characters
Using ls -F

63
Basic Unix commands
14. Visual Classification of Files With Colors Using ls -F
$ ls –color=auto : Recognizing the file type by the color in which it gets
displayed is an another kind in classification of file. In the below output
directories get displayed in blue, soft links get displayed in green, and
ordinary files gets displayed in default color.

64

You might also like