bash shell scripting

29
BASH SHELL SCRIPTING Name: Vikas Tiwari En-roll: 14486SC029

Upload: vikas-tiwari

Post on 15-Apr-2017

472 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Bash shell scripting

BASH SHELL SCRIPTING

Name: Vikas TiwariEn-roll: 14486SC029

Page 2: Bash shell scripting

SHELLS A shell is the user interface or set of programs user to interact with Unix and process commands Common shells are:BourneCKorn

Page 3: Bash shell scripting

BOURNE SHELL (SH) Written by Steven Bourne of Bell Labs Both a command interpreter and a high- level programming language Typically the default Unix shell FastApproximately 20 times faster than C shell because it is similar.

Page 4: Bash shell scripting

C SHELL (CSH) Written by Bill Joy at University of California at Berkeley Slower and more complex than Bourne shell, but has facilities to make it more user friendlyAliasHistoryJob controlFilename completion

Page 5: Bash shell scripting

KORN SHELL (KSH) Written by David Korn of AT&T, released in 1986 Includes features of both the Bourne and C shell Introduces several new user interface features including command line editing Adds features that improve its usefulness as a programming languageReport formatting capabilitiesBuilt-in arithmeticData types

Page 6: Bash shell scripting

SOME BASH COMMANDS cd (change directory): change the current working directory to ‘directory’

cd… using for one step back.

Page 7: Bash shell scripting

SOME BASH COMMANDS ls (list) : showing listing of the current folders

Page 8: Bash shell scripting

SOME BASH COMMANDS pwd (print working directory): print name of current working directory. cp (copy): Create copy of files and directories

Page 9: Bash shell scripting

SOME BASH COMMANDS mv (move): move from one directory place to another either rename of the current folder/file

Page 10: Bash shell scripting

SOME BASH COMMANDS less: view the contents of a text file one screen at a time. cat: used to in display files on print screen grep: allow you to search one file or multiple file for line that contain a pattern

Page 11: Bash shell scripting

SOME BASH COMMANDS Echo: places a string on the computer terminal.

Page 12: Bash shell scripting

SOME BASH COMMANDS touch: very quick way to create new, empty files. mkdir: make a directory.

Page 13: Bash shell scripting

SOME BASH COMMANDS chmod: allow to access the permissions to the file and directory.

Page 14: Bash shell scripting

SOME BASH COMMANDS rm (remove): use in remove file, directory.

Page 15: Bash shell scripting

SUPERUSER A user with essentially all privilegesOften referred to as root privileges

Allowed access to all files Allowed to run all commands System administrator has superuser privileges Can be very dangerous, all the Unix built in protections are by-passed

Page 16: Bash shell scripting

PASSWORD SECURITY Unfortunately, password security is a necessity Good passwords have several characteristics Minimum of six characters Mixture of alpha and numeric characters Mixture of upper and lower case No real words

Susceptible to a dictionary attack

It is also good practices to avoid: Family names Birthdates Pet’s names Any other personal data

Page 17: Bash shell scripting

WHAT HAPPENS WHEN YOU LOGIN? Unix runs the login programIf it exists, .login script is executed

Then the shell specified in /etc/passwd is executed and user is placed in his HOME directoryIf it exists, .cshrc (if csh is your default shell) is executed

So what’s the difference? Why to initialization files?.login is executed only at login.cshrc is executed every time a new shell is spanwned

Page 18: Bash shell scripting

EXIT OR LOGOUT exit terminates your current shellIf it is also your login shell, exit will exit and logout logout terminates a login shell

Page 19: Bash shell scripting

SCRIPTING BASICS Shell scripts are text files that contains a series of commands or statements to be executed. Shell scripts are useful for:Automating commonly used commands.Performing system administration ad troubleshootingCreating simple applicationsManipulations of text or files.Application prototyping

Page 20: Bash shell scripting

CREATING SHELL SCRIPTS Step 1: Use a text editor such as vi to create a text file containg commands.First line contains the magic “shbang” sequence :#!

#!/bin/bashComments your scripts

Comments start with a#

Create shell scripts which is self documenting. If you this pressing \ key followed by the Enter key on the most keyboards. This will enable you to enter one command that spans multiple lines.

Page 21: Bash shell scripting

CREATING SHELL SCRIPTS CONT. Step 2: Make the script executable$ chmod a+x myscript.sh To execute the new scripts:Place the scripts file in a directory in the executable path –OR-

Specify the absolute path or relative path to the script on the command line.

Page 22: Bash shell scripting

CONTROL STRUCTURES The three types in shell programming:Sequential structures – the program flows one line after anotherSelection structures – code execution based on a logical decision.if, if/else, if/elif/else and conditional operators.

Repetition structures (loos) –code execution is repeated based on a logical decisionfor, while and until

Page 23: Bash shell scripting

CONDITIONAL EXECUTION Commands may be executed conditionally, based on the exit states of the previous command.&& logical AND|| logical OR

Examples:$ grep vikas passwd || echo ‘No vikas!’$cp –a /tmp/*.o . && echo ‘Done!’

This structures can be used in the command line as well.

Page 24: Bash shell scripting

SELECTION STRUCTURES: IF/THEN/ELIF if selection structures execute the body of the structure only if the condition tested is true.if conditionthen statement1 statement2 ………………..

fi

Page 25: Bash shell scripting

SELECTION STRUCTURES: IF/THEN/ELIF CONT. Sometimes, you may wish to specify an alternate action when the condition fails. Here's how it's done. if conditionthen

statement1 statement2 ………………..

else statement3

fi

Page 26: Bash shell scripting

SELECTION STRUCTURES: IF/THEN/ELIF CONT. alternatively, it is possible to test for another condition if the first "if" fails.  if condition1 then

statement1 statement2 …………………

elif condition2 then

statement3 statement4 …………………

elif condition3 then

statement5 statement6 …………………

fi

Page 27: Bash shell scripting

REPETITION STRUCTURES: THE FOR-LOOP The for repetition structure provides a method for iterating, or looping, through a list of values and executing commands on each of these values. A loop requires

a) Start pointProcess/Logic/statements to be repeated

b) Some hint or condition which changed the initial condition which started the loop

c) Termination condition for variable in list-of-values

do commands …

done

Page 28: Bash shell scripting

SELECTION STRUCTURES: THE WHILE-LOOP The while loop structure provides a useful method for performing a set of commands while a condition remains true. The syntax is: while condition

do commands

Done

While loops are known as sentinel structures. An until loop works in exactly the same way, except that it continues to execute as long as the command following the until statement executes successfully; that is it will stop the loop when the command succeeds.

Page 29: Bash shell scripting

Thank You!