From: Mr. Sohit Agarwal Lecturer, Dept. of CS/IT, JNIT 22 CONTENTS II
Total Page:16
File Type:pdf, Size:1020Kb
From: Mr. Sohit Agarwal Lecturer, Dept. of CS/IT, JNIT 22 CONTENTS II ((ii) IInnttrroodduuccttiioonoof UUNN IIXX 44 ((iiii)) TTyyppees oof SShheelll 55 Commands 1.1. ddaattee 88 22.. wwhhoo 10 33.. ccaall 12 44.. llss 14 55.. eedd 16 66.. ccaatt 18 7.7. touch 20 88.. wwcc 22 99.. mmoorree 24 1100.. eecchhoo 26 1111.. pprr 28 1122.. bbcc 30 1133.. mmkkddiirr 32 1144.. rrmmddiirr 34 1155.. hheeaadd 36 16. tail 38 1177.. ppss 40 1188.. kkiillll 42 33 1199.. ccpp 44 2200.. vvii 46 2211.. ppwwdd 48 2222.. sshh 50 2233.. ggrreepp 52 2244.. ccmmpp 54 25. diff 56 4 CONTENTS Programs 1. Program to print Hello World 58 2. Program to obtain all files in current directory 58 3. Program to accept a number from user and print the same 60 4. Program to check who all are logged on 60 5. Program for addition of two numbers 62 6. Program for multiplication of two numbers 62 7. Program for adding the digits of a number 64 8. Program for reversing a number 64 9. Program to calculate Simple Interest 66 10. Program to check if given number is even or odd 66 11. Program to demonstrate syntax of while 68 12. Program to demonstrate syntax of switch 68 13. Program to calculate area and circumference of a circle 70 14. Program to check if the year is a leap year or not 70 15. Program to find the factorial of a number 72 16. Program to print the Fibonacci sequence 72 17. Program to find the shortest of three numbers 74 18. Program to find the largest of three numbers 76 19. Program to find a particular pattern 76 5 INTRODUCTION OF UNIX What is UNIX? UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren't covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session. Types of UNIX There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X. We use Solaris on our servers and workstations, and Fedora Linux on the servers and desktop PCs. The UNIX operating system The UNIX operating system is made up of three parts; the kernel, the shell and the programs. The kernel of UNIX is the hub of the operating system. it allocates time and memory to programs and handles the filestore and communications in response to system calls. The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. Everything in UNIX is either a file or a process. A process is an executing program identified by a unique PID (process identifier). A file is a collection of data. They are created by users using text editors, running compilers etc. Files can be a document (report, essay etc.), the text of a program written in some high-level programming language, etc. UNIX is really a reliable operating system with very few number of virus attacks because it does not support .exe, .bat extensions. 6 Types of Shell There are 13 types of shell known. They are listed below : 1. sh The Bourne shell, or sh, was the default Unix shell of Unix Version 7, and replaced theThompson shell, whose executable file had the same name, sh. It remains a popular default shell for Unix accounts. 2. ash The Almquist shell (also known as A Shell or ash) was originally Kenneth Almquist’s clone of the SVR4-variant of the Bourne shell; it is a fast, small, POSIX-compatible Unix shell designed to replace the Bourne shell in later BSD distributions. 3. dash Debian Almquist shell (dash) is a POSIX-compliant Unix shell, much smaller than bash. It requires less disk space but is also less feature rich. dash is a direct descendant of the NetBSD version of the Almquist Shell. 4. bash Bash is a Unix shell written for the GNU Project. The name of the actual executable is bash. Its name is an acronym for Bourne-again shell, a pun on the name of the Bourne shell. Bash is the default shell on most Linux systems as well as on Mac OS X and it can be run on most Unix-like operating systems. 5. fish fish is a Unix shell. Its name is an acronym for friendly interactive shell. fish focuses on interactive use, discoverability, and user friendliness. 6. ksh 15 Command 4 ‘ls’ COMMAND NAME : ls - list directory contents SYNOPSIS : ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). -a, --all : do not ignore entries starting with . -A, --almost-all : do not list implied . and .. --author : with -l, print the author of each file --block-size=SIZE : use SIZE-byte blocks -B, --ignore-backups : do not list implied entries ending with ~ -C : list entries by columns --color[=WHEN] : control whether color is used to distinguish file types. -d, --directory : list directory entries instead of contents. -F, --classify : append indicator (one of */=>@|) to entries --file-type : likewise, except do not append â*â -g : like -l, but do not list owner -l : uses a long listing format. -lt : causes the file to be listed in time order. -lut : shows the least used files in time sequence -u : gives information about when that file was actually used. 16 OUTPUT: ed COMMAND Command 5 17 ‘ed’ COMMAND NAME : ed, red - text editor SYNOPSIS : ed [-] [-Gs] [-p string] [file] red [-] [-Gs] [-p string] [file] DESCRIPTION ‘ed’ is a line-oriented text editor. It is used to create, display, modify and otherwise manipulate text files. 'red’ is a restricted ed. it can only edit files in the current directory and cannot execute shell commands. If invoked with a file argument, then a copy of file is read into the editorâs buffer. Changes are made to this copy and not directly to file itself. Upon quitting ed, any changes not explicitly saved with a âwâ command are lost. Editing is done in two distinct modes: command and input. When first invoked, ed is in command mode. In this mode commands are read from the standard input and executed to manipulate the contents of the editor buffer. A typical command might look like: ,s/old/new/g which replaces all occurrences of the string old with new. When an input command, such as âaâ (append), âiâ (insert) or âcâ (change), is given, ed enters input mode. This is the primary means of adding text to a file. In this mode, no commands are available; instead, the standard input is written directly to the editor buffer. Lines consist of text up to and including a newline character. Input mode is terminated by entering a single period (.) on a line. OPTIONS -G : Forces backwards compatibility. -s : Suppresses diagnostics. This should be used if edâs standard input is from a script. -p string : Specifies a command prompt. This may be toggled on and off with the âPâ command. 22 OUTPUT: wc COMMAND Command 8 23 ‘wc’ COMMAND NAME : wc - print the number of newlines, words, and bytes in files SYNOPSIS : wc [OPTION]... [FILE]… DESCRIPTION Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. -c, --bytes : print the byte counts -m, --chars : print the character counts -l, --lines : print the newline counts -L, --max-line-length : print the length of the longest line -w, --words : print the word counts 24 OUTPUT: more COMMAND Command 9 36 OUTPUT: head COMMAND Command 15 ‘head’ COMMAND 37 NAME : head - output the first part of files SYNOPSIS : head [OPTION]... [FILE]... DESCRIPTION Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -c, --bytes=[-]N : print the first N bytes of each file; with the leading â-â, print all but the last N bytes of each file. -n, --lines=[-]N : print the first N lines instead of the first 10; with the leading â-â, print all but the last N lines of each file. -q, --quiet, --silent : never print headers giving file names -v, --verbose : always print headers giving file names OUTPUT: tail COMMAND 38 Command 16 ‘tail’ COMMAND 4242 OUTPUT: kill COMMAND EFFECT : Since we are supplied the process id of the bash process, as soon as we press enter the bash process is killed and we are logged out. Command 18 4433 ‘‘kill’ COMMAND NNAAMMEE :: kill - terminate a process SSYYNNOOPPSSIISS :: kill [ -s signal | -p ] [ -a ] [ -- ] pid ... kill -l [ signal ] DESCRIPTION The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal.