Find the answer to your Linux question:
Results 1 to 2 of 2
I have created a script that finds a directory and subdirectories in my home directory and creates a copy of the directory structure in my home directory (i.e. just the ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    mkdir in script not working

    I have created a script that finds a directory and subdirectories in my home directory and creates a copy of the directory structure in my home directory (i.e. just the empty directories, no files). The script is adapted from the book Classic Shell Scripting, by Robbins and Beebe. For anyone who has the book the original script is at the bottom of pg 49.

    This is my script:


    Code:
    #!/bin/bash
    find /home/canopus/LearningPerl2/ -type d -print |
    sed 's;/home/canopus/LearningPerl2/;~/lt/;' |
    sed 's/^/"mkdir -p" /'
    I have turned on execution tracing to see what is produced and it all looks OK, viz.:

    Code:
    mkdir -p ~/lt/
    mkdir -p ~/lt/chapter1
    mkdir -p ~/lt/chapter2
    mkdir -p ~/lt/chapter3
    However, I find that the parent directory (lt) has not been created. If I type the first mkdir command shown above from the command line, it is created.

    Anyone any idea why it doesn't work in the script but does from the command line.

    Bash version is

    Code:
    $ bash --version
    GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
    Copyright (C) 2009 Free Software Foundation, Inc.
    Many thanks

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52

    You haven't executed the code. Pipe the output to sh:

    Code:
    find /home/canopus/LearningPerl2/ -type d -print |
    sed 's;/home/canopus/LearningPerl2/;~/lt/;' |
    sed 's/^/"mkdir -p" /' | sh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...