Pages: [1]   Go Down
Author Topic: bash scripts to maintain sketchbook  (Read 418 times)
0 Members and 1 Guest are viewing this topic.
Berea, KY, USA
Offline Offline
Jr. Member
**
Karma: 1
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might not be the correct section of the forum to post this.  Feel free to correct me it not.

This applies to Unix (Mac OS), Linux or Android users, or anyone with access to bash or a bash-like shell.

Here are a couple of very simple scripts that display the contents of sketchbook/<sketchname> or rename <sketchname> (and the enclosing folder).

I have found these simple tools useful for maintaining my sketchbook(s).

https://www.dropbox.com/sh/yci5mfkihjm8jh3/NtaYVcexl-

The scripts are catsketch.sh and mvsketch.sh

As always, comments, suggestions, questions are welcome and I trust these will be useful to folks.

BTW, if your environment doesn't allow you to chmode a+x <scriptname> or if /bin/bash isn't where bash lives (I'm thinking of Android here, no un-rooted devices), you can just say bash <scriptname>.
Logged

There are 10 kinds of people in the world, those who understand binary, and those who don't.

Berea, KY, USA
Offline Offline
Jr. Member
**
Karma: 1
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed my dropbox setup (to save space in the cloud) and the previous links don't work correctly anymore.  The scripts are short, so I'll just quote them here:

Code:
#!/bin/bash
#
# cat sketch
# DdelV 20131004
#
# simple -- just cat all files in the named directory.
#
if [[ $# -lt 1 ]]
then
  echo "Usage $0 [basename of sketch] ..."
  exit 1
fi
#
while [[ -d $1 ]]
do
  for f in ${1}/*
  do
echo "########## $f ############"
    cat $f
  done
  shift
done


and,

Code:
#!/bin/bash
# DdelV 20131003
# move (rename) an Arduino sketch
#
# the basename of the sketch is also
# the name of the enclosing folder.
#
# the file extension may be .pde
# or .ino
#
# script exits with status=0 on success
# or 1 incorrect arguments (displays usage text)
#    2 source directory does not exist
#    3 source file not found in source directory (or bad ext)
#    4 creation of new directory failed
#    5 moving of file to newly created directory failed
#    6 unable to remove old directory (not empty?)
#    7 bash error
#
if [[ $# -ne 2 ]]
then
  echo "Usage $0 oldname newname"
  echo "(best to execute this in ~/sketchbook folder.)"
  exit 1
fi
#
# is there a directory by that name?
if [[ -d $1 ]]
then
# yes, carve off any trailing slash
  OLDNAME=${1%/}
else
# no, complain and quit.
  echo "No directory named $1 found!"
  exit 2
fi
#
# okay, file by that name in directory?
if [[ -e $OLDNAME/${OLDNAME}.ino ]]
then
# found it, set extension
  EXT=.ino
# how about the obsolete extension?
elif [[ -e $OLDNAME/${OLDNAME}.pde ]]
then
# okay, that's it then (we'll change it, below)
  EXT=.pde
else
## debug  echo "OLDNAME is $OLDNAME"
# we didn't find it, typo?
  echo "No file named ${OLDNAME}.ino or ${OLDNAME}.pde found in $OLDNAME!"
  exit 3
fi
#
# note that we DON'T check for existing target,
# mkdir won't complain if target directory exists
# mv won't clobber, though.
#
NEWNAME=$2
if  mkdir -p -m 755 $NEWNAME
then
# DO IT!
  if mv -v -n ${OLDNAME}/${OLDNAME}$EXT ${NEWNAME}/${NEWNAME}.ino
    then
# are there more files to move?
      if [[ -e ${OLDNAME}/* ]]
      then
# yes, move them.
mv -v -n ${OLDNAME}/* ${NEWNAME}/
      fi
# directory ${OLDNAME} should now be empty
# and it can be removed.
      if rmdir -v ${OLDNAME}
      then
# that went well, let's leave with good status
        exit 0
      else
        echo "Couldn't remove old directory ${OLDNAME}."
        exit 6
      fi
    fi
  else
# here if moving sketch failed.
    echo "mv $OLDNAME$EXT $NEWNAME.ino failed!"
    exit 5
  fi
else
# here if creation of new dir failed.
  echo "mkdir ${NEWNAME} failed!"
  exit 4
fi
# we should never reach this point!
exit 7

Of course, you may want to write a rmsketch.sh script too (left as an exercise for the student)!
Logged

There are 10 kinds of people in the world, those who understand binary, and those who don't.

Pages: [1]   Go Up
Jump to: