Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shell Programming and Scripting Languages

Similar presentations


Presentation on theme: "Shell Programming and Scripting Languages"— Presentation transcript:

1 Shell Programming and Scripting Languages
III Year IT –B

2 UNIT-I Introduction to Unix: Unix utilities :
Architecture of Unix, Features of Unix , Unix utilities : process utilities, disk utilities, networking commands, text processing utilities and backup utilities. Introduction to Unix file system vi editor, file handling utilities, security by file permissions.

3 Operating Systems -Basics

4 Operating System A program that controls the execution of application programs An interface between applications and hardware Different flavors of Operating Systems: Single user , single tasking : DOS etc. Multi user and multi tasking : Windows , UNIX etc

5 What is UNIX? UNIX is an open operating system, developed by Bell labs in 1969 UNIX is a command line OS , also supports GUI Simple and easy to use.

6 Features of UNIX

7 Why UNIX ? PORTABLE. MULTIUSER. MULTITASKING. NETWORKING.
ORGANIZED FILE SYSTEM DEVICE INDEPENDENCE. UTILITIES. SERVICES.

8 PORTABLE UNIX is developed on C-LANGUAGE.
C-Programs can be easily moved from one hardware environment to another. It is relatively simple to port it to different environments

9 MULTIUSER UNIX allows multiple users to concurrently share hardware and software. The Unix resource sharing algorithm allows to share the resources while at the same time preventing any one user from locking out others. Everyone gets an equal chance at the resources

10 MULTITASKING Multitasking is an Operating system feature that allows the user to run more than one job at a time. In UNIX, a user can start a task, such as a C compilation, and then move to another task, such as editing a file, without quitting the first. In fact more than one program can be running in the background while a user is working in the foreground.

11 NETWORKING NETWORKING allows users at one location to log into systems at other sites. Users can gain access to remote systems, they operate just as though they were on a system in their area. They can execute any command of UNIX if they have permissions. Access to another system users a standard communication protocol knows as transmission control protocol / internet protocol (TCP/IP)‏

12 ORGANIZED FILE SYSTEM & DEVICE INDEPENDENCE
UNIX has a very organized file and directory system that allows users to organize and maintain files UNIX treats input and output devices like ordinary files. The input for a program can come from any device or file and output for a program can go to any device or file.

13 UTILITIES Software developers have developed over 100 utilities that a user can use in a UNIX system and more are being developed continuously. This library of utility programs provides the user with easily used productivity tools readily available with short keyboard commands. EX: ls, sort,lp,mail ... etc

14 SERVICES System Administrators monitor the system and help users when necessary. Support utilities are provided by Unix for system administrators to access system resources such as disk and security access.

15 Architecture of UNIX

16 Architecture of Unix Shell Kernel UNIX is case sensitive User User
Utilities Application Programs Kernel UNIX is case sensitive

17 shell Shell is the part of the unix that is most visible to the user.
To do anything we must give the shell a command. If the command requires a utility, the shell requests that the kernel execute the utility. If the command requires an application program, the shell requests that it be run.

18 Two Major parts of shell
1) Command line interpreter: Reads your commands and works with the kernel to execute them. 2) Programming capability: Allows you to write shell script. A shell script is a file that contains SHELL COMMANDS that perform a useful function

19 Standard Shells in UNIX
Bourne shell C shell Korn Shell Other shells: tcsh, bash

20 Bourne shell 1) The default prompt for the Bourne shell is $ (or #, for the root user). 2) Bourne shell devloped by steve bourne at the AT&T labs is the oldest. 3) It has very good features for controlling input and output, but is not well suited for the interactive user

21 C shell 1) The C-shell developed in Berkley by BILL JOY.
2) A compatible version of C-shell is tcsh. 3) The command looks similar to C like statements. 4)The default prompt for the C shell is %. 5)It has job control, so that you can reattach a job running in the background to the foreground.

22 Korn shell 1) Korn shell, devloped by David korn,at AT & T labs.
2) korn shell is powerful because it is compatible with the bourne shell. 3) Default prompt for korn shell is $

23 KERNEL Applications do not have direct access to the computer hardware. Applications have to request hardware access from a third-party that mediates all access to computer resources, the Kernel.

24 UNIX Commands

25 UNIX Command A UNIX command is an action request given to the UNIX shell for execution. Command syntax: Verb [options] [arguments] Verb is a name of the command Options is a command modifier preceded by + /- Arguments gives more information like file name

26 UNIX commands command Meaning date Displays date and time date -u
Displays GMT date and time cal Current month calendar cal month year Displays specified year calendar who Displays all users currently logged into system who –u Gives details of each user who am i Returns current user id echo Displays message

27 UNIX commands command Meaning passwd to change the password
man command name Displays online manual clear Clears the screen bc Binary calculator vi filename Opens screen editor mkdir dirname Creating a new directory cd dirname Changes the current director to the specified director

28 UNIX commands command Meaning cp source des
Copies files/dir from source destination mv s d Moves a file/dir from source to destination rmdir dirname Removes empty directory ls Lists the contents of directory ls -l Long lists ls -d Working directory cat filename Creates a file

