file system exploration
play

File system exploration Its not unusual for a bash script to search - PowerPoint PPT Presentation

File system exploration Its not unusual for a bash script to search a directory tree To do so, it needs to be able to check that a directory is actually accessible, look at its contents, distinguish between files and directories, call


  1. File system exploration ● It’s not unusual for a bash script to search a directory tree ● To do so, it needs to be able to check that a directory is actually accessible, look at its contents, distinguish between files and directories, call itself recursively, etc ● We’ll look at a simple exploration function, that expects a directory name as its parameter then searches that recursively searches that directory, looking for git repositories

  2. Basic design ● First the function checks it was actually passed a parameter ● Then it checks that actually is the name of an accessible directory (it exists, is readable, is executable) ● Then it checks if the directory is the root of a git repository, printing the directory name if so ● If the directory isn’t the root of a repo, the function calls itself on each subdirectory

  3. Code part 1: error checks function search() { # bail out if they didn’t pass the right number of parameters if [$# -ne 1] ; then return 0 end # get the parameter, bail out if it’s an empty string local dir=$1 if [ -z $dir ] ; then return 0 end

  4. Code part 2: is it a git repo? elif [ -d $dir ] ; then # yes, it is a directory # if it’s a git repo, it must have a .git subdirectory, let’s check local gitname=”$dir/.git” if [ -d $gitname ] ; then echo “git repository found: $dir” else # not a repo, so need to check each subdirectory for file in “$dir”/*; do if [ -d $file ] ; then search $file fi done fi fi }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend