Beginner's sh shell problem

Hello everyone,

This is a very quick question...

Part of a script that I am writing is supposed to create several directories and sub directories in these directories. The code below is working fine in a bash shell but not in a sh shell: mkdir -p /webtest/{web1_com,web2_org,web3_biz,web4_co_uk}/{http,logs,tmp}

In my FreeBSD console, the line above creates a single {web1_com,web2_org,web3_biz,web4_co_uk} directory instead of 4.

Could anyone help me to work out the syntax issue that I am having please?

Thank you
Fred
 
The /bin/sh shell doesn't understand grouping with {} of files and simply treats it as part of a filename. It's the shell that interprets it, not the mkdir(1) command. I would suggest using a for loop or making use of mtree(8).
 
Something like this should do the trick:
Code:
#!/bin/sh

for i in web1_com web2_org web3_biz web4_co_uk
do
  for j in http logs tmp
  do
    mkdir -p /webtest/$i/$j
  done
done
 
fred974 said:
It all look so logical when I see the solution..
Yeah, this was the easiest and simplest solution I could think off. Simple solutions usually work best (KISS principle ;) ). Using mtree(8) is a little bit more complicated but it's also a lot more powerful.
 
Back
Top