Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to write a bash script that would create a directory structure like this:

foo
 | - innerdir1
 | - innerfile1

This action should be repeated multiple times for different directory foo names. Each foo name consists of two parts. So I guess I first need to set environment variables:

    $BAR=something
    $BAZ=anotherthing

And then I need to use them in a script that would produce the mentioned structure with foo's name like $BAR.$BAZ, i.e. something.anotherthing. How do I write such a script?

share|improve this question
add comment

4 Answers 4

for BAR in abc def
do
    for BAZ in ghi jkl mno
    do
        mkdir -p "$BAR.$BAZ/innerdir1"
        touch "$BAR.$BAZ/innerfile1"
    done
done

will create:

abc.ghi
 | - innerdir1
 | - innerfile1
abc.jkl
 | - innerdir1
 | - innerfile1
abc.mno
 | - innerdir1
 | - innerfile1
def.ghi
 | - innerdir1
 | - innerfile1
def.jkl
 | - innerdir1
 | - innerfile1
def.mno
 | - innerdir1
 | - innerfile1
share|improve this answer
add comment

Instead of environment variables, pass your names as arguments and write the script to use these arguments -- that would make a more flexible tool.

Lookup 'getopts' at this bash-guide page and other places.

share|improve this answer
add comment

The script:

#!/bin/bash
if [ -z "$BAR" -o -z "$BAZ" ]; then
  echo "This script needs environment variables $BAR and $BAZ to be set."
  exit 1 
fi

dir=$BAR.$BAZ
mkdir $dir
mkdir $dir/innerdir1
touch $dir/innerfile1

Invoke as:

BAR=bar BAZ=baz ./script.sh

This will create: bar.baz/innerdir1 and bar.baz/innerfile1.

share|improve this answer
    
good idea - but it needs to have $BAR and $BAZ set - it would be better to test these values before starting mkdir. e.g. if [ -n $BAR ] –  Andreas Rehm Dec 9 '10 at 2:06
    
@Andreas: The invocation shown sets those values. However, the script will fail and try to create the . directory if they're not set - so you're correct - the script should include some tests. –  Dennis Williamson Dec 9 '10 at 3:29
    
@Dennis, @Andreas: fixed –  whitequark Dec 9 '10 at 10:32
    
Now that tests for $BAR and $BAZ to not exist as files. It says "if there's a file with the name contained in $BAR and a file with the name contained in $BAZ then issue an error." The test should be [ -z "$BAR" -o -z "$BAZ" ] to test whether the variables have values. –  Dennis Williamson Dec 9 '10 at 19:01
add comment

You should have a look on

http://tldp.org/LDP/abs/html/arrays.html

A simple start would be:

#!/bin/bash

# define foo array
FOOLIST=()

# can be repeated

# create foonames
BAR="something"
BAZ="anotherthing"

# add to foolist
FOOLIST+=("${BAR}.${BAZ}")

BAR="somethingnew"
FOOLIST+=("2${BAR}.${BAZ}")

BAR="somethingnewer"
FOOLIST+=("3${BAR}.${BAZ}")

# create folders from list
for folder in ${FOOLIST[@]}
do
        mkdir $folder
        mkdir $folder/innerdir1
        mkdir $folder/innerfile1
done
share|improve this answer
    
You can add items to an array more simply like this: array+=(item) –  Dennis Williamson Dec 9 '10 at 3:27
    
You're right - this is easier... –  Andreas Rehm Dec 14 '10 at 12:18
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.