Linux Operating System A Practical Guide to Fedora

  • Slides: 32
Download presentation
Linux Operating System A Practical Guide to Fedora and Red Hat Enterprise Linux Unit

Linux Operating System A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Command Line Interface (CLI) Chapter 9: The Bourne Again Shell By Fred R. Mc. Clurg © Copyright 2013, All Rights Reserved

Bash Shell Start Up Files Filename When Started Owner /etc/profile Interactive login System �/etc/profile

Bash Shell Start Up Files Filename When Started Owner /etc/profile Interactive login System �/etc/profile (system) ~/. bash_profile Interactive login User �~/. bash_profile ~/. bash_login Executes only if no (user) User ~/. bash_profile �~/. bash_login ( ~/. profile Executes only if no User �~/. profile ~/. bash_login or ~/. bash_profile ~/. bashrc Interactive non-login (new shell) User

Startup Files Upon Login

Startup Files Upon Login

Startup File: ~/. bash_profile: # Get aliases and functions if [ -f ~/. bashrc

Startup File: ~/. bash_profile: # Get aliases and functions if [ -f ~/. bashrc ] ; then. ~/. bashrc fi PATH=$PATH: $HOME/bin: . export PATH

Startup File: ~/. bash_profile (continued): # User functions function chmodx() { chmod ugo+x $*

Startup File: ~/. bash_profile (continued): # User functions function chmodx() { chmod ugo+x $* }

Startup Files Upon New Shell

Startup Files Upon New Shell

Startup File: ~/. bashrc: # Source global definitions if [ -f /etc/bashrc ] ;

Startup File: ~/. bashrc: # Source global definitions if [ -f /etc/bashrc ] ; then. /etc/bashrc fi # User aliases and functions set -o noclobber set -o vi

Startup File: ~/. bashrc (continued 2): # User aliases alias list='ls -l. F |

Startup File: ~/. bashrc (continued 2): # User aliases alias list='ls -l. F | less' alias last='ls -lt. F | less' alias h='history | less' alias chx='chmod ugo+x'

Startup File: ~/. bashrc (continued 3): # User functions function switch() { local tmp="switch.

Startup File: ~/. bashrc (continued 3): # User functions function switch() { local tmp="switch. $$" mv "$1" $tmp mv "$2" "$1" mv $tmp "$2" }

Standard Input, Output & Error �Three Streams Standard Ouput (STDOUT) Standard Input (STDIN) command

Standard Input, Output & Error �Three Streams Standard Ouput (STDOUT) Standard Input (STDIN) command Standard Error (STDERR)

Standard Out

Standard Out

Standard Error

Standard Error

Both Standard Out & Error Questions: 1. If stdout and stderr look the same,

Both Standard Out & Error Questions: 1. If stdout and stderr look the same, how can you tell the difference? 2. If the two go to the same place, how do you separate them?

Stderr and pipes don’t mix �Note: ◦ Standard error does not normally go through

Stderr and pipes don’t mix �Note: ◦ Standard error does not normally go through pipes

Separating stdout & stderr cat genesis hezekiah  1> stdout. txt  2> stderr.

Separating stdout & stderr cat genesis hezekiah 1> stdout. txt 2> stderr. txt

Copying stderr to stdout cat genesis hezekiah  1> std. Out. Err 2>&1 �Question:

Copying stderr to stdout cat genesis hezekiah 1> std. Out. Err 2>&1 �Question: ◦ What is the contents of file “std. Out. Err”?

Copy stderr to stdout prior to pipe �Note: ◦ Now standard error can go

Copy stderr to stdout prior to pipe �Note: ◦ Now standard error can go through a pipe

Copying stdout to stderr echo "I'm sorry Dave  I can not do that"

Copying stdout to stderr echo "I'm sorry Dave I can not do that" 1>&2 �Note: ◦ Sends a message to standard error ◦ Used for error reporting in scripts

Shell Script Writing Procedure 1. Specify the shell type on first line. Example: #!/bin/bash

Shell Script Writing Procedure 1. Specify the shell type on first line. Example: #!/bin/bash 2. Create script body with shell commands 3. Save the file 4. Make file executable using “chmod” 5. Execute the script by specifying: a. . /script. Name (relative pathname) b. /full/path/script. Name (absolute path) c. Or make sure script pathname is in the $PATH

Writing Your First Script #!/bin/bash # Print friendly message echo "Hello World"

Writing Your First Script #!/bin/bash # Print friendly message echo "Hello World"

ROT 13 Encryption Filter #!/bin/bash # Created: Fred Mc. Clurg # Creation: April 14,

ROT 13 Encryption Filter #!/bin/bash # Created: Fred Mc. Clurg # Creation: April 14, 2010 # Purpose: ROT 13 Encryption echo "Processing. . . " 1>&2 tr "a-z. A-Z" "n-za-m. N-ZA-M" date 1>&2

ROT 13 encryption via a pipe

ROT 13 encryption via a pipe

ROT 13 encryption via stdin

ROT 13 encryption via stdin

ROT 13 decryption via pipe

ROT 13 decryption via pipe

ROT 13 decryption via stdin

ROT 13 decryption via stdin

Filename Completion �Description: ◦ Automatic completion of a filename upon specifying an unique prefix

Filename Completion �Description: ◦ Automatic completion of a filename upon specifying an unique prefix and entering the tab key. See page 1082

Filename Completion �Better Description: ◦ Automatic completion of a filename after specifying an unique

Filename Completion �Better Description: ◦ Automatic completion of a filename after specifying an unique prefix and typing the tab character. �Example: ◦ ls fra <tab>

Tab for File Completion �Example: ◦ ls fra<tab>

Tab for File Completion �Example: ◦ ls fra<tab>

Filename Completion �Description Continued: ◦. . . If the prefix is not unique, a

Filename Completion �Description Continued: ◦. . . If the prefix is not unique, a single tab will not perform an automatic filename completion. A double tab can be used to list all the filenames matched by the current prefix. �Example: ◦ ls fr <tab>

Tab for File Matches �Example: ◦ ls fra<tab>

Tab for File Matches �Example: ◦ ls fra<tab>

Command History �Description: ◦ The shell maintains a list of recently issued commands (aka

Command History �Description: ◦ The shell maintains a list of recently issued commands (aka events). These events can be re -executed. �Example: ◦ history | tail

History Expansion Event Designators Designator Description !str Execute cmd starting with “str” !n Execute

History Expansion Event Designators Designator Description !str Execute cmd starting with “str” !n Execute cmd n from history !-n Execute nth preceding cmd !! Execute previous command ^old^new Substitute old for new on previous command !$ Use last arg of previous cmd