bourne again shell
play

Bourne (Again) Shell CIS 218 Bourne Again Shell Current GNU - PowerPoint PPT Presentation

Bourne (Again) Shell CIS 218 Bourne Again Shell Current GNU LINUX BASH Shell documentation: http://www.gnu.org/software/bash/manual/bashref.html Bash is an acronym for Bourne - Again SHell. The Bourne shell is the traditional


  1. Bourne (Again) Shell CIS 218

  2. Bourne Again Shell • Current GNU LINUX BASH Shell documentation: http://www.gnu.org/software/bash/manual/bashref.html • Bash is an acronym for ‘Bourne - Again SHell’. The Bourne shell is the traditional Unix shell originally written by Stephen Bourne. All of the Bourne shell builtin commands are available in Bash. Current BASH development occurs under the GNU license OS’es (essentially for LINUX). Which means to use caution when incorporating advanced bash features for portable bash scripts (i.e. sh). • Shell command processing - When the shell reads input, it proceeds through a sequence of operations. If the input indicates the beginning of a comment, the shell ignores the comment symbol (‘#’), and the rest of that line. - The shell reads its input and divides the input into words and operators based on the InterField Separator (IFS) value, employing the quoting rules to select which meanings to assign various words and characters. - The shell then parses these tokens into commands and other constructs, removes the special meaning of certain words or characters, expands others, redirects input and output as needed, executes the specified command, waits for the command’s exit status, and makes that exit status available for further inspection or processing.

  3. Shell Startup • When a shell starts, it runs startup files to initialize itself. Which files the shell runs depends on whether it is a login shell, an interactive shell that is not a login shell (such as you get by giving the command bash ), or a noninteractive shell (one used to execute a shell script). You must have read access to a startup file to execute the commands in it. • Interactive LOGIN files: - /etc/profile: The shell first executes the commands in /etc/profile. By default when called from bash, a command in this file calls /etc/bashrc. This file establishes system-wide default characteristics for bash users. -.bash_profile .bash_login .profile: Next the shell looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, executing the commands in the first of these files it finds. Commands in these files to override the defaults set in /etc/profile. - .bash_logout: When you log out, bash executes commands in the ~/.bash_logout file. Frequently commands that clean up after a session, such as those that remove temporary files, go in this file. • Noninteractive Shells, such as those that runs shell scripts. However, these shells inherit from the login shell variables that are set by these startup files. Noninteractive shells look for the environment variable BASH_ENV (or ENV, if the shell is called as sh) and execute commands in the file named by this variable.

  4. Shell Environment Variables • PS1 - This parameter is used as the primary prompt string. The default is: \s-\v\$ . • PS2 - The parameter is used as line continuation. The default is: > • PS3 - The value of this parameter is used as the prompt for the select command. • PS4 - This parameter is displayed during an execution trace (sh – x). The default is + • HOME – Stores the home directory • PATH – contains a list of directories which the shell searches whenever you type in the name of the program to be executed. The PATH specifies the directories to be searched for programs to be executed and not for any other types of files.

  5. Shell Operation • Reads its input from a file from a string supplied as an argument to the ‘ - c’ invocation option or from the user’s terminal (when <LF> is read). • Breaks the input into words and operators, obeying the quoting rules and metacharacter substitution. These tokens are separated by IFS. Alias expansion is performed. • Parses the tokens into simple and compound commands. • Performs the various shell expansions breaking the expanded tokens into lists of filenames and commands and arguments. • Performs any necessary redirections and removes the redirection operators and their operands from the argument list. • Executes the command. If a command delimiter is used for more than one command on a line (record), commands are executed sequentially. • Optionally waits for the command to complete and collects its exit status

  6. Bash command line operation • Command line editing under the Bourne Again Shell is implemented through the Readline Library which supports line editing that is consistent with that provided by bash. • vi mode: You can choose vi editing modes when using the Readline you can switch to vi mode with the following command: set-o vi • When you enter bash commands while in vi editing mode, you are in Input mode. As you enter a command, if you discover an error before you press RETURN, you can press ESCAPE to switch to vi Command mode. • HJKL keys can be used to change position just like in vi .

  7. eval command • eval command – forces shell to scan the command line twice. The internal eval command interprets and expands a command line before the shell interprets and expands the line. • You can use eval to execute a command you read using the read command. # read CMD cat /etc/group | grep mylogin # eval "$CMD" processes the CMD variable • You can use eval to expand builtin commands. # CMD=`date | wc` # $CMD # fails because the pipe is not expanded usage: date [-a sss.fff] [-u] [tformat] [mmddhhmm[yy]] # eval $CMD # eval expands CMD to date | wc · You can use eval to force variable redirection multiple times # X=10 # Y=X # echo '$'$Y # results in $X # eval echo '$'$Y # results in 10 # a='$b‘; b='$c‘; c=d # echo $a # results in $b # First level. # eval echo $a # results in $c # Second level. # eval eval echo $a # results in d # Third level. • you can use eval to expand variables within a command line to be the name of another variable. # eval last='$'{$#} # expands to last positional parameter. Note \ can be used in place of single quotes – whatever suppresses initial evaluation of the $.

  8. exec command • exec command replaces the current process with command without forking a child process istead runs in current process. It is always the last command executed in a script. Variables set in current process move to command. • Used with find command to act upon “found” files specified in {}. As in: find / - name “core” – print – exec rm {} \; • You can force recursion by using exec to call the current command: exec $0 • You can use exec to replace the current shell with another program (or shell); when you exit you will be logged off. This is how LOGIN works. It is also used for “secure” LOGINs to application specific systems. • You can use exec to perform I/O redirection commands for manipulating file descriptors within a script: somescript: exec < inputfile # same as ./somescript < inputfile exec 3< inputfile # Opens inputfile with file descriptor 3 for reading. exec 4> outputfile # Opens outputfile with file descriptor 4 for writing. exec 5<&0 # Makes fd 5 a copy of fd 0 (standard input). exec 6>&p # Attach fd 6 to co-process.

  9. expr command • expr command forces evaluation of the string as a number. Use back quotes to assign expression results to a variable: number=`expr $number + 1` • expr command is also used with back quotes for logical operations (e.g. comparison) b=`expr $x = $y`; echo $b # b is ‘0’ • expr can be used with most mathematical and logical operations supported in conditional statement. String length: STR=abcedfg; LENGTH=`expr $STR : ".*"`; echo $LENGTH expr length $STR String evaluation: if expr "$STR" : "[yY]“; then … String character position: expr index abcdef de Substring: expr substr "Goodnight Ladies" 11 6

  10. xargs • xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored. • Examples: - echo 1 2 3 4 | xargs echo - echo 1 2 3 4 | xargs -n 2 - find . - name "*core“ -print | xargs /bin/rm -f (same as – exec rm {} \;) - find . -name "*core" -print0 | xargs -0 -I {} mv {} ~/old.cores - xargs (-t) - Hi there - Ctrl D - find . -name '*.c' | xargs grep 'stdlib.h'

  11. exit command • exit [n] -Used to terminate current process with a status code ($?) of n, where n is any positive number with a maximum value of 255 (in older bash versions). • Functions within a script use return [n] to set a value upon return to caller.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend