2

I want to create maildirs with unix script, so question is how to create directory like in this example:

   example.com/j/o/h/john-2011.04.07.09.35.49/

if i have only three arguments - mailserver (example.com) and username (john) and time I don`t know how to make that "split part" for unix script, how to extract first three letters for username john look like example above. Thank you in advance!

coredump
  • 12,713
  • 2
  • 36
  • 56
user77473
  • 41
  • 1
  • 3

1 Answers1

3

In bash you can use the ${} substring match to get a single letter from a string:

coredump@anita:~$ x="john"; echo ${x:0:1}
j
coredump@anita:~$ x="john"; echo ${x:1:1}
o
coredump@anita:~$ x="john"; echo ${x:2:1}
h

So in your script you can assign those letters to variables and use it on the mkdir commands to create your directory structure, something like this I suppose:

FIRST=${USERNAME:0:1}
SECOND=${USERNAME:1:1}
THIRD=${USERNAME:2:1}

mkdir $SERVER/$FIRST/$SECOND/$THIRD/${USERNAME}-${DATE}/
coredump
  • 12,713
  • 2
  • 36
  • 56