690 likes | 970 Views
Review. Chapters 5 thru 8. Two Groups of Commands. Select commands Manipulate and Format commands. Using the Select Commands. Select commands: grep , diff , uniq , comm , wc Using Pipes – The pipe operator ( | ) redirects the output of one command to the input of another command
E N D
Review Chapters 5 thru 8
Two Groups of Commands • Select commands • Manipulate and Format commands
Using the Select Commands • Select commands: grep, diff, uniq, comm, wc • Using Pipes – The pipe operator (|) redirects the output of one command to the input of another command • An example would be to redirect the output of the ls command to the less command • The pipe operator can connect several commands on the same command line
Using Pipes Using pipe operators and connecting commands is useful when viewing directory information ls /etc | sort –r | less
Using the grep Command • Used to search for a specific pattern in a file, such as a word or phrase • grep’s options and wildcard support allow for powerful search operations • You can increase grep’s usefulness by combining with other commands, such as head or tail
Using the grep Command grep can take input from other commands and also be directed to provide input for other commands grep IBM /etc/termcap | head
Using the uniq Command • Removes duplicate lines from a file • It compares only consecutive lines, therefore uniq requires sorted input • Uniq has an option that allows you to generate output that contains a copy of each line that has a duplicate
Using the comm Command • Used to identify duplicate lines in sorted files • Unlike uniq, it does not remove duplicates, and it works with two files rather than one • It compares lines common to file1 and file2, and produces three column output • Column one contains lines found only in file1 • Column two contains lines found only in file2 • Column three contains lines found in both files
Using the diff Command • Attempts to determine the minimal changes needed to convert file1 to file2 • The output displays the line(s) that differ • The associated codes in the output indicate that in order for the files to match, specific lines must be added or deleted
Using the wc Command • Used to count the number of lines, words, and bytes or characters in text files • You may specify all three options in one issuance of the command • If you don’t specify any options, you see counts of lines, words, and characters (in that order)
Using the wc Command The options for the wc command: –l for lines –w for words –c for characters wc –l /etc/passwd
Using the Manipulate and Format Commands • These commands are: sed, tr, pr • Used to edit and transform the appearance of data before it is displayed or printed
Translating CharactersUsing the tr command • tr copies data from the standard input to the standard output, substituting or deleting characters specified by options and patterns • The patterns are strings and the strings are sets of characters • A popular use of tr is converting lowercase characters to uppercase
Using the pr Command toFormat Your Output • pr prints specified files on the standard output in paginated form • By default, pr formats the specified files into single-column pages of 66 lines • Each page has a five-line header, its latest modification date, current page, and five-line trailer consisting of blank lines
Running a Shell Script • You can run a shell script in virtually any shell that you have on your system • The Bash shell accepts more variations in command structures that other shells • Run the script by typing sh followed by the name of the script, or make the script executable and type ./ prior to the script name
Using UNIX/Linux Shell Scripts • After creating shell script, the OS is instructed that the file is an executable shell script via the chmod command • Script files can be run in several ways: • Set the path variable and type the script name at the command prompt • Type ./filename if script is in current directory • Type the script name preceded by the full path
Using Comments • Comments are important! • Provide useful documentation to both the programmer and to others who need to understand or debug the code • To use, start comment line with a #
Variables • Variables are symbolic names that represent values stored in memory • Three types of variables: • Configuration variables store information about the setup of the OS • Environment variables hold information about your login session • Shell variables are created at the command prompt or in shell scripts and are used to temporarily store information
Variables • Use the printenv variable to see a list of environment variables. • You can also use env • Look at the man pages. What is different between the two commands?
Shell Variables • Shell Variables are variables that you can define and manipulate for use with program commands in a shell • Observe basic guidelines for handling and naming shell variables • I recommend that you use all UPPERCASE characters when naming your variables
Shell Variables • Variables are handled differently depending on the syntax • Type: • echo $USERNAME • echo “$USERNAME” • echo ’$USERNAME’ • echo `$USERNAME`
Shell Operators • Bash shell operators are in four groups: • Defining operators • Evaluating operators • Arithmetic operators • Redirection operators
Defining Operators • Used to assign a value to a variable • Most common is = (equal sign) • Use quotation marks with strings • Backquote says execute the command inside the backquotes and store the result in the variable
Evaluating Operators • Used for determining the contents of a variable • echo $variablename will show the value of variablename • Double quotes can be used, but not single quotes
Arithmetic Operators (continued) • Regular mathematical precedence rules apply to arithmetic operators • To store arithmetic values in a variable, use let statement • let x=6+4*2 • echo $x
Redirection Operators • The > redirection operator overwrites an existing file • -o noclobber option of set command will prevent overwriting
Exporting Shell Variables to the Environment • Shell scripts cannot automatically access variables created and assigned • On the command line • By other scripts • Make variables global in your environment by using the export command
Modifying the PATH Variable • PATH variable controls where your shell will look for shell scripts • You can add directories to your PATH • Special directories for scripts • Your current working directory
Shell Logic Structures • Four basic logic structures needed for program development are: • Sequential logic • Decision logic • Looping logic • Case logic
Sequential Logic • Commands are executed in the order in which they appear in the script or program • The only break in this sequence comes when a branch instruction changes the flow of execution by redirecting to another location in the script or program • Used for simple, straightforward command sequences
Decision Logic • Enables your script or program to execute a statement or series of statements only if a certain condition exists • In many cases, the condition depends upon the result of a command or on a comparison • The if statement is the primary decision-making control structure in this type of logic
Looping Logic • A control structure repeats until some condition exists or some action occurs • Two common looping mechanisms: • for loops cycle through a range of values until the last in a set of values is reached • The while loop cycles as long as a particular condition exists
Looping Logic (continued) • Program control structures can be entered from the command line • Wildcard characters can be used in loops • The while loop is set up to test repeatedly for a matching condition • The while loop is used when code must be repeatedly executed an undetermined number of times
Case Logic • The case logic structure simplifies the selection from a list of choices • It allows the script to perform one of many actions, depending on the value of a variable • Two semicolons (;;) terminate the actions taken after the case matches what is being tested
Using Shell Scripting to Create a Menu • Often useful to create a menu that branches to specific shell scripts • The tput command is useful when creating menus • Can initialize the terminal display to place text and prompts in specific locations and respond to the user
Debugging a Shell Script • A shell script will not execute if there is an error in one or more commands • Running a shell script using sh enables quick debugging of problems • sh -v option displays lines of code in the script as they are read by the interpreter • sh -x option displays the command and its arguments line by line as they are run
Customizing YourPersonal Environment • When programming and shell scripting, customizing your environment by modifying the initial settings in the login scripts provides many benefits • Login scripts run just after logging in • Setting up personal bin directories and modify editor defaults are common customizations
Customizing Your Personal Environment • An alias is a name that represents another command • The .bashrc file in your home directory is used to establish customizations that take effect at each login • The .bashrc script is executed each time a shell is generated, such as when shell scripts are run
The trap Command • The trap command causes a shell program to automatically remove temporary files created when shell scripts run • Programmers often set up a subdirectory to store temporary files, and when a script file exits, trap removes the files • Having files removed from a temporary directory like this is considered “good housekeeping”
Putting It All Together in an Application • Applications require you to: • Assign and manage variables • Use shell operators • Employ shell logic structures • Use additional wildcard characters • Use tput for managing screen initialization • Use trap to clean up temporary files • Will use these skills to build a shell script application in Hands-on Project
Understanding UNIX/Linux Utilities • UNIX/Linux utilities let you • Create and manage files • Run programs • Produce reports • Monitor and maintain the system • Recover from a range of errors • New utilities are continually being added in order to make UNIX/Linux run more efficiently
Understanding UNIX/Linux Utilities • Classified into eight major areas: • File processing • System status • Networking • Communications • Security • Programming • Source code management • Miscellaneous