A Breif Intro to the Korn Shell
Total Page:16
File Type:pdf, Size:1020Kb
The Unix Shell When you login, a program called the Unix shell is run. The Shell is a command interpreter that provides you with an interface to the operating system. There is more than one shell program available for use. The most popular ones in use are the · C shell (csh) - Berkeley Unix; so known because it's commands are C like · Bourne shell (sh) - Unix System V (developed by Steve Bourne at Bell Laboratories) · Korn shell (ksh) - extends the Bourne shell (developed by David Korn at Bell Laboratories) · GNU Bourne-Again SHell (bash) - extends the Bourne shell and also has features from the C Shell and Korn shell You can determine what shell you are running by executing the ps (process status) command; most likely, you will be using the Korn shell. The chsh command changes the login shell of your username; it's not something you want to do casually. When altering a login shell, the chsh command tells you the current login shell and then prompts for the new one. The new login shell must be one of the approved shells listed in the /etc/shells file (if you have superuser privileges, you can of course use some shell of your own creation). You can run any of these shells from within your login shell; e.g., csh puts you into the C shell. You have to know how to exit the shell that you are in. For the C shell it's the exit command, just as for the Korn shell (the C shell uses a % prompt rather than a $ prompt, which helps you remember you are in it). Note that you need to use caution, because you can run shell within shell (even Korn shell within Korn shell) … this can be useful as a temporary step-aside to do some system thing, then return. File Redirection Most commands output to "standard out", which by default is the terminal. Some of them also take their input from "standard in", which by default is the terminal. In using commands, the terminal (usually pointed to by stdin and stdout) can be replaced by a file rather simply using a shell feature known as redirection, which is a means of instructing the shell to use something other than the default for standard input or standard output. The formats for standard in and standard out are: <command> < <file-name> for <command> to get its input from <file-name> rather than standard in. <command> > <file-name> for <command> to send its output to <file-name> rather than standard out. <command> >> <file-name> for <command> to append its output to <file-name>. (there is also a format for "standard error", the 3rd "standard" file associated with commands). Redirection can be surprisingly useful; for example, using the shell command sort (which does nothing more than sort a file), you can just enter sort and then doing standard input from the terminal enter lines until you finally enter an EOF (<Ctrl> d) at which point the command will sort your input and output it to standard output. With redirection, you can do things like sort < myfile > mysortedfile (or (sort < myfile) > mysortedfile if you prefer) to sort a text file. Two commands can be executed from a single line; e.g., date; cal To capture all of standard out for this construction via redirection you must use parentheses; e.g., (date;cal) > myfile Pipes Redirection involves providing a shell command with an alternative for standard in and/or standard out. You can't take standard out and turn it directly into standard in for another command without going through an intermediate file; for example, the intent of doing sort < who would need to be accomplished by who > temp sort < temp To get around this need to pass data through temporary files, the shell provides a means of taking standard out from one command and making it standard in for another. This is called a pipe. The notation <command-1> | <command-2> is used to denote that standard out from <command-1> is to be piped to <command-2> as standard in. For the above example, you would simply enter who | sort or more practically, perhaps who | sort | pg (standard out from who is piped to sort as standard in from which standard out is piped to pg as standard in at which point standard out is the terminal). You can of course do things such as sort < myfile | pg which would allow you to examine a sorted version of the file without creating a permanent, sorted version of your data. If for some reason you want to capture intermediate information flowing through a pipe, there is a utility provided for this purpose (it is not a shell command). In particular, who | sort | tee whosorted | pg does the same thing as the earlier construction, except the sort output is "teed" into whosorted as well as to standard out. tee is not a shell command since it is only used on the receiving end of a pipe. sort is an example of a type of shell command called a filter. A filter is a command that takes data from standard in and performs some simple transformation on it, the result of which is sent on to standard out. Examples: · sort - sort standard in · grep (and its derivatives) - search for keyword information · head - send on only the front end · tail - send on only the tail end · wc - count words, lines, and/or characters · crypt - encrypt standard in (use with caution!) We've already looked at sort. grep (global regular expression - print) is a shell command that matches patterns represented by limited regular expressions to the standard in character stream. The related command, egrep, allows for the full range of regular expressions (regular expressions are covered in detail in compilers). grep interprets the same regular expressions as the basic Unix editor, ed. The story (Kernighan and Pike) is that grep was actually created in an evening by doing a little surgery on ed! grep naturally includes simple pattern matching is used to locate occurrences of a single word; e.g., grep -n 'symbval' pass2.c gives the line numbers and prints the lines containing the specific symbol "symbval". Metacharacters are used to represent the general form and format of regular expressions; e.g. ^ for the beginning of a line; e.g., '^t' = lines beginning with "t". $ for the end of a line; e.g., 't$' = lines ending with "t". matches any single character; '^.t' = lines with 2nd character "t". * goes with the preceding character to represent 0 or more repetitions of the character. + is like *, but is for 1 or more repetitions (egrep only). \ turns off any special meaning for the following character. (Filters, Cont'd) Additionally, some constructions in the regular expression are permitted; e.g., [...] to match any character listed; allows ranges such as a-x. [^...] to match any character not listed; also allows ranges. <r1><r2> for regular expression 1 followed by regular expression 2. <r1>|<r2> for regular expression 1 or regular expression 2 (egrep only). (<r>) regular expressions can be nested (egrep only). You could almost go to school on grep. For example, here's a typical entry from the system password file /etc/passwd cwinton:ff5uUzurbujiM:121:101:Charles N Winton:/home/cwinton:/bin/ksh The following shell command searches this file for users without passwords: grep '^[^:]*::' /etc/passwd (^[^:]* = the string is lead by 0 or more characters not ":" that are followed by two ":" characters in succession; i.e., there is no entry in the password position). Remark: competent system administration will not allow accounts without passwords; note that while the password file is completely accessible (it has to be), encryption protects the passwords. If two users should be using the same password, the encryption routine will encrypt them differently. Since decryption is capable of breaking passwords if given enough time, the best advice is to change your password regularly. head -15 myfile lists the first 15 lines of myfile tail -15 myfile lists the last 15 lines of myfile If no number is specified, the default is 10. wc -lwc myfile counts lines, words, and characters in the file. If any one of l, w, or c is omitted, that count is not provided. (Filters, Cont'd) crypt <password> < myfile > cryptedmyfile inputs myfile and encrypts it using the "<password>" supplied, storing the encrypted file in cryptedmyfile. crypt <password> < cryptedmyfile reverses the encryption. In either case, if no password is given (i.e., you don't want it visible), the user is prompted to enter one. There are further generalizations of grep, most notably awk, that are considered to be "programmable filters", because the transformation is constructed as a program in a simple language. awk is named after its authors, Aho, Kernighan, and Weinberger, all of Bell labs. One of the programmable filters commonly used is sed, the streaming version of the basic Unix text editor, ed. Editor commands that do not process multiple lines or look backward will generally work with sed, which processes its input line by line as an input stream, applying the commands stipulated. For example, sed '/./s/^/<tab>/' <file-name> will send the file to standard out with each line indented by a tab. This works as follows: the initial “.” is a regular expression for any character, so if the line has none, it is not processed; otherwise, ed’s search and replace (s) is applied, replacing the null front of the line given by the regular expression /^/ with a tab. Unless specifically instructed not to (with the –n option), sed sends each line as it is processed to standard out.