Findit BASH Version

In the spirit of short scripts I needed another version of FINDit like you may see in my PowerShell post. Here is that very thing in BASH/ZSH

findit()
{
  #$1 is the path to search for a string
  #$2 is the string to search and find
   usage()
    {
      echo " -- Usage for findit --- 
      \$1 is the path to search for a string
      \$2 is the string to search and find
    __________________________
    example: findit $HOME/.zshrc powerlevel10k
    output:  /Users/thom.schumacher/.zshrc:10:ZSH_THEME=\"powerlevel10k/powerlevel10k\"
            /Users/thom.schumacher/.zshrc:50:source ~/powerlevel10k/powerlevel10k.zsh-theme
    __________________________"
    }
  if [ -z "$1" ] || [ -z "$2" ]; then
    echo >&2 'Need 2 parameters which are not empty'
    usage
  return 1
  fi
  grep -rnw "$1" -e "$2"

}

findit . echo

This will search from your current directory and tell you ever file that it finds the word Echo in.

with the filename and the line it found it.

Following a URL in a BASH function

I had need to ensure a Url was valid.. So i decided to write a function that I could use in my bash session for just that type of thing:

followedUrlValid()
  usage()
  {
    echo " -- Usage for followedUrlValid --- 
    \$1 = the account you wish to login to
      looks for the http_code from getting the url if valid returns true if not returns false
      if the url has redirects it'll follow the redirects
    "
  }
  if [ -z "$1" ]; then
    echo >&2 'Need 1 parameters which are not empty'
    usage
    return 1
  fi
   returnval='false'
   value=$(curl -o /dev/null -LIsw '%{http_code}\n' $1)
   if [ "$value" = 200 ]; then
    returnval='true'
  fi
  echo "$returnval"

Below you can see two uses of this function in your bash/zsh prompt

followedUrlValid https://google.com
true

followedUrlValid https://invalidurl.okiedokie
false

Parsing path in ZSH

Lately i’ve had to work with a new operating system (apple) and it’s built in shell is ZSH.

With that i’ve had to understand how to parse strings in it compared to KSH and Bash. One interesting thing I found is that that the standard $PATH variable in zsh is already split into an array for you in this variable $path. So to test and see if something is in your path like BASH you can simply use that variable to your advantage like this:


getBashVersion()
{
  for p in $path;do
    b="$p/bash"
    if [[ -f "$b" ]]; then
       echo "_____$(echo $p)______"
       $b --version | grep version
       echo "_____________"
    else
      echo "no bash found in $p"
    fi
  done

}

Once you’ve sourced this in your profile you can run like this:

> getBashVersion

no bash found in /Users/username/py3/bin
_____/usr/local/bin______
GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin21.1.0)
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
_____________
no bash found in /usr/bin
_____/bin______
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
_____________
no bash found in /usr/sbin
no bash found in /sbin
no bash found in /usr/local/MacGPG2/bin
no bash found in /Users/username/src/Python/environments/ekp-regress/bin
no bash found in /Users/username/.cargo/bin
no bash found in /Users/username/.cargo/bin

Hopefully this helps someone

Until then keep scripting

thom