bash script ask for an amount and then ask that many prompts

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am getting confused here how to do this. Say I want to make a script that asks how many tracks in a cd and then asks for the track name that many times with each track formatted and exported to a file. I tried this so far but its wrong.



#!/bin/bash

read -p 'How many tracks are there?' TRACKCOUNT
#while not amount of tracks
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
#indirection not a good idea?
#http://mywiki.wooledge.org/BashFAQ/006#Indirection
#ask for track<count> title save to variable TTITLE
read -p 'Track '$(($TRACK + 1))': ' TTITLE
TTITLE="$TTITLE:-No Name"
set TTITLE$TRACK=TTITLE
echo $!TTITLE$TRACK
(( TRACK++ ))
done


What I expect to get is a file such as:



TTITLE1=Uptown Funk!
TTITLE2=Thinking Out Loud
TTITLE3=See You Again


what i get is line 14: $!TTITLE$TRACK: bad substitution
and set | grep TITLE returns nothing so no variable is being set (I think).







share|improve this question





















  • I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
    – FoxSam12
    Apr 18 at 16:15










  • The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
    – FoxSam12
    Apr 18 at 17:29














up vote
0
down vote

favorite












I am getting confused here how to do this. Say I want to make a script that asks how many tracks in a cd and then asks for the track name that many times with each track formatted and exported to a file. I tried this so far but its wrong.



#!/bin/bash

read -p 'How many tracks are there?' TRACKCOUNT
#while not amount of tracks
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
#indirection not a good idea?
#http://mywiki.wooledge.org/BashFAQ/006#Indirection
#ask for track<count> title save to variable TTITLE
read -p 'Track '$(($TRACK + 1))': ' TTITLE
TTITLE="$TTITLE:-No Name"
set TTITLE$TRACK=TTITLE
echo $!TTITLE$TRACK
(( TRACK++ ))
done


What I expect to get is a file such as:



TTITLE1=Uptown Funk!
TTITLE2=Thinking Out Loud
TTITLE3=See You Again


what i get is line 14: $!TTITLE$TRACK: bad substitution
and set | grep TITLE returns nothing so no variable is being set (I think).







share|improve this question





















  • I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
    – FoxSam12
    Apr 18 at 16:15










  • The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
    – FoxSam12
    Apr 18 at 17:29












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am getting confused here how to do this. Say I want to make a script that asks how many tracks in a cd and then asks for the track name that many times with each track formatted and exported to a file. I tried this so far but its wrong.



#!/bin/bash

read -p 'How many tracks are there?' TRACKCOUNT
#while not amount of tracks
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
#indirection not a good idea?
#http://mywiki.wooledge.org/BashFAQ/006#Indirection
#ask for track<count> title save to variable TTITLE
read -p 'Track '$(($TRACK + 1))': ' TTITLE
TTITLE="$TTITLE:-No Name"
set TTITLE$TRACK=TTITLE
echo $!TTITLE$TRACK
(( TRACK++ ))
done


What I expect to get is a file such as:



TTITLE1=Uptown Funk!
TTITLE2=Thinking Out Loud
TTITLE3=See You Again


what i get is line 14: $!TTITLE$TRACK: bad substitution
and set | grep TITLE returns nothing so no variable is being set (I think).







share|improve this question













I am getting confused here how to do this. Say I want to make a script that asks how many tracks in a cd and then asks for the track name that many times with each track formatted and exported to a file. I tried this so far but its wrong.



#!/bin/bash

read -p 'How many tracks are there?' TRACKCOUNT
#while not amount of tracks
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
#indirection not a good idea?
#http://mywiki.wooledge.org/BashFAQ/006#Indirection
#ask for track<count> title save to variable TTITLE
read -p 'Track '$(($TRACK + 1))': ' TTITLE
TTITLE="$TTITLE:-No Name"
set TTITLE$TRACK=TTITLE
echo $!TTITLE$TRACK
(( TRACK++ ))
done


What I expect to get is a file such as:



TTITLE1=Uptown Funk!
TTITLE2=Thinking Out Loud
TTITLE3=See You Again


what i get is line 14: $!TTITLE$TRACK: bad substitution
and set | grep TITLE returns nothing so no variable is being set (I think).









