User Tools

Site Tools


unix_102

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
unix_102 [2016/05/25 15:21]
peek
unix_102 [2016/06/07 13:57] (current)
peek
Line 350: Line 350:
 Whenever a program exits it returns an exit code to the shell.  An exit code of 0 means that the program exited normally.  A non-zero exit code means that an error occurred.  This is useful information for building conditional commands that may change behavior depending on what errors arise.  For instance, the ''make'' command will execute a list of commands in a file named ''Makefile'', and exit the first time it encounters an error.  Makefiles are often used to generate programs and content.  But for now, it's sufficient for you to know that exit codes exist and that they are useful. Whenever a program exits it returns an exit code to the shell.  An exit code of 0 means that the program exited normally.  A non-zero exit code means that an error occurred.  This is useful information for building conditional commands that may change behavior depending on what errors arise.  For instance, the ''make'' command will execute a list of commands in a file named ''Makefile'', and exit the first time it encounters an error.  Makefiles are often used to generate programs and content.  But for now, it's sufficient for you to know that exit codes exist and that they are useful.
  
-====== File Names, Directory Names, and Wildcards ====== 
  
-Most of the file you will provide a list of files and/or directories for a command on the command line, one at a time and separated by a space.  But what if there are too many pathnames to list one at a time?  Or, conversely, what if you're just too lazy to list them?  Computers were invented to be labor-saving devices, so of course there's a way to make the machine do your work for you.  That's where wildcards come in.  A wildcard is a special character or tag that the shell recognizes as a filter for selection criteria from a directory listing.+====== Playing Around ======
  
-Pattern ^ Matches ^ +Make a safe place to play around || 
-^ <code>*</code> | Used by itself, this will match any file or directory name. <code> +Type: | <code>$ mkdir /tmp/playground 
-ls -1 * +cd /tmp/playground
-Aware_-_Kontinuum.flac +
-Glaciers-SD.mp4 +
-log-messages.txt +
-MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv +
-README+
 </code> | </code> |
-^ <code>G*</code> | Matches any file or directory name that starts with a ''G''<code+Get a text file to play around with || 
-$ ls -1 G* +^ Type: | <code>$ wget -O file.txt 'http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt' 
-Glaciers-SD.mp4+--2016-06-07 09:25:57--  http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespea 
 +re.txt 
 +Resolving ocw.mit.edu (ocw.mit.edu)... 23.15.135.8, 23.15.135.19 
 +Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|:80... connected. 
 +HTTP request sent, awaiting response... 200 OK 
 +Length: 5458199 (5.2M) [text/plain] 
 +Saving to: ‘file.txt’ 
 + 
 +100%[======================================>] 5,458,199   1.58MB/  in 3.3s    
 + 
 +2016-06-07 09:26:00 (1.58 MB/s) ‘file.txt’ saved [5458199/5458199]
 </code> | </code> |
-<code>*.txt</code> Matches any file or directory name that ends with ''.txt''<code> +How many lines are in the file? || 
-ls -1 *.txt +^ Type: | <code>wc -l file.txt 
-log-messages.txt+124456 file.txt
 </code> | </code> |
-<code>file*.txt</code> | Matches any file or directory name that begins with ''file'' and ends with ''.txt''+How many words are in the file? || 
-^ <code>READ???</code> Matches any file or directory name that starts with ''READ'', followed by exactly three more characters. +Type: | <code>wc -w file.txt 
-<code>[abc]*</code> | Matches any name beginning with either an ''a'', a ''b'', or a ''c'', followed by any number of characters. | +901325 file.txt
-^ <code>file-[0-9][0-9]</code> | Matches any name beginning with ''file-'' and followed by exactly two characters. | +
-^ <code>[[:upper:]]*</code> Matches any name beginning with an upper case character, and followed by any number of characters. <code> +
-ls -1 [[:upper:]]* +
-Aware_-_Kontinuum.flac +
-Glaciers-SD.mp4 +
-MendelMax_3_Full_Kit_Packing_Slip_-_Sheet1.csv +
-README+
 </code> | </code> |
-<code>[![:digit:]]*</code> Matches any name not beginning with a number. +What are the first 10 lines of this file? || 
-<code>*[[:lower:]123]</code> | Matches any name ending with ''a''-''z'', or the numbers ''1''''2''or ''3''|+Type<code>$ head -10 file.txt 
 +This is the 100th Etext file presented by Project Gutenbergand 
 +is presented in cooperation with World LibraryInc., from their 
 +Library of the Future and Shakespeare CDROMS.  Project Gutenberg 
 +often releases Etexts that are NOT placed in the Public Domain!!
  
 +Shakespeare
 +
 +*This Etext has certain copyright implications you should read!*
 +
 +<<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM
 +</code> |
 +^ What is the 3rd word on each line of the last ten lines? ||
 +^ Type: | <code>$ cat file.txt \
 +> | awk '{print $3}' \
 +> | tail -10
 +ONLY,
 +COMMERCIAL
 +CHARGES
 +
 +
 +
 +this
 +
 +
 +
 +</code> |
 +^ What are the top 10 most frequently used words? ||
 +^ Type: | <code>$ cat file.txt \
 +> | awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" \
 +> | sort -nr \
 +> | head -10
 +517065 
 +23242 the
 +19540 I
 +18297 and
 +15623 to
 +15544 of
 +12532 a
 +10824 my
 +9576 in
 +9081 you
 +</code> |
 +^ NOTE: How would you know to do that!?!?  The easiest way is to just search online for someone who's already done it, and then copy what they typed.  There are several online forums for command line usage too.  That's what I did.  Awk is so powerful, I've only scratched the surface of it myself. ||
 +^ In the file '/etc/passwd', what is the 8th line? ||
 +^ Type: | <code>$ cat /etc/passwd | head -8 | tail -1
 +lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
 +</code> |
 +^ The lines in /etc/passwd are fields separated by a colon.  What is the value in the 5th field? ||
 +^ Type: | <code>$ cat /etc/passwd | head -8 | tail -1 | awk -F: '{print $5}'
 +lp
 +</code> |
 +^ The 3rd field is the User ID number (UID).  What is the sum of all UIDs in '/etc/passwd'? ||
 +^ Type: | <code>$ n=0
 +$ cat /etc/passwd \
 +> | awk -F: '{print $3}' \
 +> | while read d ; do let n=$(( $n + $d )) ; done
 +$ echo $n
 +0
 +</code> |
 +^ | NOTE: That didn't work!  Why?  Because the while loop executes in a subshell, and while it is possible to pass values of exported values from parent shell to child subshell, the child gets a copy and not the original variable.  This means that when the child process sums up values for 'n', that value is lost when the child process exits.  Since the parent's version of 'n' never changes, it's value is still zero. |
 +^ So what's the correct way to do it?  Here's one way that works: ||
 +^ Type: | <code>$ n=0
 +$ for d in $(cat /etc/passwd | awk -F: '{print $3}') ; do \
 +> n=$(( $n + $d )) ; \
 +> done
 +$ echo $n
 +68740
 +</code> |
 +^ | NOTE: The for loop doesn't execute in a subshell.  How would you know this?  Well, reading the bash manual is probably the best way.  :-/ |
unix_102.1464189700.txt.gz · Last modified: 2016/05/25 15:21 by peek