Need some bash scripting assistance

Cameron Simpson cs at zip.com.au
Fri Jun 11 01:41:53 UTC 2010


On 10Jun2010 13:51, James Wellnitz <jwellnitz at gmail.com> wrote:
| Something like this will basically do the "increment target link" part
| (making some big assumptions, doing no error checking, not handling
| wrap-around, etc.):
| 
| cur_link=$(readlink target | sed -e "s#./##")
| cur_number=${cur_link%% *}
| cur_number=${cur_number#0}
| let new_number=cur_number+1
| new_number=$(printf "%02d" $new_number)
| new_link=$(find . -name "${new_number} *")
| ln -sf "${new_link}" target
| 
| Dealing with the leading '0' (zero) accounts for some of odd parts
| (e.g. the printf).

Or, untested, this:

  cur_link=$(readlink target | sed -ne 's/^0*\([0-9][0-9]*\) .*/\1/p')
  next=$(expr "$curlink" + 1)
  next0=$(printf "%02d" "$next")
  set -- "$next "*
  rm target && ln -s "$1" target

which seems a bit shorter.

There's no sanity checking in there, eg that $cur_link is not empty or
that the set command matches exactly one file.

Most particularly, using find to get filenames is rather heavyweight and
wordy and flakey in the face of weird filenames.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

If I had thought about it, I wouldn't have done the experiment.
The literature was full of examples that said you can't do this.
      --Spencer Silver on the work that led to the unique adhesives
        for 3-M "Post-It" Notepads.


More information about the users mailing list