share|improve this question












share|improve this question




share|improve this question








edited Apr 18 at 20:07









cunninghamp3

473215




473215









asked Apr 18 at 16:12









FoxSam12

132




132











  • I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
    – FoxSam12
    Apr 18 at 16:15










  • The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
    – FoxSam12
    Apr 18 at 17:29
















  • I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
    – FoxSam12
    Apr 18 at 16:15










  • The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
    – FoxSam12
    Apr 18 at 17:29















I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
– FoxSam12
Apr 18 at 16:15




I just found stackoverflow.com/a/35592555/4200976 that says to use bash arrays. I will try to see if that works.
– FoxSam12
Apr 18 at 16:15












The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
– FoxSam12
Apr 18 at 17:29




The original question as posted was unclear. I ultimately needed this list as a seperate file anyway so I dont need to have a bunch of variables and then write them I can write them as they are created and re-use the variables.
– FoxSam12
Apr 18 at 17:29










4 Answers
4






active

oldest

votes

















up vote
2
down vote



accepted










UPDATE:



To print to a file as you indicate, here's an updated script:



#!/bin/bash
read -p 'How many tracks are there?' TRACKCOUNT
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
read -p "Track $TRACK: " TTITLE
echo "TTITLE$TRACK=$TTITLE:-No Name" >> ./track_output.txt
(( TRACK++ ))
done



To answer the title of your question, fixing your script to work, and going that little bit farther to spit the results into an array indexed by track number (per your comment):



#!/bin/bash
declare -a TRACK_ARRAY
read -p 'How many tracks are there?' TRACKCOUNT
TRACK=1
while [ $TRACK -le $TRACKCOUNT ]
do
read -p "Track $TRACK: " TTITLE
TRACK_ARRAY[$TRACK]="$TTITLE:-No Name"
(( TRACK++ ))
done

echo "$TRACK_ARRAY[*]"


This takes your input and loops through, correctly asking for TRACK1 through TRACKN where N is the number entered by the user. At the end I simply print all indices of the array, since I have no idea how you are utilizing it. This page is a start (from TLDP) on how you might work with that array in bash



You were indexing incorrectly (would have caught that quickly once you got it to run), but another problem you had was that you were trying to assign a variable name which itself had a $ contained within it: set TTITLE$TRACK=TTITLE - this will not create a variable named TTITLE# where # is the value of $TRACK. I can't confidently tell you what this does.



When trying to use that variable you did this: echo $!TTITLE$TRACK, in which the bang (!) is actually a bang command trying to find the most recent command starting with TTITLE... (not what you intended).






share|improve this answer























  • That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
    – FoxSam12
    Apr 18 at 16:55










  • @FoxSam, check out my update, which echos to a file in the same directory.
    – cunninghamp3
    Apr 18 at 17:01

















up vote
1
down vote













Whenever you find yourself trying to dynamically assign variable names, you should ask yourself if there's a higher-order data structure that can solve your problem. In other words, keep data out of your variable names. I think arrays are a good solution.






share|improve this answer





















  • They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
    – FoxSam12
    Apr 18 at 16:42

















up vote
1
down vote













Commenting to cunninghamp3's answer I realised I don't need to use variables or arrays I can just list everything into a file simply step by step. This is what I ended up with.



#!/bin/bash
i=1
#clear the template file and start blank
echo "#Template file of CD Tracks"> ./template.txt
read -p 'How many tracks are there?' TRACKCOUNT
while [ $i -le $TRACKCOUNT ]
do
read -p 'Track '$i': ' TRACK
TRACK="$TRACK:-Track #"$i""
echo ""TTITLE"$((i-1))"="$TRACK" >> ./template.txt
(( i++ ))
done





share|improve this answer




























    up vote
    0
    down vote













    get the user to write the track titles into a file, one per line.



    in your script read the titles into an array:



    mapfile -t titles < track_title_file.txt


    then process the elements. For example:



    for index in "$!titles[@]"; do
    printf "TITLE%d=%sn" "$index" "$titles[index]"
    done





    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438550%2fbash-script-ask-for-an-amount-and-then-ask-that-many-prompts%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      UPDATE:



      To print to a file as you indicate, here's an updated script:



      #!/bin/bash
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      echo "TTITLE$TRACK=$TTITLE:-No Name" >> ./track_output.txt
      (( TRACK++ ))
      done



      To answer the title of your question, fixing your script to work, and going that little bit farther to spit the results into an array indexed by track number (per your comment):



      #!/bin/bash
      declare -a TRACK_ARRAY
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      TRACK_ARRAY[$TRACK]="$TTITLE:-No Name"
      (( TRACK++ ))
      done

      echo "$TRACK_ARRAY[*]"


      This takes your input and loops through, correctly asking for TRACK1 through TRACKN where N is the number entered by the user. At the end I simply print all indices of the array, since I have no idea how you are utilizing it. This page is a start (from TLDP) on how you might work with that array in bash



      You were indexing incorrectly (would have caught that quickly once you got it to run), but another problem you had was that you were trying to assign a variable name which itself had a $ contained within it: set TTITLE$TRACK=TTITLE - this will not create a variable named TTITLE# where # is the value of $TRACK. I can't confidently tell you what this does.



      When trying to use that variable you did this: echo $!TTITLE$TRACK, in which the bang (!) is actually a bang command trying to find the most recent command starting with TTITLE... (not what you intended).






      share|improve this answer























      • That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
        – FoxSam12
        Apr 18 at 16:55










      • @FoxSam, check out my update, which echos to a file in the same directory.
        – cunninghamp3
        Apr 18 at 17:01














      up vote
      2
      down vote



      accepted










      UPDATE:



      To print to a file as you indicate, here's an updated script:



      #!/bin/bash
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      echo "TTITLE$TRACK=$TTITLE:-No Name" >> ./track_output.txt
      (( TRACK++ ))
      done



      To answer the title of your question, fixing your script to work, and going that little bit farther to spit the results into an array indexed by track number (per your comment):



      #!/bin/bash
      declare -a TRACK_ARRAY
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      TRACK_ARRAY[$TRACK]="$TTITLE:-No Name"
      (( TRACK++ ))
      done

      echo "$TRACK_ARRAY[*]"


      This takes your input and loops through, correctly asking for TRACK1 through TRACKN where N is the number entered by the user. At the end I simply print all indices of the array, since I have no idea how you are utilizing it. This page is a start (from TLDP) on how you might work with that array in bash



      You were indexing incorrectly (would have caught that quickly once you got it to run), but another problem you had was that you were trying to assign a variable name which itself had a $ contained within it: set TTITLE$TRACK=TTITLE - this will not create a variable named TTITLE# where # is the value of $TRACK. I can't confidently tell you what this does.



      When trying to use that variable you did this: echo $!TTITLE$TRACK, in which the bang (!) is actually a bang command trying to find the most recent command starting with TTITLE... (not what you intended).






      share|improve this answer























      • That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
        – FoxSam12
        Apr 18 at 16:55










      • @FoxSam, check out my update, which echos to a file in the same directory.
        – cunninghamp3
        Apr 18 at 17:01












      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      UPDATE:



      To print to a file as you indicate, here's an updated script:



      #!/bin/bash
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      echo "TTITLE$TRACK=$TTITLE:-No Name" >> ./track_output.txt
      (( TRACK++ ))
      done



      To answer the title of your question, fixing your script to work, and going that little bit farther to spit the results into an array indexed by track number (per your comment):



      #!/bin/bash
      declare -a TRACK_ARRAY
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      TRACK_ARRAY[$TRACK]="$TTITLE:-No Name"
      (( TRACK++ ))
      done

      echo "$TRACK_ARRAY[*]"


      This takes your input and loops through, correctly asking for TRACK1 through TRACKN where N is the number entered by the user. At the end I simply print all indices of the array, since I have no idea how you are utilizing it. This page is a start (from TLDP) on how you might work with that array in bash



      You were indexing incorrectly (would have caught that quickly once you got it to run), but another problem you had was that you were trying to assign a variable name which itself had a $ contained within it: set TTITLE$TRACK=TTITLE - this will not create a variable named TTITLE# where # is the value of $TRACK. I can't confidently tell you what this does.



      When trying to use that variable you did this: echo $!TTITLE$TRACK, in which the bang (!) is actually a bang command trying to find the most recent command starting with TTITLE... (not what you intended).






      share|improve this answer















      UPDATE:



      To print to a file as you indicate, here's an updated script:



      #!/bin/bash
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      echo "TTITLE$TRACK=$TTITLE:-No Name" >> ./track_output.txt
      (( TRACK++ ))
      done



      To answer the title of your question, fixing your script to work, and going that little bit farther to spit the results into an array indexed by track number (per your comment):



      #!/bin/bash
      declare -a TRACK_ARRAY
      read -p 'How many tracks are there?' TRACKCOUNT
      TRACK=1
      while [ $TRACK -le $TRACKCOUNT ]
      do
      read -p "Track $TRACK: " TTITLE
      TRACK_ARRAY[$TRACK]="$TTITLE:-No Name"
      (( TRACK++ ))
      done

      echo "$TRACK_ARRAY[*]"


      This takes your input and loops through, correctly asking for TRACK1 through TRACKN where N is the number entered by the user. At the end I simply print all indices of the array, since I have no idea how you are utilizing it. This page is a start (from TLDP) on how you might work with that array in bash



      You were indexing incorrectly (would have caught that quickly once you got it to run), but another problem you had was that you were trying to assign a variable name which itself had a $ contained within it: set TTITLE$TRACK=TTITLE - this will not create a variable named TTITLE# where # is the value of $TRACK. I can't confidently tell you what this does.



      When trying to use that variable you did this: echo $!TTITLE$TRACK, in which the bang (!) is actually a bang command trying to find the most recent command starting with TTITLE... (not what you intended).







      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited Apr 18 at 17:36


























      answered Apr 18 at 16:48









      cunninghamp3

      473215




      473215











      • That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
        – FoxSam12
        Apr 18 at 16:55










      • @FoxSam, check out my update, which echos to a file in the same directory.
        – cunninghamp3
        Apr 18 at 17:01
















      • That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
        – FoxSam12
        Apr 18 at 16:55










      • @FoxSam, check out my update, which echos to a file in the same directory.
        – cunninghamp3
        Apr 18 at 17:01















      That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
      – FoxSam12
      Apr 18 at 16:55




      That is close. I am trying to output this to a file with each line consisting of something simmilar to TTITLE1=Uptown Funk! etc... I'll work on that and see if I can figure it out.
      – FoxSam12
      Apr 18 at 16:55












      @FoxSam, check out my update, which echos to a file in the same directory.
      – cunninghamp3
      Apr 18 at 17:01




      @FoxSam, check out my update, which echos to a file in the same directory.
      – cunninghamp3
      Apr 18 at 17:01












      up vote
      1
      down vote













      Whenever you find yourself trying to dynamically assign variable names, you should ask yourself if there's a higher-order data structure that can solve your problem. In other words, keep data out of your variable names. I think arrays are a good solution.






      share|improve this answer





















      • They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
        – FoxSam12
        Apr 18 at 16:42














      up vote
      1
      down vote













      Whenever you find yourself trying to dynamically assign variable names, you should ask yourself if there's a higher-order data structure that can solve your problem. In other words, keep data out of your variable names. I think arrays are a good solution.






      share|improve this answer





















      • They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
        – FoxSam12
        Apr 18 at 16:42












      up vote
      1
      down vote










      up vote
      1
      down vote









      Whenever you find yourself trying to dynamically assign variable names, you should ask yourself if there's a higher-order data structure that can solve your problem. In other words, keep data out of your variable names. I think arrays are a good solution.






      share|improve this answer













      Whenever you find yourself trying to dynamically assign variable names, you should ask yourself if there's a higher-order data structure that can solve your problem. In other words, keep data out of your variable names. I think arrays are a good solution.







      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered Apr 18 at 16:34









      hhoke1

      31416




      31416











      • They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
        – FoxSam12
        Apr 18 at 16:42
















      • They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
        – FoxSam12
        Apr 18 at 16:42















      They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
      – FoxSam12
      Apr 18 at 16:42




      They are but I don't understand how to use the array in the next step as variable names. I can create an array with TTITLE1 ... etc but how do I loop through the array and ask for userinput to assign a value using the array as the variable names?
      – FoxSam12
      Apr 18 at 16:42










      up vote
      1
      down vote













      Commenting to cunninghamp3's answer I realised I don't need to use variables or arrays I can just list everything into a file simply step by step. This is what I ended up with.



      #!/bin/bash
      i=1
      #clear the template file and start blank
      echo "#Template file of CD Tracks"> ./template.txt
      read -p 'How many tracks are there?' TRACKCOUNT
      while [ $i -le $TRACKCOUNT ]
      do
      read -p 'Track '$i': ' TRACK
      TRACK="$TRACK:-Track #"$i""
      echo ""TTITLE"$((i-1))"="$TRACK" >> ./template.txt
      (( i++ ))
      done





      share|improve this answer

























        up vote
        1
        down vote













        Commenting to cunninghamp3's answer I realised I don't need to use variables or arrays I can just list everything into a file simply step by step. This is what I ended up with.



        #!/bin/bash
        i=1
        #clear the template file and start blank
        echo "#Template file of CD Tracks"> ./template.txt
        read -p 'How many tracks are there?' TRACKCOUNT
        while [ $i -le $TRACKCOUNT ]
        do
        read -p 'Track '$i': ' TRACK
        TRACK="$TRACK:-Track #"$i""
        echo ""TTITLE"$((i-1))"="$TRACK" >> ./template.txt
        (( i++ ))
        done





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Commenting to cunninghamp3's answer I realised I don't need to use variables or arrays I can just list everything into a file simply step by step. This is what I ended up with.



          #!/bin/bash
          i=1
          #clear the template file and start blank
          echo "#Template file of CD Tracks"> ./template.txt
          read -p 'How many tracks are there?' TRACKCOUNT
          while [ $i -le $TRACKCOUNT ]
          do
          read -p 'Track '$i': ' TRACK
          TRACK="$TRACK:-Track #"$i""
          echo ""TTITLE"$((i-1))"="$TRACK" >> ./template.txt
          (( i++ ))
          done





          share|improve this answer













          Commenting to cunninghamp3's answer I realised I don't need to use variables or arrays I can just list everything into a file simply step by step. This is what I ended up with.



          #!/bin/bash
          i=1
          #clear the template file and start blank
          echo "#Template file of CD Tracks"> ./template.txt
          read -p 'How many tracks are there?' TRACKCOUNT
          while [ $i -le $TRACKCOUNT ]
          do
          read -p 'Track '$i': ' TRACK
          TRACK="$TRACK:-Track #"$i""
          echo ""TTITLE"$((i-1))"="$TRACK" >> ./template.txt
          (( i++ ))
          done






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 18 at 17:26









          FoxSam12

          132




          132




















              up vote
              0
              down vote













              get the user to write the track titles into a file, one per line.



              in your script read the titles into an array:



              mapfile -t titles < track_title_file.txt


              then process the elements. For example:



              for index in "$!titles[@]"; do
              printf "TITLE%d=%sn" "$index" "$titles[index]"
              done





              share|improve this answer

























                up vote
                0
                down vote













                get the user to write the track titles into a file, one per line.



                in your script read the titles into an array:



                mapfile -t titles < track_title_file.txt


                then process the elements. For example:



                for index in "$!titles[@]"; do
                printf "TITLE%d=%sn" "$index" "$titles[index]"
                done





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  get the user to write the track titles into a file, one per line.



                  in your script read the titles into an array:



                  mapfile -t titles < track_title_file.txt


                  then process the elements. For example:



                  for index in "$!titles[@]"; do
                  printf "TITLE%d=%sn" "$index" "$titles[index]"
                  done





                  share|improve this answer













                  get the user to write the track titles into a file, one per line.



                  in your script read the titles into an array:



                  mapfile -t titles < track_title_file.txt


                  then process the elements. For example:



                  for index in "$!titles[@]"; do
                  printf "TITLE%d=%sn" "$index" "$titles[index]"
                  done






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Apr 18 at 17:49









                  glenn jackman

                  45.9k265100




                  45.9k265100






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438550%2fbash-script-ask-for-an-amount-and-then-ask-that-many-prompts%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?