0

I am trying out a test script but sadly it's no working.. I am trying to create an x amount of directories based on the users input. I also would like to create a follow number for example if the user inputs the number 5, then 5 individual directories would be created like so; nameofdirectory1, nameofdirectory2 nameofdirectory3 etc.

This is what I have right now;

#!/bin/bash
#Testing1
echo "How many directories do you want"
read INPUT
mkdir -p nameofdirectory{1..$INPUT};

When I execute the script it runs fine but it only makes 1 directory. For example if my input would be 5 then the directory that would be created is: "nameofdirectory1..5" instead of the 5 individual directories I am trying to create. How do I fix this?

erik
  • 1
  • 3
  • 1
    Hi -- could you post the script as formatted code, rather than linking an image. https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question -- thanks –  Jun 19 '20 at 14:17
  • Thank you for your reply mrblewog, I edited my post just now. Have a good day. – erik Jun 19 '20 at 14:37
  • 1
    It seems to be specifically a problem with having the number in a variable. If you hard-coded the 5, for example, it works. Must be related to the order in which variable substitution and `..` expansion are done. Not sure what is best to do about it, but hopefully this advances things a little. – alani Jun 19 '20 at 14:45
  • Lots of discussion at https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash but at a quick scan through, it seems that all of the solutions involve an explicit loop in one for or another. – alani Jun 19 '20 at 14:51
  • Actually, that said, the context of that question was actually wanting to loop, so that doesn't mean that needing a loop is inevitable in this case. You could use seq with a format specifier. Example to follow.... – alani Jun 19 '20 at 14:53
  • Here you go: `mkdir -p $(seq -f "nameofdirectory%g" 1 $INPUT)` – alani Jun 19 '20 at 14:55
  • If there are any `%` signs in the directory name itself, then you will have to double them up: `mkdir -p $(seq -f "name%%of%%directory%g" 1 $INPUT)` – alani Jun 19 '20 at 14:56
  • BTW this won't work if you have spaces in the directory name. But you could always do something like this in that case: `seq 1 $INPUT | xargs -I{} mkdir -p "name of directory {}"` – alani Jun 19 '20 at 15:00
  • 1
    @alaniwi Good afternoon sir, your answer has helped and the script is now up and running with no errors. Thank you for your time and have a good rest of your day sir. – erik Jun 19 '20 at 15:01

2 Answers2

1

The input is being treated as a string, so the creation of a directory with the name nameofdirectory{1..5} is expected.

Try this:

#!/bin/bash
#Testing1
echo "How many directories do you want"
read INPUT

for i in $(seq 1 $INPUT);
do
   mkdir -p nameofdirectory$i;
done;
noam
  • 538
  • 5
  • 19
0

> Answer by @alaniwi

#!/bin/bash
#Testing
echo "How many directories do you want"
read INPUT
mkdir -p $(seq -f "nameofdirectory%g" 1 $INPUT)
erik
  • 1
  • 3