site nav‎‏‎‎‏‎

categories‎ > ‎code‎ > ‎

scripts - BaSh Script Manager

posted 10 Oct 2009 09:34 by Slippy Lane   [ updated 12 Oct 2009 15:28 ]
#!/bin/bash

script_title="Scripts - Script Manager"
script_author="Slippy Lane"
script_version="1.0"
script_inception="26th April, 2008"
script_license="Do what you like, just credit me for the original if you publish."

repo="/pub/scripts"     # The folder where your script files are stored
editor="gedit"        # Your editor of choice
viewer="less"         # Your text viewer/output pager of choice

showTitle() {
  echo "title         : $script_title"
   echo "description   : $script_description"
  echo "author        : $script_author"
  echo "version       : $script_version"
  echo "inception     : $script_inception"
  echo "license       : $script_license"
  echo
}

showUsage() {
  echo "Usage: $0 {command} [ {command options} ... ]"
  echo
  echo "Commands:"
  echo "      (l)ist    [script name]                   : shows list of scripts, or contents of a script"
  echo "      (n)ew     {script name}                   : creates a new script, and opens for editing"
  echo "      (c)opy    {existing script} {new script}  : makes a copy of an existing script with a new name and opens it for editing"
  echo "      kill      {script name}                   : deletes script from repository"
  echo "      (e)dit    {script name}                   : Calls the defined editor with script as argument"
  echo "      (a)lias   {script name}   {alias name}    : creates a shortcut alias to a script"
  echo
  echo "Settings (static):"
  echo "  repository            : $repo"
  echo "  editor                : `which $editor`"
  echo "  text viewer           : `which $viewer`"
  echo
  exit
}

toLower() {
  echo $1 | tr "[:upper:]" "[:lower:]" 


toUpper() {
  echo $1 | tr "[:lower:]" "[:upper:]" 
}

removeBackups() {
  c=0
  for bf in $repo/*~; do
    if [ -e "$bf" ]; then
      if [ $c = 0 ]; then
        echo "Removing temp files..."
        c=1
      fi
      rm "$bf"
    fi
  done
}

listScripts() {
  removeBackups
  echo "Listing scripts..."
  pushd $repo
  (
    for fn in *; do 
      echo "[ $fn ]"
    done
  ) | $viewer
  popd
  echo "Done."
}

showTitle

if [ -z "$1" ]; then
  showUsage
fi

command=`toLower $1`
shift

if [ $command = list ] | [ $command = l ]; then
  if [ -z "$1" ]; then
    listScripts
  else
    scrname=`toLower $1`
    if [ -e $repo/$scrname ]; then
      echo "Viewing script '$scrname'..."
      $viewer $repo/$scrname
    fi
  fi
elif [ $command = copy ] | [ $command = c ]; then
  if [ -e "$repo/$1" ]; then
    if [ -e "$repo/$2" ]; then
      echo "Destination script already exists: $2"
    else
      echo "Copying script $1 to $2"
      cp $repo/$1 $repo/$2
      echo "Done. Opening $2 for editing."
      $editor $repo/$2 &
      echo "Done."
    fi
  else
    echo "Script not found: $1"
  fi
elif [ $command = new ] | [ $command = n ]; then
  if [ -z "$1" ]; then
    echo "Enter a name for the new script: (leave blank to abort)"
    read newscr
    if [ -z "$1" ]; then
      exit
    fi
  else
    newscr=$1
  fi
  newscr=`toLower $newscr`
  if [ -e $repo/$newscr ]; then
    echo "Sorry, there's already a script with that name."
    exit
  fi
  echo "Creating script..."
  touch "$repo/$newscr"
  echo '#!/bin/bash' > "$repo/$newscr"
  chmod +x "$repo/$newscr"
  echo "Launching editor..."
  $editor "$repo/$newscr" &
  echo "Done."
elif [ $command = kill ]; then
  if [ -z "$1" ]; then
    echo "You must supply a script name to delete."
  fi
  killscr=`toLower $1`
  if [ -e $repo/$killscr ]; then
    echo "Are you sure you want to remove the script '$killscr'?"
    echo "Type 'YES' (in uppercase, without quotes) and hit enter to confirm."
    read strcheck
    if [ -n "$strcheck" ]; then
      if [ $strcheck = YES ]; then
        echo "Removing file."
        rm $repo/$killscr
        if [ -e $repo/$killscr ]; then
          echo "Hmmm. That didn't work. Check your permissions?"
        else
          echo "$killscr script removed successfully."
        fi
      else
        echo "Aborted."
      fi
    else
      echo "Aborted."
    fi
  else
    echo "$killscr not found."
    exit
  fi
elif [ $command = edit ] | [ $command = e ]; then
  if [ -z "$1" ]; then
    echo "You must supply a script name to edit."
  fi
  editscr=`toLower $1`
  $editor "$repo/$editscr" &
elif [ $command = alias ] | [ $command = a ]; then
  if [ -z "$2" ]; then
    echo "You must supply two arguments"
  elif [ -e "$repo/$2" ]; then
    echo "A file with that alias name already exists."
  elif [ -e "$repo/$1" ]; then
    echo "Creating alias '$2' to '$1'..."
    ln -s $repo/$1 $repo/$2
    ls -l $repo
  fi
else
  showUsage
fi
exit 

Č
ċ
ď
scripts
(4k)
Slippy Lane,
10 Oct 2009 09:35