Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prime View, Inc.1. 2 Chapter Objectives · In this chapter, you will learn about: · Command history · Output redirection and pipes · Filename metacharacters.

Similar presentations


Presentation on theme: "Prime View, Inc.1. 2 Chapter Objectives · In this chapter, you will learn about: · Command history · Output redirection and pipes · Filename metacharacters."— Presentation transcript:

1 Prime View, Inc.1

2 2 Chapter Objectives · In this chapter, you will learn about: · Command history · Output redirection and pipes · Filename metacharacters · Pathname and command completion · Command sequences and groups - Command substitution · Background process and job control

3 Prime View, Inc.3 Command History · The Korn shell maintains a history list of previous commands - To display the history list use the history command - By default, history displays the 16 most recent commands; e.g., $ history 5 ls -il 6 cp legal legal2... 18 diff list.dat new.dat 19 history $ - The command number and command text are displayed · By default, the Korn shell remembers the 128 most recent commands - To display different portions of the history list: $ history 60- From command 60 on... $ history -60 - 60 most recent commands... $

4 Prime View, Inc.4 Command History (continued) · The r command allows you to re-execute previous commands $ r- Most recent command $ r 100- Command number 100 $ r -2- Second most recent command $ r d- Most recent command beginning with d · The Korn shell supports interactive vi- and EMACS-style command editing - Our Korn shells are set to use vi-style editing - Press Esc to enter vi mode, then press: k, j, /, ? to move "up," "down" the command list h, l to move left, right in the current line x, i, a to delete, insert, append text (remember to press Esc after each insert/append) - Press Enter when done editing -- Often, just k, j are used to select command to reexecute · The Korn shell's vi mode supports nearly all of vi's cursor-movement and line- editing commands - Refer to Appendix C, "vi Tutorial"

5 Prime View, Inc.5 Output Redirection · The output redirection symbol, >, causes the shell to redirect a program's standard output to a file - Causes "good" output to go into the file (almost always) - Error messages (if any) still appear on the terminal - > is a shell metacharacter $ cal 11 1996 > Nov_96 $ more Nov_96 $ history -100 > last_100 $ more last_100 ·” >” causes the shell to - Create the output file, if it doesn't exist -- Requires write permission on the directory - Overwrite the output file, if it already exists -- Requires write permission on the file · “>>” causes the shell to - Create the output file, if it doesn't exist - Append to the output file, if it already exists $ cal 1996 > 96_and_97 $ cal 1997 >> 96_and_97 $ more 96_and_97

6 Prime View, Inc.6 Pipes · The pipe symbol, |, causes the shell to connect a program's standard output to the standard input of another program - Causes "good" output to flow as input of the other program -- Second program must expect to receive standard input! -- more is the only such program we've introduced so far - | is yet another shell metacharacter $ cal 1996 | more... see output one screenful at a time... $ history -100 | more... pipe cal 1996 more stdout from cal stdin for more

7 Prime View, Inc.7 Filename Metacharacters · Filename metacharacters provide shorthand notation for sets of filenames ? - Matches any single character * - Matches any sequence of characters (0 or more) [] - Matches any single character in the set or range bounded by square brackets · Suppose the following files are in your directory: chap1 chap12 chap4 chap7 func.c prog.o chap1.new chap12.new chap4.new chap7.new func.h sort.c chap10 chap2 chap5 chap8 func.o sort.h chap10.new chap2.new chap5.new chap8.new prog sort.o chap11 chap3 chap6 chap9 prog.c chap11.new chap3.new chap6.new chap9.new prog.h · When a filename metacharacter appears in a command-line word, the shell searches for matching files and substitutes the list of matched files in place of the command-line word

8 Prime View, Inc.8 Filename Metacharacters (continued) $ more chap?# same as: more chap1 chap2... chap9 $ rm *.0# same as: rm func.o prog.o sort.o $ more func.[ch]# same as: more func.c func.h $ ls -l *.new# same as: ls -l chap1.new chap10.new... $ more chap[135]# same as: more chap1 chap3 chap5 $ ls -l chap[3-6]*# same as: ls -l chap3 chap3.new chap4... · What about $ rm -i *$ more *[0-9][0-9]* $ ls ???? Pathname and command completion Metacharecters (‘wild cards’) can also be used in pathname or command completition. Suppose you have a subdirectory called my_glorious_scripts_for_this UNIX_ course $ cd my_* will do the trick for you. Depending on the shell version, \ sequence for completion of unambiguous names or = to recuest the list of possible matches might work too.

9 Prime View, Inc.9 Command Sequences and Groups · Commands can be sequenced with “;” $ date > who_now; who >> who_now; more who_now · Commands can be grouped with ( ) - Allows redirection or piping of the entire group $ (date;who) > who_now; more who_now... $ (cal 1996;cal 1997) | more... $

10 Prime View, Inc.10 Filtering with grep Try this command: $ ps -ef |grep bash |grep -v grep Compare with output from ps -ef. What is the difference? Grep in the first pipe filters only for those lines that contain the search string, grep -v in second pipe removes our own process. In effect, we get a list of users running bash shell. Wouldn’t it be nice to have this list sorted? Try pipe it to sort: $ ps -ef |grep bash |grep -v grep|sort What if we do not want to see all users, but want just to know how many of them are logged in? Put grep -c in place of sort in example above. Other way to do this would be to use wc (word count) utility like this: $ ps -ef |grep bash |grep -v grep| wc -l Let’s say we have a big system with many users and want to put this list in the file. $ ps -ef |grep bash |grep -v grep| tee users.lst This way we can also watch output on the screen as it goes to file. Is the file to big? $ split -l 3 users.lst three_line This will split original file by 3 lines pieces named like three_lineaa, three_lineab etc.

11 Prime View, Inc.11 Check the directory listing to confirm. Let’s use grep to find out in what file is the teach’s process: $ grep teach three_line?? Put the files back together again: $cat three_line* > big.list Command Substitution · Suppose we want to search the eight oldest (least recently modified) files in our directory for lines containing "Alice" · Select the right tools for the job $ ls -1t... filenames, 1 column, newest to oldest... $ ls -1t | tail -8... names of eight oldest files... $ ls -1t | tail -8 | grep Alice... file names containing Alice in file name... $

12 Prime View, Inc.12 Command Substitution (continued) · grep is the right tool in the wrong place - Somehow, we need the output of "ls -1t | tail -8" to be treated as arguments to "grep Alice," not as standard input · The standard output of a command or pipeline can be given as arguments to another command using backquotes $ grep Alice `ls -1t | tail -8`... lines containing Alice... $ · The shell executes the command in backquotes first !!! - The shell collect the command's standard output, and substitutes it as arguments to the left-hand command · Standard output of any command can be substituted as arguments to any other command

13 Prime View, Inc.13 Command Substitution (continued) $ date Tue Jan 9 17:51:34 EST 1996 $ echo Today is `date` Today is Tue Jan 9 17:51:34 EST 1996 $ echo Today is `date | awk '{print $2, $3 ",", $6}’` Today is Apr 20, 1999 · The Korn shell provides $(...) as an alternative to the older `...` $ echo Today is $(date | awk '{ print $2, $3 ",", $6 }') Today is Jan 9, 1996 Two words about awk Awk is a scripting language found in practically all UNIX flavors. Detail discussion on awk is far beyond this course. For our purposes we need to know only its general form: awk [options] ‘script’ var=value file(s) For example, get list of all users on your server: $ awk -F: ‘{print “user “ $1}’ /etc/passwd

14 Prime View, Inc.14 Space Around Metacharacters · Whitespace is optional around ;, (...), >, >>, and |, but not around ?, *, and [...] · These two commands are the same $ ls -li>long_out;more long_out · These two commands are not t he same! $ rm [Tt]*

15 Prime View, Inc.15 Starting a Background Process · So far, we've executed programs in foreground - The shell waits for the command to finish before issuing a prompt · Any program can be executed in the background - The shell starts the program, then issues a prompt immediately -- UNIX is multitasking: no need to wait! · Good candidates include programs that - Create their own user interface windows, or - Run for a long time and need no terminal I/O · To execute a program in the background, add “&” at the end of the command line $ xclock & [1] 273 $

16 Prime View, Inc.16 Job Number and PID · xclock's job number and process ID number (PID) are displayed, immediately followed by a prompt · Job numbers are assigned by the shell - Each background job executed by a shell is given a unique job number · PIDs are assigned by the system - Each process currently running on the system has a unique PID Process Status · The ps command displays process status - With no argument, only shows the current shell's processes - Displays PID, controlling terminal, cumulative execution time, and command name $ ps PID TTY STAT TIME COMMAND 18386 p0 S 0:00 ksh 18387 p0 S 0:00 xclock 18388 p0 R 0:00 ps

17 Prime View, Inc.17 Process Status (continued) · To see every process on the system, in detail, try $ ps -ef# System V $ ps -aux# BSD · Output will be long; good idea to pipe trough “more” - Most processes are systems daemons $ ps -aux USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND alex 18386 0.1 1.5 928 472 p0 S 17:19 0:00 ksh alex 18387 0.2 3.8 2200 1200 p0 S 17:19 0:00 xclock alex 18428 0.0 1.7 920 536 p0 R 17:21 0:00 ps -aux … Try to get process ID for teach’s shell. Hint: pipe ps to grep

18 Prime View, Inc.18 Killing Processes · The intr and quit characters (Ctrl-C and Ctrl-\, on our systems) cause the tty driver to generate INTerrupt and QUIT signals, respectively, to foreground processes only - Usually kills the current foreground job - Background jobs are not sent the signals · To kill background jobs, use the kill command - By default, kill sends a TERMinate signal to a process -- Usually kills the process - Give PID(s) as argument(s) - Unless you are the super-user, you can only kill your own processes $ kill 18387# kills xclock, if 18387 is PID

19 Prime View, Inc.19 Killing Processes (continued) · Some processes ignore TERMinate; e.g., the shell · Processes cannot ignore “KILL”, signal number 9 (INTerrupt is 2, QUIT is 3, TERMinate is 15, etc.) · kill -9 - is a sure kill - Many systems allow kill -KILL · When a terminal "freezes" - Log in on another terminal, with the same user name - Use ps to get the PID of the hung terminal's shell - Do kill -9 on the shell · Do not use kill -9 on database, mail, or lineprinter processes unless no alternative exists - Can corrupt synchronized data files

20 Prime View, Inc.20 Job Control · Job control originated in the C shell under BSD - The Korn shell supports job control if the underlying operating system does; some older SVR3-based systems do not · Each command or pipeline is a job; jobs may be stopped, started in the foreground or background, moved between foreground and background, or killed · To stop (or suspend) a job, press the tty driver's susp character (usually CTRl-Z); then use jobs- Display current jobs bg %N- Run job N in the background fg %N- Run job N in the foreground kill %N- Send TERMinate to job N

21 Prime View, Inc.21 Who are you, where are you? OK, it can happen to everybody. You got lost. What user ID you are and on what machine? $ whoami ikovalev $ who am i ikovalev /dev/pts/93 Mar 11 21:25 Feel better already. $ where am i ksh: where: not found Oops, watch your language! $ hostname freenet.nether.net Isn’t it nice command? Let’s see if we can find out more about it. $ which hostname /bin/hostname $ whereis hostname hostname: /etc/hostname.le0 /etc/hostname.le1 /usr/bin/hostname /usr/ucb/hostname $ ls -l /usr/bin/hostname /usr/ucb/hostname -r-xr-xr-x 1 bin bin 525 Oct 6 1998 /usr/bin/hostname lrwxrwxrwx 1 root root 15 Nov 14 1999 /usr/ucb/hostname ->../bin/hostname

22 Prime View, Inc.22 Chapter Summary In this chapter, you have learned about · Command historyhistory r vi-style command editing · Output redirection and pipes> >> | · Filename metacharacters* ? [...] · Pathname and command completion · Command sequences, groups; (...) - Command substitution ` ` · Background processes& ps kill · Job controlsusp character (Ctrl-Z) jobs fg bg kill - grep, sort, wc, tee, split - whoami, who am i, whereis, which, hostname


Download ppt "Prime View, Inc.1. 2 Chapter Objectives · In this chapter, you will learn about: · Command history · Output redirection and pipes · Filename metacharacters."

Similar presentations


Ads by Google