CS 279 - Week 13 Lecture 2 - 2022-11-16 TODAY WE WILL * announcements/reminders * a few more words about hard and soft links * quick intro to Bash functions * prep for next class ===== a bit more on hard and symbolic/soft links ===== * the ln command lets you create a link; ln with the -s option creates a symbolic link, ln without that option creates a hard link ln a-file.txt hard-link-to-file.txt ln -s a-file.txt soft-link-to-file.txt * yes, symbolic links do behave differently in certain situations... * like when you remove the linked-to file! (hard link: other hard links are fine; symbolic link: oops, when you follow it, now you get an error) * ls -li results are different: (hard links: only way you can tell is if you notice two files' i-node numbers are the same symbolic links: you SEE the l in its permissions string, you SEE soft-link-name -> what-linked-to on the right-hand-side) * hard links vs. symbolic links? * hard links CANNOT refer to a file in another file system, but symbolic links CAN * so if you need such a link, symbolic it is! * turns out: a hard link cannot refer to a directory or special file, BUT a symbolic link can * some hazards with symbolic links: * if you delete its target, the link is still there but you get an error if you follow it * can create circular references...! ===== next Bash feature: FUNCTIONS! ===== * because CS people like abstractions! * yes, one shell can call another... * BUT Bash also does have its own (slightly-skewed) take on functions... * basic syntax: desired_funct_name() { body of function } also OK: function desired_funct_name() { body of function } function desired_funct_name { body of function } * and you can call desired_funct_name desired_funct_name # no parentheses needed ===== parameters? ===== * you do not define them in the function header ...you just use $1, $2, ... as desired in the function body (!!!!!!!!!!!) * YES, this can have interesting interactions with the containing shell script's $1, $2, etc...! * how do you call a function with arguments? just the name of the function, followed by the arguments, separated by blank/tab ==== using a function from another shell script ==== * one typical approach is to use the source command! if, in shell script B, you use the source command with shell script A, then any variables or things created by shell script A (including functions!) can be used in shell script B ===== scope and parameter passing ===== * inside the function: $1, $2, etc. are relative to the function and its passed "arguments" ...the function cannot see the $1, $2, etc. from the calling shell script * I think the posted example scope1.sh corroborates this? * note that parameters cannot be changed within a function, because how WOULD you change $1, $2, etc.?