csc 352– unix programming, spring 2015 april 28 a few final commands

19
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Upload: charles-hall

Post on 16-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

CSC 352– Unix Programming, Spring 2015

April 28A few final commands

Page 2: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Based on student assn 3 solutions

• These are some bash shell mechanisms and Unix utilities used by students in assignment 3 that can be very useful.

• I had forgotten about some of these.• Some may be buried in or missing from the

textbook.• I always learn or re-learn when teaching Unix.

Page 3: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

bc – the “bench calculator”

• It reads standard input, not command line.• Following POSIX bc operators behave exactly like

their C counterparts:• + - * /• += -= *= /=• ++ -- < >• == != <= >=• ( ) [ ] { }

• GNU bc has others, but they are not portable.

Page 4: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Some bc examples

• [:-) ~] echo '1.1 + 20 * -2.0' | bc• -38.9• [:-) ~] bc << EOF1.1 + 20 * -2.0EOF• -38.9• Latter is a “here document” for supplying literal text to

standard input.• Note that 0 means false and non-0 means true in a bc

expression, just like C & C++, but opposite of shell exit status.

Page 5: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Some pitfalls in = assignment

• [:-) ~] expression='1.1 + 20 * -2.0'• [:-) ~] echo $expression• 1.1 + 20 bignum.py ... FILES ... -2.0– Glob matching captures the *

• [:-) ~] expression='1.1 + 20 \* -2.0'• [:-) ~] echo $expression• 1.1 + 20 \* -2.– The backslash is now part of the string.

Page 6: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

More pitfalls in = assignment• expression="1.1 + 20 * -2.0” glob matches• expression="1.1 + 20 \* -2.0” captures \• [:-) ~] expression=1.1 + 20 \* -2.0

-bash: +: command not found• [:-) ~] set –f # SHUT OFF GLOBBING• [:-) ~] ls *• ls: cannot access *: No such file or directory• [:-) ~] expression='1.1 + 20 * -2.0'• [:-) ~] echo $expression• 1.1 + 20 * -2.0• [:-) ~] echo $expression | bc• -38.9

Page 7: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

“No spaces” works without -f

• [:-) ~] expression='1.1+20*-2.0'• [:-) ~] echo $expression• 1.1+20*-2.0• [:-) ~] echo $expression | bc• -38.9

Page 8: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

[[ EXPRESSION ]]

• [[ expression ]]• Return a status of 0 or 1 depending on the evaluation of

the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as -f must be unquoted to be recognized as primaries.

Page 9: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

[[ … ]] examples

• http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

• [:-) ~] [[ a -lt 4 && a -gt 2 ]]• [:-) ~] echo $?• 0• You can use logical connectives && and ||

inside the test brackets.

Page 10: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

[[ … ]] regular expression matching• [:-) ~] number='^(([0-9]+(\.[0-9]*)?)|([0-9]*\.[0-9]+))$’• The \. Is necessary to match literal “.” -- . by itself matches any char.• [:-) ~] [[ 1 =~ $number ]] ; echo $?• 0• [:-) ~] [[ 1. =~ $number ]] ; echo $?• 0• [:-) ~] [[ 1.1 =~ $number ]] ; echo $?• 0• [:-) ~] [[ .1 =~ $number ]] ; echo $?• 0• [:-) ~] [[ 1f =~ $number ]] ; echo $?• 1

Page 11: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

egrep (extended grep) patternspattern * 0 or more occurrences of previous character or (expression)pattern + 1 or more occurrences of previous character or (expression)pattern ? 0 or 1 occurrence of previous character or (expression)pattern . any single characterpattern [pqr] any single character from set p, q or rpattern [a-zA-Z] any single character from range a-z or A-Zpattern [^pqr] any single character *not* from set p, q or rpattern ( EXPR ) is for subexpression groupingpattern P1 | P2 is for pattern P1 or P2pattern ^ the start of the string being searched for a matchpattern $ the end of the string being searched for a matchpattern \ escapes the next character so it is treated as a regular charThese are the most useful regular expression patterns available to grep, sed,

[[ $string =~ $pattern ]] tests, and for searching in emacs and vi.The shell uses so-called “glob-style matching” for strings (*.java), which differ from

regular expressions (.*\.java) used by grep, sed, emacs and vi.

Page 12: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Regular expression examples p.1• [:-) ~/unix] cat re1.txt• abcdefg• 1234567• 123.456• 123abc• !@#$%^&*()• ABCDEFGHIJKLM• AaAaBbVv• [:-) ~/unix] egrep '[0-9]+' re1.txt• 1234567• 123.456• 123abc

Page 13: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Regular expression examples p.2• [:-) ~/unix] egrep '^[0-9]+$' re1.txt• 1234567• [:-) ~/unix] egrep '^[0-9]*$' re1.txt• 1234567• [:-) ~/unix] egrep '[0-9]*' re1.txt• abcdefg• 1234567• 123.456• 123abc• !@#$%^&*()• ABCDEFGHIJKLM• AaAaBbVv

Page 14: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Regular expression examples p.3• [:-) ~/unix] egrep '[0-9]+.?[0-9]*' re1.txt• 1234567• 123.456• 123abc• [:-) ~/unix] egrep '^[0-9]+.?[0-9]*$' re1.txt• 1234567• 123.456• [:-) ~/unix] egrep '^[0-9]+[^0-9][0-9]*$' re1.txt• 123.456• [:-) ~/unix] egrep '^[0-9]+[^0-9]?[0-9]*$' re1.txt• 1234567• 123.456• [:-) ~/unix] egrep '^[0-9]+[^0-9]?[A-Fabcdef]+$' re1.txt• 123abc

Page 15: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Regular expression examples p.4• [:-) ~/unix] egrep '(([0-9]+)|([A-Za-z]+))$' re1.txt• abcdefg• 1234567• 123.456• 123abc• ABCDEFGHIJKLM• AaAaBbVv• [:-) ~/unix] egrep '[^A-Za-z0-9]+$' re1.txt• !@#$%^&*()

Page 16: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

Regular expression examples p.5• [:-) ~/unix] egrep '[^A-Za-z0-9]?$' re1.txt• abcdefg• 1234567• 123.456• 123abc• !@#$%^&*()• ABCDEFGHIJKLM• AaAaBbVv• [:-) ~/unix] egrep '[^A-Za-z0-9]*$' re1.txt• abcdefg• 1234567• 123.456• 123abc• !@#$%^&*()• ABCDEFGHIJKLM• AaAaBbVv

Page 17: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

sed writes to standard output,sed –r uses extended Res p.1

• [:-) ~/unix] egrep '[^A-Za-z0-9]+$' re1.txt• !@#$%^&*()• [:-) ~/unix] sed -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt• abcdefg• 1234567• 123.456• 123abc• !@#$%^&*()• ABCDEFGHIJKLM• AaAaBbVv

Page 18: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

sed writes to standard output,sed –r uses extended Res p.1

• [:-) ~/unix] sed -r -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt• abcdefg• 1234567• 123.456• 123abc• AllFixedUp• ABCDEFGHIJKLM• AaAaBbVv• sed does NOT modify the source file. It sends to stdout but you could:

– sed -r -e 's/[^A-Za-z0-9]+$/AllFixedUp/g' re1.txt > tmpfile.txt– mv tmpfile.txt re1.txt

Page 19: CSC 352– Unix Programming, Spring 2015 April 28 A few final commands

[[ =~ ]] pattern matching• [:-) ~/unix] pattern='^[0-9]+.?[a-z]*$'• [:-) ~/unix] echo $pattern• ^[0-9]+.?[a-z]*$• [:-) ~/unix] for line in `cat re1.txt`• > do• > if [[ $line =~ $pattern ]] # [[ ! $line =~ $pattern ]] for non-matching lines• > then• > echo "MATCH $line"• > fi• > done• MATCH 1234567• MATCH 123abc