29 PATH Path is an environment variable echo $PATH
PATH will contain the paths of different shells such as korn, c, tcsh etc. PATH variable can be set in .profile or .bash_profile depending upon the shell you are using. If you want to set your compilers like c or java we set the path variable in PATH variable.

30 The path variable is used to search for a command directory.
Unix command uses the entries in the path variable to search for the command under each directory in the PATH variable. Example: The shell would look for the date command first in /bin next /usr/bin finally current directory.

31 Example $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/games $
Every path variable is seperated by a colon ( : ) .

32 man man - an interface to the on-line reference manuals
The man command displays online documentation. You can quickly check the online manual and look up the answer. There is even a manual explanation for the man command itself.

33 1 Executable programs or shell commands
2 System calls (functions provided by the kernel)‏ 3 Library calls (functions within program libraries)‏ 4 Special files (usually found in /dev)‏ 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)‏ 8 System administration commands (usually only for root)‏ 9 Kernel routines [Non standard]

34 Example of man NAME date - print or set the system date and time
SYNOPSIS date [OPTION]... [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] DESCRIPTION Display the current time in the given FORMAT, or set the system date. -d, --date=STRING display time described by STRING, not ‘now’

35 AUTHOR Written by David MacKenzie. REPORTING BUGS Report bugs to COPYRIGHT Copyright © Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later < This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly installed at your site, the command info date

36 Example man 2—system calls
$man 2 open NAME open, creat - open and possibly create a file or device SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode);

37 man -k option searches the short descriptions of manual pages
-k – will display information including commands about the topic Example : man -k command_topic man -k unlink unlink (1) call the unlink function to remove the specified file unlink (1posix) call the unlink function unlink (3posix) remove a directory entry unlinkat (2) remove a directory entry relative to a directory file descriptor

38 man -f option searches the manual pages referenced by option and print out the short descriptions of any found. man -f man man (1) an interface to the on-line reference manuals man (7) macros to format man pages man (1posix) display system documentation

39 echo print message command
The echo command copies its arguments back to the terminal. Syntax: echo -options arguments Example: 1) echo Hello world 2) echo “Error 105: invalid number”

40 echo -e option -e enable interpretation of backslash escapes
\a alert (BEL)‏ \b backspace

41 \c suppress trailing newline
\f form feed \n new line \r carriage return \t horizontal tab \v vertical tab

42 Examples 1) $ echo -e "printing backslash \\" printing backslash \
2) $echo -e "ring the bell \a" ring the bell 3) $echo -e "backspace\b e is removed in backspace" backspac e is removed from backspace 4) $ echo -e " hello \n\n \c" hello

43 Examples hello x y z hello x y z sandeep@sandeep:~$
echo -e "hello\vx\vy\vz" hello x y z echo -e "hello\tx\ty\tz" hello x y z

44 printf printf - format and print data Syntax:
printf FORMAT [ARGUMENT]... printf OPTION

45 Printf FORMAT controls the output as in C printf. Interpreted sequences are same as echo -e. %c --> character %d --> integer %f --> float %s --> string

46 Examples on printf $printf "integer=%d \n float=%f \n string=%s \n char=%c\n" $integer $float "$string" $char integer=1 float= string=welcome to printf char=c

47 Script record session command
Used to record an interactive session. Script makes a typescript of everything printed on your terminal. To record a whole session, including logout make it the first command of the session. Syntax: script -options arguments

48 How to stop recording ? exit

49 arguments -a --> to append the data to the exisisting file.
-q --> be quiet -t --> Output timing data to standard error. -t contains two fields 1) How much time elapsed since the previous output ? 2) How many characters were output this time ?

50 Example $ script scriptfile Script started, file is scriptfile $ cat x
hello welcome to unix programminf how are you feeling $ exit exit Script done, file is scriptfile try other arguments

51 Passwd used to chang your password
passwd - change user password syntax passwd [options] [LOGIN]

52 Changes passwords for user accounts.
A normal user may only change the password for his/her own account. The superuser may change the password for any account.

53 options -l --> Lock the password of the named account.
-m --Set the minimum number of days between password changes to MIN_DAYS. A value of zero for this field indicates that the user may change his/her password at any time.

54 -u --> Unlock the password of the named account.
This option re-enables a password by changing the password back to its previous value -x --> Set the maximum number of days a password remains valid. After MAX_DAYS, the password is required to be changed.

55 -d --> Delete a user´s password (make it empty).
This is a quick way to disable a password for an account. It will set the named account passwordless. -w --> Set the number of days of warning before a password change is required. Number of days prior to the password expiring, that a user will be warned that his/her password is about to expire.

56 As a general guideline, passwords should consist of 6 to 8 characters
Including one or more characters from each of the following sets: 1) lower case alphabetics 2) digits 0 thru 9 3) punctuation marks

57 Examples on passwd sandeep@sandeep:~$ passwd sandeep
Changing password for sandeep. (current) UNIX password: Enter new UNIX password: Retype new UNIX password: Password unchanged passwd: password updated successfully

58 Examples on passwd $ passwd -d dummy passwd: Permission denied. $ su
Password: # passwd -d dummy Password changed. #

59 Examples on passwd # passwd -l dummy Password changed. #

60 # passwd -n 20 dummy Password changed. # passwd -S dummy dummy P 06/23/ by default it warns before 7 days # passwd -w 15 dummy dummy P 06/23/

