Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

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}/
share|improve this answer
    
great! it works! –  user77473 Apr 7 '11 at 12:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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