-1

The Explanation:

It is part of a study I am doing. I am not allowed to change the filenames of the mp3 files, I have to somehow do this from the script and keep it as short a script as possible. The script itself is a menu that will have the user select a chapter, then the part of the chapter and from there on they can listen to the text in my language, transliteration in their language and then the sentence in their language/alphabet.

I have to simplify matters by creating a script that makes a menu inside bash. All works quite well and I automated a lot already. So that part is done and I don't need the question answered for the problem below as this basically works as it is.

And it has to be bash scripted, not python or anything else.

Also English is not my native language, so please bare with me!


The Situation:

So I ran into a situation where I need to add the function to play audio files inside a menu script. I am unsure how to explain it any better than by giving examples.

Basically all works well except for one thing.

I have a set series of mp3 files ranging from 001001.mp3 to 114006.mp3

They used their naming convention to have the first three numbers as chapter, last three as parts of the chapter. To me it is odd as I would have created a different structure and can't right now as these files are being used already.

Now I can get the numbers of the first three automatically by generating those numbers by input (read -p ": " chapternumber). But the second is more tedious to get inside the script as I count from 1 to a maximum needed of 300 but without leading zeros, 0 or 00. And I can't simply use read -p to achieve this as it will automate based on the input of the numbers I give it.

Example of filenames:

001001.mp3 # chapter 001 part 001
001015.mp3 # chapter 001 part 015
067012.mp3 # chapter 067 part 012
277103.mp3 # chapter 277 part 103

Below is the script that I am using in order to print the bash menu selection

I ask immediately to forget about the first three-digit part of the number, this has been covered already!!

i=1
while [ $i -le 300 ]
    do
        echo "  printf \"$i) : Chapter $chapter Part $i\n\""
        ((i++))
    done

So this basically results in

1) Chapter 001 part 1
2) Chapter 001 part 2
3) Chapter 001 part 3
4) Chapter 001 part 4
5) Chapter 001 part 5
6) Chapter 001 part 6
7) Chapters part 7
...
43) Chapters part 43
44) Chapters part 44
45) Chapters part 45
...
100) Chapters part 100
201) Chapters part 201
etc

Given the restraints because of the filename I am unsure how to edit that script snippet so I can get 001, 002, etc as the last three digits of that name, so force it to use three digits by one or two leading zeros into this:

001
002
003
...
014
015
...
057
...
103
284
etc

I hope anyone can help me on this and understand what I wish for, and perhaps help me to ask this question better. :)

Cyrus
  • 77,979
  • 13
  • 71
  • 125

1 Answers1

1

If leading zero is what you wanted maybe this.

#!/usr/bin/env bash

i=1
while ((i <= 300)); do
  printf -v "leading_zero" %03d "$i"
  printf '%s) Chapter %s Part %s\n' "$i" "$chapter" "$leading_zero"
  ((i++))
done

Or at least that is how understood what you wanted.

Jetchisel
  • 5,722
  • 2
  • 14
  • 15