Www.mitntraining.com



4910455-3175009525078105mobile itnTSolutions, LLC.00mobile itnTSolutions, LLC.4572002857500Linux Systems AdministratorProgramClass Notes:“Working with the Bash Shell”00Linux Systems AdministratorProgramClass Notes:“Working with the Bash Shell”sWorking with the Bash Shell To communicate commands to the OS kernel, an interface is needed that sits between the kernel and the end user issuing these commands. . .this interface is known as “the shell”.Several “shells” are available: --- Bash : short for the Bourne Again Shell. . .the one used in most situations, because it’s compatible with the Bourne shell, commonly found on UNIX servers. --- command line interface or shell used on most Linux systems --- incorporates features from sh (the original Bourne shell), csh (the C shell), and ksh (the Korn shell)--- tcsh : A shell with a scripting language that works like the C programming language. . .very popular with C programmers--- zsh : A shell compatible with Bash but offers even more features--- sash : known as stand-alone shell. . .a minimal-feature shell that runs in almost all environments. . .very well suited for system troubleshootingHow to check which shell I’m using? # echo $0 OR echo $SHELLUseful Bash Key Sequences (using keyboard): sometimes, you will enter a command from the Bash command line and nothing or something totally unexpected, will happen. If that occurs, it is good to know that some key sequences are available to perform basic Bash mgmt tasks. . .Here’s a short list of the most useful of these key sequences:Ctrl+C : Cancels the currently running command //// add on: 12 Mar 2015 ///////////Ctrl+D : Logs out of current session //// add on: 12 Mar 2015 /////////// Ctrl+R : This is the reverse search feature of History. -- useful when you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully-- For example: initially run command: cat /etc/redhat-release- to use search history: now [Press Ctrl+R from cmd prompt, which will display the reverse-i-search prompt]- should see: (reverse-i-search) ‘red’: cat /etc/redhat-release- [Press enter when you see your command, which will execut the command from the history]Ctrl+A : Brings the cursor to the beginning of the current commandCtrl+F : Moves the cursor forward one character //// add on: 12 Mar 2015 ///////////Ctrl+B : moves the cursor backward one character //// add on: 12 Mar 2015 ///////////Ctrl+U : erases the complete line. //// add on: 12 Mar 2015 ///////////Ctrl+Z : used to stop a command. . .but it does not terminate the command. . ..it’s just halted until it is started again with the fg command as a foreground job or with the bg command as a background job.Example: # yes > /dev/null …. Then press “Ctrl + Z”--- Alternative to "Executing a background job" is using ampersand (&) : For example: find / -ctime -1 > /tmp/changed-file-list.txt &Working with Bash HistoryAnother useful aspect of the Bash shell is the “history” feature. This history mechanism displays the last commands you used…… By default, the last 1,000 commands of any user are remembered [Example]use history "n" to see the last "n" commands [Example]# history 10As an administrator, you sometimes need to manage the commands that are in the history list. There are two ways to do this:1st, you can manage the file “.bash_history” [~/.bash_history or /home/name/.bash_history], which stores all the commands you have used before. . .this is stored in the home directory of the userFor example: if you wanted to delete this file for the user “joyce”, just remove it with the command: “rm /home/joyce/.bash_history2nd way, is by using the history command, with the “-c” option. . .this will clear the history list for the user who uses this commandFor example: use “history –c” to make sure that your history is cleared.Additional Options:To repeat the last command in bash, type “!!” – pronounced as bang bangRepeat other commands using one bang [!] followed by one or more characters. . .this shell will repeat the last command that started with those characters Example: # touch file42then,# cat file42then, # !tocommand should display last command beginning with “to”typing ! followed by the number preceding the command: Example: !43How to determine number of commands to be saved to History file: $HISTSIZE Variable can determine number of commands to be remembered in your current environment. Change History size with command: Example below:# echo $HISTSIZE ---> should see default number (500 or 1000)# HISTSIZE=1200<to see new size> # echo $HISTSIZERegular Expressions:Also, referred to as pattern matching, regex, or regexp, is a pattern that is matched against a string of characters in a file or supplied inputAny pattern with one or more white spaces must be enclosed within quotesLinux provides a powerful tool called “grep (global regular expression print) for pattern matchingThis tool searches the contents of one or more text files or supplied input for a regular expressionFor example: To search for the pattern user student1 in the /etc/passwd file# grep student1 /etc/passwdMetacharacters:These are special characters that possess special meaning to the shell, which include:< > (input/output direction)^ (caret)$. (period)* (asterick)? (question mark)[] (square bracket)| (pipe){} (curley braces)() (parentheses)+ (plus)\ (backslash)The * Character:The * matches zero to an unlimited number of charactersFor example, to list names of all files in the /etc directory that begin with letters “ali” followed by any characters: # ls /etc/ali*The [] Characters:Used to match either a set of characters or a range of characters for a single character positionFor example, the 2 characters enclosed in brackets below, the output will include all files and directories that begin with either of the two characters followed by any number of characters# ls /usr/sbin/[yw]*When the range of characters must be specified in a proper sequence such as [a-z] or [0-9], the example below matches all directory names that begin with any letter between m and p:# ls -d /etc/system/system/[m-p]*The | Character:The pipe is a special character that is used to send the output of one command as input to the nextThe character is also used to define alternations in regular expressionsFor example, the following example runs the “ll” command on “/etc” and sends the output to the “more” command for displaying the directory listing one screenful at a time: # ll /etc | moreExercise To Do:Shell History located on website under Exercises ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download