Re: Need some bash scripting assistance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10Jun2010 13:51, James Wellnitz <jwellnitz@xxxxxxxxx> 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@xxxxxxxxxx> 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.
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux