-1

What I need to do is create a script that makes 15 directories (named from 1 to 15) and inside each directory I have to have a letter assign to it (in alphabetical order).

So after I run the script it should be like this:

Directory 1 contains letter A; Directory 2 contains B and so on until it reaches number 15 and has the letter O in it.. after that it should stop.

closed as off-topic by cuonglm, roaima, Scott, Jakuje, Anthon Jan 29 '16 at 19:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – roaima, Scott, Anthon

  • 1
    Well, I'm too lazy to write up an example, but you will want to create a while (or for) loop, a variable that increments (starts at 0, increments to +1 on first one, and you use this variable to name the directories) that runs 15 times, creating a directory each time. I don't really understand what you mean by putting letters inside each directory, but if you mean files named in that theme, then you could create a switch (case) statement for each number executing the command to create that file with the corresponding letter to the number. That's how I'd do it in a hurry anyhow. – Cestarian Jan 29 '16 at 15:00
  • 2
    askubuntu.com/q/727191/80864 – choroba Jan 29 '16 at 15:04
  • Apart from no effort, you don't explain what it means for you that a directory has a letter assigned to it inside. Is that the same for you as containing a letter? How does that correspond to the more usual things a directory contains like other directories, links (hard and soft), devices, and files. – Anthon Jan 29 '16 at 19:24
2

Not bash but awk, and a one-liner at that.

echo {A..O} | xargs -n 1 | awk '{system("mkdir "NR" && touch "NR"/"$1)}'
  • Hehe I stay away from awk (and sed) whenever possible since I don't understand how they work at all, but that really is nice. – Cestarian Jan 29 '16 at 15:30
  • I always try and choose the slickest tool for the job. :) – parkamark Jan 29 '16 at 15:33
  • also, you technically are still using bash. You could copy this line into a .sh file, it'd work all the same. it just uses two more programs than other options, not that that's bad. – Cestarian Jan 29 '16 at 17:59
  • Yes, the only part(s) which use any bash functionality are the pipes (obviously) and the echo expansion trick to list A to O. For older versions of bash, that might not work, in which case one would have to either list each letter manually or use the seq method instead, as posted by Dennis. – parkamark Jan 29 '16 at 18:07
1
letters=(0 A B C D E F G H I J K L M N O)
i=1
while [ ${i} -le 15 ]
do
  mkdir ${i}
  touch ${i}/${letters[${i}]}
  (( i++ ))
done

Explanation: letters is an array containing the letters corresponding to numbers. First entry in an array is at index 0. Since your numbers start from 1, I put a padding character. Whatever it is, doesn't matter

while i changes value between 1 and 15, it will execute mkdir command with i being the directory name.

then inside each directory, it will create a file, name of which will be the corresponding letter, from the letters array

1

This is how I'd create the directories containing files with corresponding letters:

 #!/bin/bash 
 declare -r LETTERS=(A B C D E F G H I J K L M N O) # Read Only Letters Array
 declare -i COUNTER=0 # Integer Variable "COUNTER"

 while [  $COUNTER -lt 15 ]; do # While Counter Less Than 15 Do...
     COUNTER+=1 # Add 1 to counter
     mkdir $COUNTER # Make the directory
     echo > $COUNTER/${LETTERS[$COUNTER-1]} # Create the file
 done

The reason I use echo is that I've had problems with touch in the past (and echo does the job just fine)

I initially would have thought to use switch cases (I am so out of touch), but I used MelBurslan's example to use an array instead.

  • Shouldn't it be ((COUNTER+=1))? – Mohammad Jan 29 '16 at 15:24
  • @mohammad.k nope, you could do (( COUNTER++ )) like MelBurslan did though (which does require the parentheses to work) but if you're gonna do the +=1 way like I did, you don't need parentheses. Either way, it makes no difference – Cestarian Jan 29 '16 at 15:26
  • Ah, it is because you have used declare -i. I didn't see that. :) – Mohammad Jan 29 '16 at 15:32
  • @mohammad.k that makes no difference actually, I just like declaring things properly, it's a good habit. Never hurts to be explicit when you're coding. – Cestarian Jan 29 '16 at 15:34
  • No. COUNTER+=1 will cause COUNTER to be 01, and then 011, and so on. – Mohammad Jan 29 '16 at 15:38
1

Try this in bash,

seq  1 15 | awk '{ printf "mkdir %d && touch %d/%c \n", $0,$0, $0+64; }' | sh
1
#!/bin/bash
set   -- {A..O}
j=0
for   i
do    j=$((j+1))
      mkdir "Dir$j"
      touch "Dir$j/$i"
done

This code use the positional arguments to do its work, thus those get erased (nothing important, but good to know).

  • that looks very damn good. – Cestarian Jan 29 '16 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.