A Breif Intro to the Korn Shell

Total Page:16

File Type:pdf, Size:1020Kb

A Breif Intro to the Korn Shell 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.
Recommended publications
  • Unix Introduction
    Unix introduction Mikhail Dozmorov Summer 2018 Mikhail Dozmorov Unix introduction Summer 2018 1 / 37 What is Unix Unix is a family of operating systems and environments that exploits the power of linguistic abstractions to perform tasks Unix is not an acronym; it is a pun on “Multics”. Multics was a large multi-user operating system that was being developed at Bell Labs shortly before Unix was created in the early ’70s. Brian Kernighan is credited with the name. All computational genomics is done in Unix http://www.read.seas.harvard.edu/~kohler/class/aosref/ritchie84evolution.pdfMikhail Dozmorov Unix introduction Summer 2018 2 / 37 History of Unix Initial file system, command interpreter (shell), and process management started by Ken Thompson File system and further development from Dennis Ritchie, as well as Doug McIlroy and Joe Ossanna Vast array of simple, dependable tools that each do one simple task Ken Thompson (sitting) and Dennis Ritchie working together at a PDP-11 Mikhail Dozmorov Unix introduction Summer 2018 3 / 37 Philosophy of Unix Vast array of simple, dependable tools Each do one simple task, and do it really well By combining these tools, one can conduct rather sophisticated analyses The Linux help philosophy: “RTFM” (Read the Fine Manual) Mikhail Dozmorov Unix introduction Summer 2018 4 / 37 Know your Unix Unix users spend a lot of time at the command line In Unix, a word is worth a thousand mouse clicks Mikhail Dozmorov Unix introduction Summer 2018 5 / 37 Unix systems Three common types of laptop/desktop operating systems: Windows, Mac, Linux. Mac and Linux are both Unix-like! What that means for us: Unix-like operating systems are equipped with “shells”" that provide a command line user interface.
    [Show full text]
  • Shell Scripting with Bash
    Introduction to Shell Scripting with Bash Charles Jahnke Research Computing Services Information Services & Technology Topics for Today ● Introductions ● Basic Terminology ● How to get help ● Command-line vs. Scripting ● Variables ● Handling Arguments ● Standard I/O, Pipes, and Redirection ● Control Structures (loops and If statements) ● SCC Job Submission Example Research Computing Services Research Computing Services (RCS) A group within Information Services & Technology at Boston University provides computing, storage, and visualization resources and services to support research that has specialized or highly intensive computation, storage, bandwidth, or graphics requirements. Three Primary Services: ● Research Computation ● Research Visualization ● Research Consulting and Training Breadth of Research on the Shared Computing Cluster (SCC) Me ● Research Facilitator and Administrator ● Background in biomedical engineering, bioinformatics, and IT systems ● Offices on both CRC and BUMC ○ Most of our staff on the Charles River Campus, some dedicated to BUMC ● Contact: [email protected] You ● Who has experience programming? ● Using Linux? ● Using the Shared Computing Cluster (SCC)? Basic Terminology The Command-line The line on which commands are typed and passed to the shell. Username Hostname Current Directory [username@scc1 ~]$ Prompt Command Line (input) The Shell ● The interface between the user and the operating system ● Program that interprets and executes input ● Provides: ○ Built-in commands ○ Programming control structures ○ Environment
    [Show full text]
  • Object Oriented Programming
    No. 52 March-A pril'1990 $3.95 T H E M TEe H CAL J 0 URN A L COPIA Object Oriented Programming First it was BASIC, then it was structures, now it's objects. C++ afi<;ionados feel, of course, that objects are so powerful, so encompassing that anything could be so defined. I hope they're not placing bets, because if they are, money's no object. C++ 2.0 page 8 An objective view of the newest C++. Training A Neural Network Now that you have a neural network what do you do with it? Part two of a fascinating series. Debugging C page 21 Pointers Using MEM Keep C fro111 (C)rashing your system. An AT Keyboard Interface Use an AT keyboard with your latest project. And More ... Understanding Logic Families EPROM Programming Speeding Up Your AT Keyboard ((CHAOS MADE TO ORDER~ Explore the Magnificent and Infinite World of Fractals with FRAC LS™ AN ELECTRONIC KALEIDOSCOPE OF NATURES GEOMETRYTM With FracTools, you can modify and play with any of the included images, or easily create new ones by marking a region in an existing image or entering the coordinates directly. Filter out areas of the display, change colors in any area, and animate the fractal to create gorgeous and mesmerizing images. Special effects include Strobe, Kaleidoscope, Stained Glass, Horizontal, Vertical and Diagonal Panning, and Mouse Movies. The most spectacular application is the creation of self-running Slide Shows. Include any PCX file from any of the popular "paint" programs. FracTools also includes a Slide Show Programming Language, to bring a higher degree of control to your shows.
    [Show full text]
  • Using the GNU Compiler Collection (GCC)
    Using the GNU Compiler Collection (GCC) Using the GNU Compiler Collection by Richard M. Stallman and the GCC Developer Community Last updated 23 May 2004 for GCC 3.4.6 For GCC Version 3.4.6 Published by: GNU Press Website: www.gnupress.org a division of the General: [email protected] Free Software Foundation Orders: [email protected] 59 Temple Place Suite 330 Tel 617-542-5942 Boston, MA 02111-1307 USA Fax 617-542-2652 Last printed October 2003 for GCC 3.3.1. Printed copies are available for $45 each. Copyright c 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being \GNU General Public License" and \Funding Free Software", the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled \GNU Free Documentation License". (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. i Short Contents Introduction ...................................... 1 1 Programming Languages Supported by GCC ............ 3 2 Language Standards Supported by GCC ............... 5 3 GCC Command Options .........................
    [Show full text]
  • Introduction to Unix
    Introduction to Unix Rob Funk <[email protected]> University Technology Services Workstation Support http://wks.uts.ohio-state.edu/ University Technology Services Course Objectives • basic background in Unix structure • knowledge of getting started • directory navigation and control • file maintenance and display commands • shells • Unix features • text processing University Technology Services Course Objectives Useful commands • working with files • system resources • printing • vi editor University Technology Services In the Introduction to UNIX document 3 • shell programming • Unix command summary tables • short Unix bibliography (also see web site) We will not, however, be covering these topics in the lecture. Numbers on slides indicate page number in book. University Technology Services History of Unix 7–8 1960s multics project (MIT, GE, AT&T) 1970s AT&T Bell Labs 1970s/80s UC Berkeley 1980s DOS imitated many Unix ideas Commercial Unix fragmentation GNU Project 1990s Linux now Unix is widespread and available from many sources, both free and commercial University Technology Services Unix Systems 7–8 SunOS/Solaris Sun Microsystems Digital Unix (Tru64) Digital/Compaq HP-UX Hewlett Packard Irix SGI UNICOS Cray NetBSD, FreeBSD UC Berkeley / the Net Linux Linus Torvalds / the Net University Technology Services Unix Philosophy • Multiuser / Multitasking • Toolbox approach • Flexibility / Freedom • Conciseness • Everything is a file • File system has places, processes have life • Designed by programmers for programmers University Technology Services
    [Show full text]
  • C Shell Scripts File Permissions a Simple Spelling Checker Command
    Cshell scripts Asimple spelling checker mylatex1.csh #!/bin/csh #!/bin/csh tr -cs "[:alpha:]" "[\n*]" < $1 \ latex file.tex |tr"[:upper:]" "[:lower:]" \ dvips -f file.dvi > file.ps |sort -u > tempfile rm file.aux file.dvi file.log comm -23 tempfile /usr/share/dict/words rm tempfile mylatex2.csh #!/bin/csh Put one word per line \ |convert everything to lowercase \ latex $1.tex |sor t the words,remove duplicates and write the result to ’tempfile’. dvips -f $1.dvi > $1.ps rm $1.aux $1.dvi $1.log Pr int those words in ’tempfile’ that are not in the dictionary. Remove the temporar y file. Graham Kemp,Chalmers University of Technology Graham Kemp,Chalmers University of Technology File permissions Command substitution in a script ls -l To include the output from one command within the command line for another command, enclose the command whose output is to be included rwxrwx rwx within ‘backquotes‘. For example: rwxr -xr -x #!/bin/csh 111101101 echo Date and time is: date echo 1111 011 012 = 7558 echo "Your username is:" ‘whoami‘ echo "Your current directory is:" ‘pwd‘ chmod 755 file.sh chmod u+x file.sh chmod -R a+rX . Graham Kemp,Chalmers University of Technology Graham Kemp,Chalmers University of Technology Editing several files at once (1) sed scripts Suppose wewant to change ‘‘cie’’to‘‘cei’’inall files in the current grep href publications.html \ director y whose name ends with ‘‘.tex’’. |sed ’s/[ˆ"]*"//’ \ |sed ’s/".*//’ #!/bin/csh Instead of giving a single editing command on the command line,wecan ls *.tex | sed ’s/.*/sed s\/cie\/cei\/g & > &tmp/’ > s1 create a script file containing a sequence of editing commands.
    [Show full text]
  • Shell Script & Advance Features of Shell Programming
    Kirti Kaushik et al, International Journal of Computer Science and Mobile Computing, Vol.4 Issue.4, April- 2015, pg. 458-462 Available Online at www.ijcsmc.com International Journal of Computer Science and Mobile Computing A Monthly Journal of Computer Science and Information Technology ISSN 2320–088X IJCSMC, Vol. 4, Issue. 4, April 2015, pg.458 – 462 RESEARCH ARTICLE Shell Script & Advance Features of Shell Programming Kirti Kaushik* Roll No.15903, CS, Department of Computer science, Dronacharya College of Engineering, Gurgaon-123506, India Email: [email protected] Jyoti Yadav Roll No. 15040, CS, Department of Applied Computer science, Dronacharya College of Engineering, Gurgaon-123506, India Email: [email protected] Kriti Bhatia Roll No. 15048, CS, Department of Applied Computer science, Dronacharya College of Engineering, Gurgaon-123506, India Email: [email protected] Abstract-- In this research paper, the idea of shell scripting and writing computer programs is examined and different parts of shell programming are likewise contemplated. A shell script is a PC system intended to be controlled by the UNIX shell which is a charge line translator. The different tongues of shell scripts are thought to be scripting dialects. Regular operations performed by shell scripts incorporate document control, program execution, and printing content. A shell script can give an advantageous variety ofa framework order where unique environment settings, charge alternatives, or post-transforming apply naturally, yet in a manner that permits the new script to still go about as a completely typical UNIX summon. The real ideas like Programming in the Borne and C-shell, in which it would be clarified that how shell programming could be possible in Borne and C-shell.
    [Show full text]
  • “Linux at the Command Line” Don Johnson of BU IS&T  We’Ll Start with a Sign in Sheet
    “Linux at the Command Line” Don Johnson of BU IS&T We’ll start with a sign in sheet. We’ll end with a class evaluation. We’ll cover as much as we can in the time allowed; if we don’t cover everything, you’ll pick it up as you continue working with Linux. This is a hands-on, lab class; ask questions at any time. Commands for you to type are in BOLD The Most Common O/S Used By BU Researchers When Working on a Server or Computer Cluster Linux is a Unix clone begun in 1991 and written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. 64% of the world’s servers run some variant of Unix or Linux. The Android phone and the Kindle run Linux. a set of small Linux is an O/S core programs written by written by Linus Richard Stallman and Torvalds and others others. They are the AND GNU utilities. http://www.gnu.org/ Network: ssh, scp Shells: BASH, TCSH, clear, history, chsh, echo, set, setenv, xargs System Information: w, whoami, man, info, which, free, echo, date, cal, df, free Command Information: man, info Symbols: |, >, >>, <, ;, ~, ., .. Filters: grep, egrep, more, less, head, tail Hotkeys: <ctrl><c>, <ctrl><d> File System: ls, mkdir, cd, pwd, mv, touch, file, find, diff, cmp, du, chmod, find File Editors: gedit, nedit You need a “xterm” emulation – software that emulates an “X” terminal and that connects using the “SSH” Secure Shell protocol. ◦ Windows Use StarNet “X-Win32:” http://www.bu.edu/tech/support/desktop/ distribution/xwindows/xwin32/ ◦ Mac OS X “Terminal” is already installed Why? Darwin, the system on which Apple's Mac OS X is built, is a derivative of 4.4BSD-Lite2 and FreeBSD.
    [Show full text]
  • A Multiplatform Pseudo Terminal
    A Multi-Platform Pseudo Terminal API Project Report Submitted in Partial Fulfillment for the Masters' Degree in Computer Science By Qutaiba Mahmoud Supervised By Dr. Clinton Jeffery ABSTRACT This project is the construction of a pseudo-terminal API, which will provide a pseudo-terminal interface access to interactive programs. The API is aimed at developing an extension to the Unicon language to allow Unicon programs to easily utilize applications that require user interaction via a terminal. A pseudo-terminal is a pair of virtual devices that provide a bidirectional communication channel. This project was constructed to enable an enhancement to a collaborative virtual environment, because it will allow external tools such to be utilized within the same environment. In general the purpose of this API is to allow the UNICON runtime system to act as the user via the terminal which is provided by the API, the terminal is in turn connected to a client process such as a compiler, debugger, or an editor. It can also be viewed as a way for the UNICON environment to control and customize the input and output of external programs. Table of Contents: 1. Introduction 1.1 Pseudo Terminals 1.2 Other Terminals 1.3 Relation To Other Pseudo Terminal Applications. 2. Methodology 2.1 Pseudo Terminal API Function Description 3. Results 3.1 UNIX Implementation 3.2 Windows Implementation 4. Conclusion 5. Recommendations 6. References Acknowledgments I would like to thank my advisor, Dr. Clinton Jeffery, for his support, patience and understanding. Dr. Jeffery has always been prompt in delivering and sharing his knowledge and in providing his assistance.
    [Show full text]
  • Seashore Guide
    Seashore The Incomplete Guide Contents Contents..........................................................................................................................1 Introducing Seashore.......................................................................................................4 Product Summary........................................................................................................4 Technical Requirements ..............................................................................................4 Development Notice....................................................................................................4 Seashore’s Philosophy.................................................................................................4 Seashore and the GIMP...............................................................................................4 How do I contribute?...................................................................................................5 The Concepts ..................................................................................................................6 Bitmaps.......................................................................................................................6 Colours .......................................................................................................................7 Layers .........................................................................................................................7 Channels ..................................................................................................................
    [Show full text]
  • Chapter 1 Introducing Python
    3 Chapter 1 Introducing Python 1.1: Introduction Python is a programming language that is both simple and powerful. For those who have struggled with learning programming languages in the past, this may come as a pleasant surprise. This chapter describes some of the main features of Python and its use as a programming language to write scripts for ArcGIS. The logic and struc- ture of this book and the accompanying exercises is described, followed by some examples of how Python is used. The final part of this chapter intro- duces Python editors that make it easier to write and organize your code. 1.2: Exploring the features of Python Python has a number of features that make it the programming language of choice for working with ArcGIS. Among them: It’s simple and easy to learn: Python is easy to learn compared with other highly structured programming languages like C++ or Visual Basic. The syntax is simple, which gives you more time to focus on solving problems than having to learn the language itself. It’s free and open source: Python is free and open source software ( FOSS ). You can freely distribute copies of the software, read the source code, make changes to it, and use pieces of it in new free programs. One of the reasons Python works so well is that it has been created, and is constantly being improved, by an active and dedicated user community. The FOSS nature of Python makes it possible for Esri to distribute Python with ArcGIS software. It’s cross platform: Python is supported on different platforms, includ- ing Windows, Mac, Linux, and many others.
    [Show full text]
  • Examining the Hamilton C Shell
    EXAMINING ROO M Examining the Hamilton C Shell Unix power for OS/2 Scott Richman tarting OS/2 for the first time was, file system), long filenames, and threads. in the background and the server will for me, like unlocking a Ferarri, Additionally, the Shell supports large be kept busy. sitting behind its wheel and find­ command lines and pipes (up to 64K) ing a Yugo's dash. What a disap­ and includes faster and more powerful Supporting Procedures pointment. Sure, the engine and utilities than those supplied with OS/2. Script programmers can create C Shell suspensionS were first rate, but the con­ This is more than Unix - this is a pow­ procedures, which are more like func­ trols were minimal, the clutch was stiff, erful requirement for development un­ tions: They accept a parameter list and and the pedals were nonresponsive! der OS/2. The ability to execute C shells return a value. These procedures are OS/2 comes with great stuff, but CMD simultaneously in different Presenta­ compiled into C Shell memory and are .EXE, the default command-line pro­ tion Manager (PM) text windows con­ then executed as new shell commands. cessor, is poor compared to the pow­ verts your PC into a flexible workstation. Procedures can greatly extend the erfuloperating system beneath. CMD.EXE The Hamilton C Shell comes with power and flexibility of your environ­ appears to be a port of the MS-DOS many programs and shell scripts. To ment. COMMAND.COM and lacks the major install the Shell, you simply copy the As an example, consider ZCW.CSH features of a serious front end.
    [Show full text]