
Shellscript Programming Using bash Shell • Program that interacts with the user to receive requests for running programs and executing them – Most of the Unix shells provide character-based user interface to the computer – Other interfaces are Graphic User Interface (GUI) and touch-screen interface (ATM) • Can be used as an interpreted programming language – The programs written in shell command language do not have to be compiled (like the C programs) but can be executed directly • Useful features of the shell – Metacharacters – Quoting – Creating new commands – Passing arguments to the commands – Variables – Control flow bash • Bourne Again shell – Bourne shell was the original shell on Unix ∗ Developed by and named after Stephen R. Bourne at Bell Labs • Useful for both interactive use as well as writing shellscripts • Home directory – The default area to store a user’s files; individual to the user – The first directory the user encounters after logging in – Typically stores all the configuration files for the user – Tilde (˜) notation ∗ Used to abbreviate home directories ∗ Home directory of username johndoe is abbreviated as ˜johndoe/ ∗ Your own home directory is abbreviated as ˜/ • Referring to multiple files using shell metacharacters – File names can be considered to be patterns of characters – Shell metacharacters ? Any single character * Any string (including empty string) [...] Any character in the set [!...] Any character not in the set – Metacharacters described above are used to match filenames and are interpreted by shell before handing over to the command as parameters Shellscript Programming 2 – ! in the shell can be matched literally by escaping it using the metacharacter \ – A range of characters can be specified by using - [a-e] Match any of abcde [a-cx-z1-5] Match any of abcxyz12345 – Process of matching metacharacters is also known as wildcard expansion or globbing • Brace expansion – Mechanism to generate arbitrary strings – Similar to metacharacter expansion as above but the filenames generated need not exist – Patterns in the form of an optional preamble followed by a set of comma-separated strings enclosed in braces followed by an optional postscript – Examples ∗ a{r,c,sse}t gives art act asset ∗ {a..e} gives a b c d e ∗ {0..20..3} gives 0 3 6 9 12 15 18 ∗ {a..z..4} gives a e i m q u y Metacharacters • Characters that have special meaning to the shell • Most notable metacharacter is the asterisk or * • An asterisk, by itself, matches all the files in the current working directory $ echo * • What happens when you issue the following command echo 2 * 3 > 5 is a valid inequality • Metacharacters can be protected from interpretation by the shell by using quotes $ echo ’***’ • You can also use double quotes but shell peeks inside the double quotes to look for $, ‘ ... ‘ (back quotes), and \ • You can also protect metacharacters by preceding them with a backslash or escape character $ echo \*\*\* – A backslash is the same as using the single quotes around a single character – The backslash character can be used to quote itself as \\ $ echo abc\\def abc\def $ – A backslash at the end of the line causes the line to be continued (as if the newline character is not there) • Quoted strings can contain newlines $ echo ’hello > world’ Shellscript Programming 3 Table 1: Other metacharacters > redirect stdout to a file >> append stdout to a file < take stdin from a file | pipeline <<str stdin follows, upto next str on a line by itself * match any string of zero or more characters ? match any single character in the filenames [ccc] match any single character from ccc in the filenames ranges such as [0-9] and [a-z] are legal ; terminate command & terminate command and run it in the background ‘...‘ run command(s) in ... (...) run command(s) in ... as a subshell {...} run command(s) in ... in current shell $1, $2 $0 ... $9 replaced by arguments to shell file $var value of shell variable var ${var} value of shell variable to be concatenated with text \c take character c literally (suppress newline in echo) ’...’ take ... literally "..." take ... literally, but after interpreting $, ‘...‘, and \ # beginning of comment (ends with the end of line) var=value assignment (no spaces around operator) p1 && p2 run p1; if successful, run p2 p1 || p2 run p1; if unsuccessful, run p2 • The > character above is secondary prompt stored in variable PS2 and can be modified to preference • The metacharacter # starts a comment only if it is placed at the beginning of a word (following whitespace) • The newline following the echo command can be suppressed with the \c option (It is also done by using the -n option on other UNIX variants) • The newline following the echo command can be suppressed with the -n option • Metacharacter interpretation – Metacharacters are interpreted by shell rather than the filesystem – All the major current shells follow the same metacharacter conventions ∗ Exceptions to the convention may occur when you use a special purpose shell such as DCL-like shell – The metacharacters are interpreted by the shell before a program has an opportunity to see them ∗ This rule ensure that the command find . -name *.dvi -print is interpreted as find . -name foo.dvi fubar.dvi -print when the files foo.dvi and foobar.dvi exist in the directory where the command is executed ∗ The intent of the command is preserved by delaying the interpretation by protecting the metacharacter using quotes, such as find . -name ’*.dvi’ -print Command line structure Shellscript Programming 4 • Each command is a single word that names a file to be executed • Example $ who • Command ends with a newline or a semicolon $ date ; who • Sending the output of the above through a pipe $ date ; who | wc • Pipe takes precedence over the semicolon – Semicolon separates two commands – Pipe connects two commands into one • The precedence can be adjusted by using the parenthesis $ ( date ; who ) | wc • Data flowing through a pipe can be intercepted by the command tee to be saved in a file $ ( date ; who ) | tee save | wc tee copies the input to the named file, as well as to the output • Commands can also be terminated by an ampersand (&) – Useful to run long running commands in the background $ ( sleep 5 ; date +"%D" ) & date • Precedence rules (again) – & has a lower priority compared to both | and ; • The special characters interpreted by the shell (<, >, |, ;, and &) are not arguments to the programs being run, but control the running of those programs • The command $ echo Hello > junk can also be written as $ > junk echo Hello • Avoiding overwriting files by accident – Set the noclobber option by set -o noclobber to avoid overwriting a file by accident during output redirection – If you have already set noclobber and want to overwrite a file still, you have to follow the redirection symbol > with a pipe (|) as Shellscript Programming 5 echo hello >| junk Control keys • Special characters with meaning to the shell • Don’t print anything but may affect the functioning of your input Key Action ˆM Enter or Return ˆ? Delete ˆH Backspace ˆU Kill line ˆW Erase last word ˆC Interrupt ˆZ Suspend process ˆD End of input • Use stty -a to see the mappings Getting help on bash builtins • Use the command help • Does not replace man Command line editing • Enabled by default when you start bash – Can be disabled by starting bash with option -noediting • Editor used to edit the command line can be customized between vi and emacs by one of the following commands set -o vi set -o emacs • Command history – Each command is recorded as you execute it in the file ˜/.bash_history – The history is read (and preserved) when you start bash • Textual completion (in emacs mode) Customizing bash • Performed by a startup file called .bash_profile in your home directory – If .bash_profile does not exist, bash will look for .profile, bash_profile, or .bashrc – .bash_profile is sourced for login shells – .bashrc is sourced for login as well as non-interactive shells Shellscript Programming 6 • Effectively, you will customize the environment in which you execute commands • Environment – Collection of internal variables that can help in customization of look and feel of the shell, as well as allow the machine to respect your privacy – Handled by environment variables – Issue the command set with no parameters to see the environment variables • Ownership – Ownership of files, resources, and processes is determined by UID and GID – Shell picks it up when you log in from your authentication record – A closely related set is that of effective UID and effective GID – Used to find permissions to read/write/execute files • File creation mask – Created by umask command – Used to set default values for file permissions as the files get created – Specified as the complement of desired octal permission values ∗ If you want to have the permissions as rwxr-xr-x or 755, the umask value will be 777 − 755 or 022 • Working directory – Default directory for work of a process – Kept in the variable PWD – Changed by the command cd – Default assigned as your home directory, kept in variable HOME Aliases • Allow renaming of existing commands and to create new commands • Created by using the command alias $ alias name=command – Syntax dictates that there is no space on either side of = • Removed by using the command unalias • It is a good idea to add aliases to your .bashrc • The command alias without any arguments lists your current aliases and the commands assigned to them • Examples – Make the -i option a default on the command to delete files
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages22 Page
-
File Size-