0

I'm writing a script that replaces the mkdir -p command. Here is what I have so far:

#!/bin/sh  

n=1;
max=50;
while [ "$n" -le "$max" ]; do
mkdir "s$n"
n='expr "$n" + 1';
done

I should be able to then use this script to type in aaa/bbb/ccc and it'll make these directories.

  • 2
    Which -p "command" are you referring to? Can you provide input and expected output? What does not work in what you have thus far tried? – DopeGhoti Dec 9 '16 at 18:07
  • 2
    alias mkdir='mkdir -p' if all you're trying to do is save yourself some typing. – user4443 Dec 9 '16 at 18:15
3

execute the script as below.

./script.sh abc/def/ghi/jkl

#!/bin/bash

DIR_LIST=${1}

IFS=/
for DIR_NAME in ${DIR_LIST}
do
    mkdir ${DIR_NAME}
    cd ${DIR_NAME}
done
  • @KingBoi If you are satisfied and this solve your question, you should consider to take a minute to thanks Kamaraj by accepting his answer. – andreatsh Dec 9 '16 at 21:46
  • @KingBoi And upvoting! – DepressedDaniel Dec 10 '16 at 1:27
1

Using a recursive function:

#!/bin/bash
mkdir_p() {
    local path=$1
    local parent=${path%/*}
    [[ ${path} != /* ]] && parent=${PWD}/${parent}
    [[ ! -d $parent ]] && mkdir_p "${parent}"
    [[ ! -d $path ]] && mkdir "${path}"
}
mkdir_p "${1}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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