DOC PREVIEW
UI CS 270 - Bash (Bourne Again Shell)

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Bash (Bourne Again Shell)The standard shell of Linuxmajor shell commands include:alias, bg, builtin, case..in..esac, cd, declare, dirs, env, export, fg, for..do..done, function, history, if..then..elif..then..else..fi, jobs, kill, local, popd, pushd, read, readonly, return, select..do..done, set, source, trap, unalias, unset, until..do..done, while..do..donebash is installed at /bin/bash, /bin/sh is link to /bin/bash1Bash (Bourne Again Shell)Features of Bash new or different from the previous discussionvariable manipulationcommand-line processing, aliases, and historyarithmetic, conditional expressions, control structuresdirectory stackjob controlshell functions2Bash StartupBash starts just like any programas login shellexecutes commands in .bash_profileas normal invocationexecutes commands in file .bashrcmay want to call .bashrc from within .bash_profile3Variables creation and use of shell variables are for:Value assignment and accessDefining and using lists of valuesTesting a value or for existence of a variableReading or writing a variable's value4Variables Simple variable -- creation/assignment{value = name}e.g. gameswon=12built-in command set displays all variables set in shell5Accessing simple variables$name Replaced by the value of name.${name} Replaced by the value of name. This form is useful if the expression is immediately followed by an alphanumeric that would otherwise be interpreted as part of the variable name.$ verb=sing ...assign a variable.$ echo I like $verbing ...there's no variable "verbing".I like$ echo I like ${verb}ing ...now it works.I like singing$ _6${name-word} Replaced by the value of name if set, and word otherwise.-bash-3.2$ ddd=${ddd- `date`}-bash-3.2$ echo $dddMon Sep 27 09:31:03 PDT 2010-bash-3.2$ -bash-3.2$ echo ${asdfasdf-test} (there is no asdfasdf)test7${name+word} Replaced by word if name is set, and nothing otherwise.-bash-3.2$ flag=1-bash-3.2$ echo ${flag+'flag is set'}flag is set-bash-3.2$ echo ${flag2+ 'flag2 is set'}_-bash-3.2$ 8${name=word} Assigns word to the variable name if name is not already set. Then it is replaced by the value of name.-bash-3.2$ echo x = ${x=10}x = 10-bash-3.2$ echo $x109${name?word} Replaced by name if name is set. If name is not set, word is displayed to the standard error channel and the shell is exited. If word is omitted, then a standard error message is displayed instead.-bash-3.2$ total=10-bash-3.2$ value=${total?'total not set'}-bash-3.2$ echo $value10-bash-3.2$ value=${grandTotal?'grand total not set'}-bash: grandTotal: grand total not set-bash-3.2$ 10Another example for ${name?word} $ cat script.sh ...look at the script.value=${grandTotal?'grand total is not set'}echo done # this line is never executed.$ script.sh ...run the script.script.sh: grandTotal: grand total is not set$ _ 11${#name} Replaced by the length of the value of name.${#name[*] } Replaced by the number of elements in the array name.${name:+word } Work like their counterparts that do not contain a :, except that name must be set and non-null instead of just set.${name:=word }${name:?word }${name:+word }12${name#pattern} Removes a leading pattern from name. The expression is replaced by the value of name if name doesn't begin with pattern, and with the remaining suffix if it does. This form removes the smallest matching pattern.${name##pattern} This form removes the largest matching pattern-bash-3.2$ echo $PWD/home/krings/CS270-bash-3.2$ echo $HOME/home/krings-bash-3.2$ echo ${PWD#$HOME/}CS270-bash-3.2$ echo ${PWD#$HOME}/CS27013${name%pattern} Removes a trailing pattern from name. The expression is replaced by the value of name if name doesn't end with pattern, and with the remaining prefix if it does. This form removes the smallest matching pattern.${name%%pattern} This form removes the largest matching pattern.-bash-3.2$ testfile=menu.sh-bash-3.2$ echo ${testfile%.sh}.bakmenu.bak-bash-3.2$ 14List Variables List variables (arrays) are created with declare. Simply using the variable in an array format will also work.Shell command: declare [-ax] [listname] If the named variable does not already exist, it is created. If an array name is not specified when -a is used, declare will display all currently defined arrays and their values. If the -x option is used, the variable is exported to subshells. declare writes its output in a format that can be used again as input commands. This is useful when you want to create a script that sets variables as they are set in your current environment.15List Variables example for declare$ declare -a teamnames$ teamnames[0]="Dallas Cowboys"$ teamnames[1]="Washington Redskins"$ teamnames[2]="New York Giants"if you omit the declare command, the other lines will still work as expected.16Accessing List Variables When accessing array values, you can always put braces around the variable name to explicitly distinguish it from other text that might be around it.$ echo "There are ${#teamnames[*]} teams in the NFL"There are 32 teams in the NFL$ echo "They are: ${teamnames[*]}"...17Building Lists You can build an array in one of two ways. If you know how many elements you will need, you can use the declare built-in command to define the space and assign the values into specific locations in the list. If you don't know, or don't care, how many elements will be in the list, you can simply list them and they will be added in the order you specify.18Building Lists: example $ declare -a teamnames$ teamnames[0]="Dallas Cowboys"$ teamnames[1]="Washington Redskins"$ teamnames[2]="New York Giants" ...$ teamnames[31]="Houston Texans"This can also be done in a single (long) command:$ declare -a teamnames$ teamnames=([0]="Dallas Cowboys" \ [1]="Washington Redskins" \ ... [31]="Houston Texans")or simply type$ teamnames = ("Dallas Cowboys" "Washington Redskins" \ "New York Giants" "New York Jets" \ ... "Houston Texans")19Building Lists: example How many elements are counted in the array?As many as are assigned? mylist[0]=27? mylist[5]=30? echo ${#mylist[*]} ...number of elements in mylist[]2? declare -a ...display defined element valuesdeclare -a mylist='([0]="27" [5]="30")'? _it shows 2 and not 6 elements20Destroying Lists List variables are deallocated by using built-in command unsetShell command: unset name unset


View Full Document

UI CS 270 - Bash (Bourne Again Shell)

Download Bash (Bourne Again Shell)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Bash (Bourne Again Shell) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Bash (Bourne Again Shell) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?