61 Stty set terminal command
The set terminal (stty) command sets / unsets selected terminal input/output options. To configure the terminal. Syntax: Stty options arguments

62 options to stty -a, --all -g, --save
print all current settings in human-readable form -g, --save print all current settings in a stty-readable form.

63 Examples $ stty -a speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; $ stty -g 6d02:5:4bf:8a3b:3:1c:7f:15:4:0:1:ff:11:13:1a:ff:12:f:17:16:ff:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

64 Set terminal without option or argument
Stty without options are arguments will display the current common settings of your terminal. Network settings ( baud rate)‏ Control settings ( delete key)‏

65 Example $ stty speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?; ixany iutf8

66 Set terminal with arguments
Set erase and kill (ek)‏ Set terminal to general configuration(sane)‏ Set erase key(erase)‏ Set kill (kill)‏ Set interrupt key (intr)‏

67 Set erase and kill(ek)‏
ek argument sets the default erase Ctl + h -> delete key Ctl + c -> kill Example: $ character is deleted when you press ctl +h $ line is killed when you press ctl + c

68 Set terminal to general configuration (sane)‏
The sane argument sets the terminal configuration to a reasonable setting that can be used with a majority of terminals Example: $stty sane

69 Set erase key (erase)‏ Example: Default erase key is ctl + h
It deletes the previous character typed. We can reconfigure the keyboard to use another key as the delete Example: $ stty erase stty: missing argument to `erase' Try `stty --help' for more information. $ stty erase x $

70 Set kill (kill)‏ The kill key deletes the whole line.
Default is ctl + u Example $ stty kill ^b $ TYPE AND PRESS CTL + b

71 Examples on stty $stty -echoe switching off backspace
$stty -echo switching off keyboard input display $stty intr ^c setting interrupt key to ctl +c

72

73 tty: (Knowing your Terminal)‏
Since Unix treats terminals as files, tty(teletype) command tells you the file name of the terminal you are using. It is an obvious reference to the device. Example: $tty /dev/pts/10 you can use tty in a shell script to control the behavior of the script depending on the terminal it is invoked from. If the program must from only one specified terminal, the script logic must use tty to make decision.

74 Uname uname - print system information
-a, --all print all information, in the following order, except omit -p and -i if unknown: -s, --kernel-name print the kernel name -n, --nodename print the network node hostname -r, --kernel-release print the kernel release

75 -v, --kernel-version print the kernel version -m, --machine print the machine hardware name -p, --processor print the processor type or "unknown" -i, --hardware-platform print the hardware platform or "unknown" -o, -- operating-syste print the operating system

76 Examples for uname $ uname Linux $ uname -s $ uname -a $ uname -n
Linux sandeep #1 SMP Thu Mar 26 01:08:11 UTC 2009 i686 GNU/Linux $ uname -n sandeep $ uname -r

77 $ uname -m i686 $ uname -p unknown $ unmae -i bash: unmae: command not found $ uname -o GNU/Linux $ uname -v #1 SMP Thu Mar 26 01:08:11 UTC 2009

78 Who who is online Displays all the users currently logged in into the system syntax: who options am i

79 Options -b, --boot time of last system boot -d, --dead
print dead processes -H, --heading print line of column headings

80 Options -q, --count all login names and number of users logged on
-r, --runlevel print current runlevel -s, --short print only name, line, and time (default)‏

81 Options -u, --users list users logged in -H, --heading
print line of column headings

82 Examples $ who am i sandeep pts/1 2009-06-24 13:07 (:0.0)‏
tells the current user who is logged in. $ who -H | more NAME LINE TIME COMMENT sandeep tty :51 (:0)‏ $ who -u | more sandeep tty : (:0)‏ sandeep pts/ :23 02: (:0.0)‏ shows idle time, process numbers

83 Examples on who $ who -b --> boot system boot 2009-06-24 09:50
$ who -d >dead processes : id=si term=0 exit=0 : id=l2 term=0 exit=0 $ who -q --> users count sandeep sandeep sandeep # users=3 $ who -r run-level : last=S

84 date: (Display the System Date)‏
The UNIX system maintains an internal clock, you can display the current date with the ‘date’ command, which shows the date and time to the nearest second. Example: $date Sat June 14 16:22:40 IST 2008 2) The command can also be used with suitable format specifiers as arguments. Each argument is preceded by the + symbol, followed by the % operator and a single character describing the format.

85 Options +%d The day of the month +%m The month +%y +%h The month name
Format Specifier Significance +%d The day of the month +%m The month +%y The last two digits of the year +%h The month name +%H Hour +%M Minute +%S Second +%D The date in format mm/dd/yy +%T The time in format hh:mm:ss

86 Examples $date +%m 06 $date +”%h %m” Jun 06
$date “+this is the date %B” this is the date june explore more options in lab

87 lp To print a file/spool a file to print. Example: $lp a.lst
request id is prl-320 (1 file)‏ $_ Options: -d<printer name> <file name> To specify printer name. -t”tile” <file name> To specify page title. -m To notify user after the file has been printed. -n<number> To specify number of copies. $lp –dlaser a.list $lp –t”first chapter” a.txt $lp –n3 –m a.lst

88 MORE More allows us to set the output page size and pauses at the end of each page to allow us to read the file. After each page we may request one or more lines, a new page, or quit. Syntax: $more options input_files b / ^b ---- move back to previous screen space – display next screen of output. Enter (return) – Advances one line. d --- Displays half a screen.

89 options -d --> prompts the user with the message "[Press space to continue, ’q’ to quit.]" and will display "[Press ’h’ for instructions.] -l --> Ignores form feed character, unix treats ^L as form feed, pause after any line that contains a form feed. -f Causes more to count logical, rather than screen lines (i.e., long lines are not folded). DO NOT SCROLL -p clear the whole screen and then display the text. -c paint each screen from the top, clearing the remainder of each line as it is displayed.

90 Options of more -s Squeeze multiple blank lines into one.
-u Suppress underlining. -w waits at the end of the output for the user to enter any key to continue. Most of these options dont work properly with pipe Example: $man more | more $ more -f x $more file1 file2 Explore all the options in lab.

91 chmod Used to change the permissions of a file or directory.
By default for a directory the permissions while creation are 777, for file 666 Thes values while creation are subtracted from umask value. The default umask value is 022

92 Changing permissions with chmod
There are two ways to change the permissions. 1) Symbolic : A user can understand and can change. 2) Octal : A computer can understand much faster than user.

93 9 - permissions r --> read w --> write x --> Execute $ls -l
-rw-r--r-- 1 sandeep sandeep :08 a shows the permission column rw-r--r-- rw- --> user permissions r-- --> Group permissions. r-- --> Other permissions User permissions : Permissions for the person who created the file. Group permissions : Permissions for the group, user/root has given access Other permissions : Permissions for Other groups or users who can access the file r --> read w --> write x --> Execute

94 Chmod : Symbolic Assignment operator ( = ) Adding permissions ( + )
Removing permissions ( - )‏ User is represented as ( u )‏ Group is represented as ( g )‏ Others are represented as ( o )‏ For giving permissions to all we use ( a )‏ Read permission is represented as ( r )‏ Write permission is represented as ( w )‏ Execute permission is represented as ( x )‏

95 Examples: Assignment operator
$ ls -l file ---xr--r-- 1 sandeep sandeep :08 file $ chmod u=rw file > change user permission to read and write $ ls -l file --> analyze the output -rw-r--r-- 1 sandeep sandeep :08 a $ chmod g=rw file --> change group permission to read write -rw-rw-r-- 1 sandeep sandeep :08 a $ chmod o=x file

96 Examples: Adding permissions
$ ls -l file -rw-rw---x 1 sandeep sandeep :08 file $ chmod u+x file -rwxrw---x 1 sandeep sandeep :08 file $ chmod u+x,g+x,o+r file -rwxrwxr-x 1 sandeep sandeep :08 file

97 cd There are no options for change directory.
The pathname can be relative or absolute. If there is no pathname argument the default is the home directory. cd ~ --> tilde symbol can also be used to change to home directory. cd .. --> change directory to the parent directory. cd . --> change directory to current directory cd / --> change directory to root. If you dont have Execute permission on a directory you cant change.

98 Example sandeep@sandeep:~/sandeep$ cd BegLinuxProgrammers/
bash: cd: BegLinuxProgrammers/: Permission denied chmod 277 BegLinuxProgrammers/ chmod 177 BegLinuxProgrammers/

99 pwd print name of current/working directory. $pwd
Displays absolute path name of your current directory. Example: /home/kumar cd sandeep/ cd BegLinuxProgrammers/ pwd /home/sandeep/sandeep/BegLinuxProgrammers

100 cp Copy files and directories
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Creates a DUPLICATE of a file. Syntax: $cp options sources destination options -i --> Interactive prompt -r --> recursive (copy subdirectories )‏ -p --> Preserve time and permissions

101 Rules to copy a file successfully
The source file must exist. Otherwise, unix prints the following error message. cp: cannot stat `filesee': No such file or directory If no destination path is specified, UNIX assumes the destination is the current directory. If destination file doesnt exist it is created; if it does exist it is replaced. If the source is a multiple files or a directory the destination must be a directory. To prevent automatic replacement of the destination file, use the interaction ( -i ) option.

102 To preserve the modification and file access times use the preserve ( -p ) option.
Simple file copy: $ cat >file1 a d c f ^C $ cp file1 file2

103 Copy and replace file sandeep@sandeep:~/sandeep$ cat >file1
Contents file 1 (one )‏ These are the contents which are going to be replaced in file 2 ^C cat > file2 Contents of file 2 ( Two )‏ be erased and file 1 contents are replaced. cp file1 file2 cat file2

104

105 Path locating commands
There are two path locating commands. Which Type Example: $ which ls /bin/ls $ type man man is hashed (/usr/bin/man)‏ $

106 Help For any command you can get help. $command -- help

107 cat command cat options input_files Catenate command or cat for short.
It combines one or more files by appending them in the order they are listed in the command. The input can come from the keyboard or file and output goes to the monitor or file. Syntax: cat options input_files

108 Options of cat command -e ----> print $ at the end of the line.
-n ----> number lines -s ---->silent ( no error message )‏ -t ----->print tabs and form feeds. -u ----->unbuffered output. -v -----> print control characters

109 Using cat to display a file
Cat is useful to display a file. When only one input is provided, the file is catenated with a null file. Result is that input file is displayed on the screen ( monitor). They are no automatic pauses at the end of the screen. ( Ex : In more command we have pauses )‏ If we accidentally cat a binary file to the monitor, all types of strange characters and sounds result. File should be a text file for display.

110 Example to display a file with cat
cat a this is cat file name of the file is a this content is getting appended press control + d key for end of file

111 Creating file with cat To create or append contents to a file we need to use redirection opertators. ( > , >>)‏ To tell cat end of file use ( ctl +d ) key in unix it is end of file. stty command tells the interrupt key for end of file. Example $cat > a this is my first file. This contains the contents for file “a” . ctl+d ( press these two keys at a time on keyboard )‏ $

112 Appending contents with cat
If you want to append contents at the end of file use >> double redirection. >> with cat will append contents at the end of the file, if file is not specified then contents will be written to the monitor. Example: cat >>a append contents to file a this content is getting appended` press control + d key for end of file cat a ---to see contents of a this is cat file name of the file is a this content is getting appended

113 Basic cat command srinivas@srinivas:~/srinivas$ cat a b c
this is cat file name of the file is a this is cat file b It contains the contents of b this is cat file c It contains the contents of c in the above we are catenating a b c files and displaying them on screen.

114 Options can be grouped Options are grouped into 4 categories.
1) Visual characters. 2) Buffered output. 3) Missing files. 4) Numbered lines.

115 Visual characters ( -v and -e)‏
If we want to see unprintable characters, such as ASCII control characters. Such characters are not displayed by cat. -v option is used to display, allows us to see control characters. If we use option -ve will display dollar at end of each file. If we use option -vt the tabs will appear as ^I.

116 Examples on visual characters
cat -v example -- > to display unprintable characters cat -ve example ---> to display $ at the end cat -vt example ---> to display tabs a ^I cat -vet example ---> two display tabs as ^I and $ at end cat -v a this is a file with ASCII control characters pageup ^[[5~ pagedown ^[[6~ home ^[OH end ^[OF cat -v a this is a file with ASCII control characters pageup ^[[5~ pagedown ^[[6~ home ^[OH end ^[OF

117 Buffered output When output is buffered, it is kept in the computer until the system has time to write it to a file. Normally cat output is buffered. To have unbuffered write, use -u option. Using these option -u may slow down your system (not recommended)‏ Example: $cat -u file

118 Missing files While concatenating several files together if one of them is missing, the systems displays the message such as Cannot open x.dat : no such file or directory. You can specify cat is to be silent by using -s option. Example: $cat -s x.dat a

119 Numbered lines srinivas@srinivas:~/srinivas$ cat > a hello
are you there cat > b Hi yes I am here cat -n a b 1 hello 2 are you there 3 Hi 4 yes I am here

120 ls The ls commands lists the contents in a directory.
Depending upon the options used it can list files, directories, or sub directories. Syntax : ls options pathname

121 options -l ---------- long list -d --------- working directory.
-n user / group ids -r reverse order -t time sequence -u time of last access -c i-node creation time -p identify directories -R Recursive -i Print i-node print one column

122 ls with no arguments Lists all directories and files in present working directory in columns. Example: ls a BegLinuxProgrammers example y b c x z

123 -l option of ls -l → will list the file in long list format Example:
ls -l total 20 -rw-r--r-- 1 srinivas srinivas :08 a -rw-r--r-- 1 srinivas srinivas :08 b -rw-r--r-- 1 srinivas srinivas :19 c drwxr-xr-x 2 srinivas srinivas :30 sample Examination of output: d rwxr-xr-x srinivas srinivas :30 sample

124 First field Is type of the file.
- → Regular file d → directory c → character special file b → Block special file l → symbolic link. P → FIFO s → socket. Second field is permissions rwx → for user (first three)‏ rwx → for group ( second three)‏ rwx → for others ( last three )‏ There are totally 9 permissions rwxrwxrwx

125 Third field specifies the links
Directory the default no. of links is two. Regular file and others the default link count is 1. For symbolic link the link count is one. Fourth and fifth columns specify the username and Group to which they belong. 6th field tells File size of directory / file 7th field specifies the date and time. 8th field specifies the name of the file

126 Hidden files listing (-a)‏
All hidden files in unix start with . (dot)‏ To list the hidden files with ls use -a option. Example: ls -a a b BegLinuxProgrammers c example .file2 .hiddenfile sample x y z

127 Displaying the working directory name ( -d )‏
-d option displays working directory name. Used with -l option displays attributes. Example: ls -ld drwxr-xr-x 4 srinivas srinivas :49 .

128 Displaying user and group id ( -n )‏
-n is same as l with a little difference, it displays user and group id instead of there names. Example: ls -n total 24 -rw-r--r :08 a -rw-r--r :08 b

129 Reverse order ( -r )‏ -r → sorts the display in descending order.
By default ls sorts in ascending order. Example: ls -r z y x sample example c BegLinuxProgrammers b a

130 Time sorts (-t)‏ There are three time sorts
1) Basic time sort ( -lt ) : sorts by time stamp. 2) Last access ( -lu ): sorts by last access 3) Inode change ( -lc ): sorts by i-node change Example: ls -lt total 24 drwxr-xr-x 2 srinivas srinivas :30 sample -rw-r--r-- 1 srinivas srinivas :08 b -rw-r--r-- 1 srinivas srinivas :08 a

131 Identify directories (-p)‏
-p option appends each directory with a slash at the end for a directory. Example: ls -p a b BegLinuxProgrammers/ c example sample/ x y z

132 Recursive list ( -R )‏ To list all the files in the current directory, and subdirectories and subdirectories until all the directories are listed. Example ls -R .: a b BegLinuxProgrammers c example sample x y z ./BegLinuxProgrammers: a a.out b c copy_system_block_by_block.c copy_system.c out ./sample:

133 Print one column ( -1 )‏ To print filenames as columns we use -1 option Example: ls -1 a b BegLinuxProgrammers c example sample x y z

134 Mkdir (making directories)‏
Mkdir command is used to create new directories. Syntax: mkdir options directory_name options -m -> mode -p -> Parent

135 Examples sandeep@sandeep:~/sandeep$ mkdir Class
ls a b BegLinuxProgrammers c Class example sample x y z mkdir -m 777 SecondYearCSE ls -ld SecondYearCSE/ drwxrwxrwx 2 sandeep sandeep :48 SecondYearCSE/ $ mkdir -p First/Second/Third (To create Recursive Directories )‏ $ mkdir -p Current Current2 ( To Create Multiple Dir in current Dir)‏ $ls -R check the output in lab

136 rmdir (remove Empty directory)‏
rmdir is used to delete directories. The directory should be empty to delete. You need to be in the parent directory, to delete. Syntax: rmdir options directory_name options: -p --> to delete the subdirectories.

137 Examples $ mkdir -p First/Second/Third
cd First/ touch a b c .. mkdir Current $ rmdir -p First/ rmdir: failed to remove `First/': Directory not empty rmdir Current $ rmdir -p First/Second/Third/

138 rm (remove files and directories)‏

139 Unix Utilities

140 Unix Utilities Process utilities Disk utilities Networking commands
Backup utilities Text processing utilities

141 Process utilities Ps (process status) command:
Display some process attributes. Example: $ ps PID TTY TIME CMD Pts/ : bash

142 Process utilities The following options are used with ps command
Description -f Displays full information about process -x Shows information about processes without terminals. -u Shows additional information like -f option. -e Display extended information (system process) Example: $ ps -f UID PID PPID C STIME TTY TIME CMD : pts/ :00:00 -bash : pts/ :00:00 ps -f

143 The following information is displayed by ps command
Column Description UID User ID that this process belongs to (the person running it). PID Process ID. PPID Parent process ID (the ID of the process that started it). C CPU utilization of process. STIME Process start time. TTY Terminal type associated with the process TIME CPU time taken by the process. CMD The command that started this process.

144 Kill command: kill command is used to kill (stop) the process as follows:
Syntax: Kill PID Ex: $ kill 2594 top command : displays the processes information in sorted order

145 Networking commands ftp: file transfer protocol
With this we can upload and download files from one computer to another Syntax: $ ftp host name or ip-address ping : The ping command sends an echo request to a host available on the network. Using this command you can check if your remote host is responding well or not. Syntax: $ ping host name or ip-address

146 telnet: Telnet is a utility that allows a computer user at one site to make a connection, login and then conduct work on a computer at another site. $telnet host name or ip-address finger: The finger command displays information about users on a given host. The host can be either local or remote. $finger Check all the logged in users on local machine $finger hostname or ip_address Check all the logged in users on remote machine

147 Backup utilities tar: the tape archive program
Tar doesn’t normally write to the standard output but creates an archive in the media. Tar accepts file and directory names as arguments. 1.Creating an archive using tar with cvf option: syntax: tar -cvf archive_name.tar dirname/ c – create a new archive v – verbosely list files which are processed. f – following is the archive file name

148 2.Creating a tar gzipped archive using option cvzf
Syntax : tar -cvzf archive_name.tar.gz dirname/ 3.Extracting (untar) an archive using tar command Syntax: tar -xvf archive_name.tar

149 cpio: copy input-output
Cpio command copies files to and from a backup device. It uses standard input to take the list of filenames. Cpio can be used with redirection and piping. Cpio uses two options-o (output) and –i (input) either of which must be there in the command line. $ cpio -oacv > backupfile -a Reset the access times of files after reading them to the initial value. -c  The c option tells cpio to use the ASCII header format. -v The v option results in verbose output

150 Disk utilities Du: disk usage
Du estimate the file space usage on the disk. It produces a list containing the usage of each subdirectory of its argument and finally produces a summary. $du /home/usr1 df: displays the amount of free space available on the disk. The output displays for each file system separately. $ df

151 Introduction to UNIX File System and File Handling Utilities

152 The UNIX file system bin lib dev etc tmp home unix
user user user 3 All the above mentioned directories are present on almost all UNIX installations. These directories and their purpose are given below: bin – Binary executable files lib – Library functions dev – Devices related files etc – Binary executable files usually required for system administration tmp – Temporary files created by UNIX or users home – contains directories of all users unix – UNIX kernel related files.

153 File: All versions of the UNIX recognize three types of files:
The file is a collection of information that is assigned a name and is stored on a secondary storage medium like disk or magnetic tape. All versions of the UNIX recognize three types of files: Ordinary files. This type of file is used to store the data. Directory files. A directory file contains a list of files. Each entry in the list consists of two parts (file name and a pointer to the actual file on the disk. Special files. These files are used to reference physical devices, such as terminals, printers, disks, and tape drives.

154 File Name: A filename can be any sequence of ASCII characters.
Never start your file name with a period. (Which indicates that is hidden) Do not use special characters like > and < .

155 Directories Files are organized by grouping them into directories.
There are four special directories supported by UNIX Root Directory : A top of the directory structure. Home Directory: The directory which holds all user created directories. Working Directory: Current directory. Parent directory: Is immediately above the working directory.

156 pwd :: prints the current working directory
Login: example $ pwd /home/example $ _ / home bin example new

157 Absolute and Relative pathnames
Absolute pathname: A pathname is a sequence of directory names that leads you through the hierarchy from a starting directory to a target file, called ‘absolute pathname’. Eg: /home/example/file1 Relative pathname: A pathname is a sequence of directory names that leads you through its current working directory to a target file, called ‘relative pathname’. It uses following fields: Home directory: ~ current directory: . parent directory: .. Eg’s: ../cse007/file2.c, file1.c, ../../bin/myfile, ./dir1/dfile.c

158 cat – concatenate and print a file/files
$ cat > filename9 ……… ………… type your data here ^d $ $ cat filename9 ………… display the content of filename9

159 cp – copy an ordinary file
cp command allows you to create a duplicate copy of an ordinary file. $ cp srcfile destfile Here srcfile is the original source file and destfile is the name of the copy to be created. $ cat file1 unix is an operating system. $ cat file2 invented by kenthompson. $ unix is an operating system -i interactive copying

160 rm – remove an ordinary file
Removes one or more ordinary files from a directory, effectively erasing the file $ rm filename…….. where each filename is separated by a white space. $ ls file1 file2 file3 $ rm file1 file2 file3 $

161 rm options rm file1file2 file3 file4
removes file1file2 file3 file4 in current directories if exists. rm new/file1 old/file2 rm * deletes all files -r or –R recursive deletion -f forcing removal -rf deletes every thing in the current directory and below

162 mv – move ( rename ) a file
The mv command changes the name associated with a file by associating a new file name. $ mv oldname newname This command line changes the name of the file from ‘oldname’ to ‘newname’.

163 ls – listing the directory contents
To obtain the list of the all file names in the current directory. $ ls 560.c TOC.c XYZ.txt a.out temp.bak $ ls directoryname Lists the content of given directory name

164 ls -options -x : output in multiple columns ls –x
560.c TOC.c XYZ.txt a.out temp.bak -F : identifying Directories and Executables ls –Fx 560.c* TOC.c* XYZ.txt a.out* temp.bak new/ old/ ‘*’ indicates executable code and ‘/’ refers to directory. -a : show hidden files also ls –axF . / . . / .exrc .kshrc .profile .xinitrc .sh_hostory 560.c* TOC.c* XYZ.txt a.out/ old/ new/

165 ls -options -R Recursive list
-r Sorts the filenames in reverse order (based ASCII)‏ -d ‘dirname’ lists only ‘dirname’ if ‘dirname’ is a directory -t sorts file names by last modification time -lt sorts listing by last modification time -u sorts filenames by last access time -lu sorts listing by last access time -I displays longlist

166 ls with longlist option
$ ls -l : which prints following information about your files and directories File type : directory / other Permissions : read /write/execute Links Owner id group id File size Last modification time filename

167 ln – Creating filename Aliases
The ‘ln’ command is used for establishing additional links for the same physical file. $ ln oldname newname Both ‘oldname’ and ‘newname’ refer to the same file. $ ls letter poem1 $ ln poem1 versa letter poem1 versa $ cat poem1 or cat versa generates the same result

168 Wildcards: A wildcard is a token that specifies that one or more different characters can be used to satisfy a specific request. Three wildcards in UNIX: ? ( single character) [ …] ( Set of characters) * ( Zero or more characters)

169 The lp subsystem: printing a file
lp line printing command provides spooling facility. lp file1.c request id is prl -11 ( 1 file)‏ lp –d printername filename uses a printer specified as printername to print filename. lp –t “title” filename prints a title as “title” on the first page. lp –n3 file2.c prints three copies .

170 file : Knowing the file types
UNIX provides the file command to determine the type of file. file filename filename : file type file * lists all file names and their types.

171 wc :Counting lines, words, and characters
wc ‘filename’ no of lines no of words no of chars ‘filename’ A line is any group of characters not containing a newline. A word is a group of characters not containing a space, tab, or newline. A character is the smallest unit of information, and includes a space, tab, and newline. -l counts number of lines -w counts number of words -c counts number of characters wc file1 file2 file2 …………… lines in file1 words in file1 char’s in file1 file1 lines in file2 words in file2 char’s in file2 file2 lines in file3 words in file3 char’s in file3 file3

172 Directory related utilities
pwd : Prints current working directory. mkdir : Creates directory/directories. $ mkdir dir or mkdir dir1 dir2 dir3 $ mkdir /books/thompson/Ritchie rmdir : removing directory / directories $ rmdir directoryname/names $ rmdir –p /books/thompson/Ritchie Removes all the directories. Note: If the directory name is empty then it removed. cd: change directory, uses either absolute path or relative path. cd “pathname”

173 Unix File Security

174 File Security In Unix file system, A file will have a number of attributes (Properties) that are stored in the Inode Table. ls command looks up the file’s Inode Table to fetch its attributes. ls –l command gives detailed listing of all seven fields of attributes of a file. Example: $ls –l Total (Total Bytes occupied by the files, 72 Blocks each of 512B)‏ _rw_r_ _r_ _ kumar metal May : chap1 _rw_r_ _r_ _ kumar metal May : chap2 drw_r_ _r_ _ kumar metal Apr : prgs _rwxr_ _r_ _ kumar metal Apr : add.sh _rw_rw_r_ _ kumar metal Apr : sal.txt

175 Fields: File Type & Permissions: First character indicates file type. [ _ ordinary file, d - Directory] Next series of characters that take the values r, w, x. r – read w- write x- execute Links: The second column indicates the number of links associated with a file. Ownership: Shows owner of all the files and sub-directories. Group Ownership: When opening a user account, the administrator also assigns the user to same group. All the group members have privileges on same file. File size: Shows file size in bytes.

176 File ownership: The user_id (UID) – both its name and number. $id
Last modification time: The 6th , 7th and 8th columns indicates last modification date of file. File name: Names are arranged in ASCII collating sequence. File ownership: When you create a file, your user name shows up in the third column of the file’s listing. your group is the “group owner” of the file displayed in the fourth column of the file’s listing. when the system administrator creates a user account: The user_id (UID) – both its name and number. The group_id (GID) – both its name and number. How can I know my own UID and GID? $id uid=655537(kumar) gid=655535(metal)‏

177 File Permissions Owner: the person who created the file.
Unix follows a three-tired file protection system that determines the file’s access rights. Each group represents a “category” and contains three slots, representing read, write, execute permissions. The three groups/categories are: Owner: the person who created the file. Group: all the users belonging to a group. Others: those how are neither owner nor belonging to a group. (also referred as “world”)‏

178 chmod: Symbolic code. Octal code. Symbolic code:
This command is used to set the permissions of one or more files for all categories of users (owner, group and others)‏ It can be run by only the owner of the file and the super user. The command can be used in two ways: Symbolic code. Octal code. Generally a file or a directory is created with a default set of permissions. The default permissions in general: _rw_r_ _r_ _ Symbolic code: When changing permissions in a relative manner, chmod only changes the permissions specified in the command line and leaves the other permissions unchanged. syntax: $chmod category operation permissions filename

179 u —user Category specifies following: g – group o —other a —all
There are three sets of operators: = reset the permissions add a permission delete a permission Permissions are represented as r read w write x execute

180 Examples: To assign execute permissions to the user.
$ chmod u+x file1.text The expression combining all 3 categories. $ chmod ugo+x file1.text To remove a permission. $ chmod go-r file1.text Combining assign, remove operation in single line. $ chmod a-x,go+r file1.text Combining permissions in a single line. $ chmod o+wx file1.text Note: When you do not specify the category, then the permissions are applied to all categories. $ chmod +x file1.text (it is same as $chmod a+x file1.txt)‏

181 Octal code: Permissions are represented by octal digits.
The octal commands set every permissions. Permission octal values: read write – 2 execute Examples: Assigning r,w permissions to all the 3 categories. $ chmod 666 abc.text (same as $chmod a+rw abc.txt)‏ To restore the original permissions to the file. $ chmod 644 abc.text

182 Using chmod recursively (-R):
-R (recursive) option can be used with chmod to make it descend a directory and apply the expression to every file and subdirectory it finds. Examples: $chmod –R a+x prgs $chmod –R (works on hidden files also)‏ $chmod –R a+x * (Leaves out hidden files)‏

183 Directory permissions:
Directories also have their own permissions and the significance of these permissions differ a great deal from ordinary files. Ordinary files also influenced by the permissions of the directory. A directory must never be writable by group and others. To check permissions of a directory: $mkdir prgs; ls -ld prgs Setting default File and Directory permissions: When you create files and directories, the permissions assigned to them depend on the system’s default settings. The Unix system has the following default permissions: rw_rw_rw_ (Octal 666) for regular files. rwxrwxrwx (Octal 777) for directories.

184 Actually, this default is transformed by subtracting the “user mask” from it to remove one or more permissions. umask is a shell built-in command that can be used to set a new default permissions. Example: $umask (To display current umask value)‏ 002 $umask 022 (To set new umask value)‏ This indicates the octal number to be subtracted from default to obtain the actual default setting. (i.e., 644 for files, 755 for directories)‏ Note: No one – not even the Administrator can use umask to change the system wide default settings. The system wide umask setting is placed in one of the machine’s startup scripts, and is automatically available to all users.

185 Changing ownership and group
There are two commands that allow the owner and group to be changed To change ownership : chown To change group : chgrp Syntax: chown newusername filename


Download ppt "Shell Programming and Scripting Languages"

Similar presentations


Ads by Google