bash-scripting-cheatsheets.pdf - Bash scripting cheatsheet...

This preview shows page 1 - 4 out of 9 pages.

CHEATSHEET FORBash scriptingPattern substitutionSTR=/path/to/foo.cecho${STR%.c}#=> "/path/to/foo"echo ${STR%.c}.o#=> "/path/to/foo.o"echo${STR##*.}#=> "c" (extension)BASE=${STR##*/}#=> "foo.c" (basepath)DIR=${SRC%$BASE}#=> "/path/to"Substitutions by regexecho${STR/hi/hello}# Replace first matchecho${STR//hi/hello}# Replace all matchesecho${STR/#hi/hello}# ^hiecho${STR/%hi/hello}# hi$echo"${STR:0:3}"# .substr(0, 3) -- position, lengthecho"${STR:-3:3}"# Negative position = from the rightecho${#line}# Length of $line[ -z"$CC"] && CC=gcc# CC ||= "gcc"assignment${CC:=gcc}# $CC || "gcc"${CC:-gcc}# same as aboveReading inputBash scripting cheatsheet1 of 92017-05-08 02:51 PM
echo -n"Proceed? [y/n]: "read ansecho $ansread -n1ans# Just one characterLoopsBasic for loopfor i in /etc/rc.*; doecho $idoneRangesfor i in {1..5}; doecho"Welcome $i"doneReading linescat file.txt | whileread line; doecho $linedoneBash scripting cheatsheet2 of 92017-05-08 02:51 PM
Fun�ionsDe�ning functionsmyfunc() { ... }fuction myfunc { ... }fuction myfunc(){ ... }Returning stringsmyfunc() {local myresult='some value'echo $myresult}result=$(myfunc)Errors

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 9 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Summer
Professor
Doffman
Tags

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture