Zero-fill

Your assignment is to write a bash script "~/bin/zf.bash" that accepts an N argument list that consists of (1) a zero-fill count and (2 .. N) arguments that are filenames to be zero-filled. Please remove leading pathname information before zero-filling the filenames. Please don't create new files or rename the old filenames; just create the new names in the fashion shown below.

For instance, your script should run like exactly this on the filenames in the test directory /usr/local/zero-fill:

[test@localhost]$ ~/bin/zf.bash 10 /usr/local/zero-fill/05101
05101 becomes 0000005101
[test@localhost]$ ~/bin/zf.bash 10 /usr/local/zero-fill/05101 /usr/local/zero-fill/49
05101 becomes 0000005101
49 becomes 0000000049
[test@localhost]$ ~/bin/zf.bash 10 /usr/local/zero-fill/*
05101 becomes 0000005101
15 becomes 0000000015
2 becomes 0000000002
3 becomes 0000000003
4 becomes 0000000004
49 becomes 0000000049
50345 becomes 0000050345
51 becomes 0000000051
5815 becomes 0000005815
991 becomes 0000000991
and becomes 0000000and
[test@localhost]$ ~/bin/zf.bash 6 /usr/local/zero-fill/*
05101 becomes 005101
15 becomes 000015
2 becomes 000002
3 becomes 000003
4 becomes 000004
49 becomes 000049
50345 becomes 050345
51 becomes 000051
5815 becomes 005815
991 becomes 000991
and becomes 000and
[test@localhost]$ ~/bin/zf.bash 5 /usr/local/zero-fill/*
05101 becomes 05101
15 becomes 00015
2 becomes 00002
3 becomes 00003
4 becomes 00004
49 becomes 00049
50345 becomes 50345
51 becomes 00051
5815 becomes 05815
991 becomes 00991
and becomes 00and
[test@localhost]$ ~/bin/zf.bash 3 /usr/local/zero-fill/*
05101 becomes 05101
15 becomes 015
2 becomes 002
3 becomes 003
4 becomes 004
49 becomes 049
50345 becomes 50345
51 becomes 051
5815 becomes 5815
991 becomes 991
and becomes and
[test@localhost]$ ~/bin/zf.bash 2 /usr/local/zero-fill/*
05101 becomes 05101
15 becomes 15
2 becomes 02
3 becomes 03
4 becomes 04
49 becomes 49
50345 becomes 50345
51 becomes 51
5815 becomes 5815
991 becomes 991
and becomes and
[test@localhost]$ ~/bin/zf.bash 1 /usr/local/zero-fill/*
05101 becomes 05101
15 becomes 15
2 becomes 2
3 becomes 3
4 becomes 4
49 becomes 49
50345 becomes 50345
51 becomes 51
5815 becomes 5815
991 becomes 991
and becomes and
      

As you might recall, this is exactly equivalent to the C library's %d format conversion specifier, and thus it might occur to you to try to use the program "printf", which does work for all of the given cases in "/usr/local/zero-fill" except for "and":

[test@localhost]$ printf "%05d\n" 935
00935
[test@localhost]$ printf "%05d\n" and
bash: printf: and: invalid number
00000
      

While it's possible to make "printf" work even in such cases, please don't use "printf" in your shell script. Instead, please limit yourself to either bash built-ins (including the wide arena of parameter manipulations, but I don't think any of those make this trivial), and these three programs

Please leave this script in your ~/bin/ subdirectory on 45.56.74.139. It should be there by 11:59pm on Monday, March 30.