Presentation is loading. Please wait.

Presentation is loading. Please wait.

The UNIX Shell Learning Objectives:

Similar presentations


Presentation on theme: "The UNIX Shell Learning Objectives:"— Presentation transcript:

1 The UNIX Shell Learning Objectives:
To give detailed description for using commands in Unix Shell To introduce more advanced techniques for handling complicate commands in Unix Shell To understand the the usage of background jobs & shell switching To learn more about pattern matching

2 How Does the Shell Find a Command?
The shell searches a list of directories for an executable file with the same name. $ echo $path shows you the current path The list of directories is stored in the PATH variable for Bourne shells and in the path array for csh/tcsh $ PATH=/usr/local/bin:$PATH sh % set path=(/usr/local/bin $path) csh, tcsh If there is a match in more than one directory, the shell uses the first one it finds. It is usually set in your .cshrc file in your home directory

3 Alias The C Shell has the alias command, which allows you to create command shortcuts. $ alias dir "ls -F" $ alias rm "rm -i" $ alias + "chmod u+x *" $ alias - "chmod u-x *" $ alias 111 "cd ~horner/111" $ pwd /bin $ 111 /homes/horner/111 If you put the alias commands in your .cshrc file, you can use them every time you login.

4 Tee A special command called tee acts like a T-joint in plumbing:
$ who horner pts/3 Feb 11 10:23 (csnt1.cs.ust.hk) horner pts/0 Feb 11 11:57 (csz096.cs.ust.hk) $ who | sort | tee sortedwho | wc -l 2 $ cat sortedwho $ In this example, the output of sort is placed in a file “sortedwho“ and piped to wc -l, which counts the number of lines.

5 Background Jobs A simple command or pipeline can be put into the background by following it with the “&” character: $ sort names > names.sort & [1] 3236 The shell will print the process ID (PID), and a job number (1, in this case). Put a job in the background by typing “CTRL-Z” $ ypcat passwd | sort >passwd.sort ^Z Suspended The job is suspended - not running - until you either place it in the background using bg: $ bg [1] ypcat passwd | sort > passwd.sort & or back to the foreground using fg: $ fg ypcat passwd | sort >passwd.sort

6 Jobs You can stop a job with the kill command:
$ ypcat passwd | sort > passwd.sort & [1] $ kill %1 [1] Terminated ypcat passwd | Exit 2 sort > passwd.sort The “%1” means “job #1”. You can also use the PID. The ps command is the main way to find out about jobs: [1] $ ps PID TTY TIME CMD 3477 pts/0 0:00 sort 1401 pts/0 0:01 csh 3476 pts/0 0:01 ypcat $

7 Combining Commands (1) Multiple pipelines can be input on one command line by separating them with semicolons. When entering a long command, use a backslash (\) to continue the command on the next line. $ date; sort names; \ who Thu Feb 11 19:40:28 HKT 1999 Bill Clinton Bill Gates Monica Lewinski horner pts/ Feb 11 10:23 (csnt1.cs.ust.hk) horner pts/ Feb 11 19:11 (csz096.cs.ust.hk)

8 Combining Commands (2) Commands can be grouped together using parentheses There are two main reasons to group commands: To create a “single command” out of a group of commands (especially useful before a pipe): $ (cat names; head -2 names) | wc –l # if names has 8 lines, what does this line return? To run a set of commands in their own subshell (especially when trying to limit the effect of a cd command): $ (cd secret; ls | wc -l); ls | wc -l 3 25 This line has the effect of counting the files in secret, and then counting the files in the current directory. Question: what does this do? cd ; cd 111; (cd ..;cd 122); cd ..

9 cut and tr cut -cx-y cuts characters from position x to position y (the first position is 1). -r--r--r horner cs Feb 12 16:00 letter1 What does this do? ls -l | sort +4 | tail -1 | cut -c55-70 The following command returns the user name who am i | cut -f1 -d' ‘ -f1 specifies the first field Field is defined by the delimiter ‘ ‘ The following command translates all lower case to upper case tr “[a-z]” “[A-Z]” filename tr means translate To compress all spaces tr -s '[ ]*' ' ‘ Who | tr –s ‘[ ]*’ ‘ ‘ | cut –d’ ‘ –f2

10 Patterns The pattern “[abcd]” matches any single one of the enclosed characters. The following “ls” command lists any file whose name start with “i” or “l” followed by any other characters $ ls [il]* it it ith letter1 letter4 $ * matches anything The notation “[a-z]” matches any lowercase letter once. The notation “[0-9]” matches any digit character once. The notation “[0-59]” matches any the digit characters 0, 1, 2, 3, 4, 5, and 9. $ ls letter* letter1 letter4 $ ls letter[0-35] letter1 $ ls letter[0-24]

11 More Pattern Matching – grep (1)
grep finds all matches of a pattern in a file : To build a pattern, use the following: ^ start of line $ end of line . any character \ quote next character * match zero or more + match one or more ? match zero or one [aeiou0-9] match a,e,i,o,u, and 0 thru 9 [^aeiou0-9] match anything but a,e,i,o,u, and 0 thru 9 ‘a*’ match zero or more occurrences of ‘a’ ‘a+’ match one or more occurrences of ‘a’ (egrep)

12 More Pattern Matching – grep (2)
$ cat names Bill Gates Jacky Gs James Bill J. Jones $ grep '^Bill’ names $ grep 'Bill$’ names $ grep ’J...s’ names $ grep 'G.*s' names Bill Gates Jacky Gs $ grep 'G.+s' names $ egrep 'G.+s' names $ egrep 'G.s' names $ egrep 'G.?s' names J. Jones $ grep 'J\.' names Some versions of grep cannot process ?+

13 More Pattern Matching – grep (3)
$ cat names Bill Gates Jacky Gs James Bill J. Jones $ grep 'Ja[cm]' names $ grep 'J[^a]' names $ grep ’[t-y]' names $ grep -in bill names 1:Bill Gates 3:James Bill $ grep -v Bill names Jacky Gs J. Jones $ grep -l Bill n* names $ grep -c Bill names 2 -i ignores case -n shows line number -v means line *not* containing pattern -l shows *filename* containing pattern -c shows matching count

14 How to do this? Find all lines in ‘file1’ that does not contain any vowels Hint: use –v option Find all lines in ‘file1’ that contains only vowels. Hint: use ^string$ Find all lines in file1 with length 7 and ‘a’ as the fourth character Find all lines that contain ‘a’, ‘c’, and ‘e’, in that order


Download ppt "The UNIX Shell Learning Objectives:"

Similar presentations


Ads by Google