Chapter 7: Working with the BASH Shell

Ace your homework & exams now with Quizwiz!

using sed command

-cat file | sed s/the/THE/ (replaces the with THE but only for the first instance on each line) -cat file | sed s/the/THE/g (globally replaces the with THE which means all instances are replaced) -cat file | sed /love/s/the/THE (replaces the with THE only on lines that contain the word love) -cat file | sed 5,8s/the/THE (replaces the with THE only on lines 5 to 8) -cat file | sed /the/d (removes lines that contain the)

filter command

A command that can take from stdin and send to stdout. In other words, a filter is a command that can exist in the middle of a pipe

alias command

A command used to create and view list of special variables that are shortcuts to longer command strings; to create aliases to multiple commands, separate them with a semicolon

env command

A command used to display a list of exported variables present in the current shell, except special variables; you typically want to pipe grep with the command

tee command

A command used to take from stdin and send to both stdout and a specified file; ex: cat filename |tr a A | sort | pr -d | tee newfile | less

tr command

A command used to transform or change characters received from stdin; ex: tr r R </etc/issue makes the output lowercase r's into uppercase; to save a copy of the output, you can include both standard output and standard input redirection on the same command; ex: tr r R </etc/issue >newissue

set command

A command used to view all variables in the shell, except special variables; you can also view user-created variables with this command; you typically want to pipe grep with this command

standard input (stdin)

A file descriptor that represents information input to a command during execution; represented by the number 0

standard output (stdout)

A file descriptor that represents the desired output from a command; represented by the number 1

awk command

A filter command used to search for and display text, but it treats each line as a record in a database and each word in a line as a database field; to refer to a word, use $1, $2, $3, etc; -cat file | awk '/the/ {print $1, $4}' (displays one the first and fourth words on line that have the word the in them)

counter variable

A variable that is altered by loop constructs to ensure that commands are not executed indefinitely

environment files

The files used immediately after login to execute commands; they are typically used to load variables into memory. if you want a variable you create to be used at all times, you must place it in a one of these files; to add a variable to one of these files, create a new line with the command that creates the variable (export newvariable="contents"); you can also add commands into files for them to run on shell startup; to add commands for when exiting shell (like cleanup commands) add them into the /bash_logout file

hashpling

The first line in a shell script, which defines the shell that will be used to interpret the commands in the script file.

shell scripts

The text files that contain a list of commands or constructs for the shell to execute in order; used to create custom programs that perform administrative tasks on Linux systems

standard error (sterr)

a file descriptor that refers to any error messages generated by the command; represented by the number 2

variable

a reserved portion of memory containing information that might be accessed

subshell

a shell started by the current shell; most commands that are run by the shell are run in a subshell

user-defined variables

a user's own custom variables

decision constructs

alter the flow of a program based on whether a command in the program completed successfully or based on a decision that the user makes in response to a question posed by the program

while construct

begins with a statement and as long as the statement is true, the loop continues to execute. typically contains a counter variable. syntax: while this returns true do these commands done

delimiters

characters that separate data entries from one another; awk uses spaces or tabs as delimiters; to change it to a colon for configuration files, do this: awk -F :

test statement

enclosed within square brackets [] or prefixed by the word "test" and used to test certain conditions on the system

seq command

generates a list of numbers for use within a for loop

to increment

make a variable equal to 'expr $VARIABLE + 1'

How to write a Script

make it legible with the echo command that prints whatever you want it to (print spaces and friendly phrases)

How to create a new variable

newvariable = "contents" OR export newvariable = "contents" which exports and creates it

Special Variables

not displayed by the set or env commands, they perform special functions

commands in constructs

return true if they perform their function properly

escape sequences

special sequences involving the backslash character that further manipulate the way text is displayed to the terminal screen, provided the -e option is specified to the echo command (used in scripts); good for formatting output

comment

starts with a hashtag and is ignored by the shell (the hashpling is the exception)

for construct

sued to process a list of objects, such as files, directories, users, printers, and so on. syntax: for var_name in string1 string2 string3 ... ... do these commands done the program does its thing for each string and ends when there are no more

if construct

syntax: if this is true then do these commands (one per line and there can be more than one, typically indented for legibility) elif this is true (optional and you can do an unlimited amount of these) then do these commands else (optional) do these commands fi (ends statement)

read command

takes user input from standard input and places it in a variable specified by an argument to the read command ex: echo -e "What is your name? --> \c" read USERNAME echo "Hello $USERNAME"

variable identifier

the name of a variable

file descriptors

three for each command that can be manipulated by the BASH shell: standard input (stdin), standard output (stdout), and standard error (stderr)

echo command

to view the contents of PS1, use this command and $PS1

environment variables

typically set by the system and contain information that the system and programs access regularly

bash command

use and specify your script as the argument to run a script (this is if you only have read permission); if you have write and execute permission on the script, you can execute it like any other program (specify the pathname)

Where do you put an executable file if you want it to run without specifying a pathname?

use echo $PATH to see which directories you could put it in (usually sbin or bin)

export command

use this to export a variable to all subshells to ensure that all programs started by the current shell have the ability to access the variable

case construct

use when there are more choices in a script. the case statement compares the value of a variable with several different patterns of text or numbers and when a match occurs, it executes the commands to the right of the pattern. syntax: case variable in pattern1 ) do this ;; pattern2 ) do this ;; pattern3 ) do this ;; esac (end statement) ex: case $ANSWER in d | D ) echo -e "\nToday's date is: \c" date ;; * ) echo -e "Invalid choice! \a" ;; esac

=

used to change the value of a variable; PS1= "new value" or HOME=/etc (/etc is the new home. dont do this)

loop constructs

used to execute commands repetitively, like decision constructs, they alter the flow of a program based on the result of a particular statement. two most common loop constructs are for and while

pipes

used to send output of one command to another command as input; pipe with the less command to view output in a readable format; pipe with grep <keyword> to view lines with the information you want; you can pipe however many commands you want; you need to redirect input in the beginning and output at the end

the && and || constructs

used when only one decision needs to be made. syntax: command && command (the command on the right executes if the one on the left did) command || command (the command on the right executes if the one on the left did NOT)

redirection

using the symbol >, you can redirect standard output or standard errors to a file on the filesystem; ex: ls /etc/hosts /etc/h 1><filename> ; the standard error is still seen on the terminal but the standard output was moved to the file; if the file had contents, BASH would have cleared it;


Related study sets

Chapter 17 - Retailing and Omnichannel Marketing (Smartbook)

View Set

Systematic Innovation: Week 1 Material

View Set

Principles of Advertising (JMC 348) Connect Assignments

View Set