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
  • 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
    Commented Dec 9, 2016 at 18:07
  • 2
    alias mkdir='mkdir -p' if all you're trying to do is save yourself some typing.
    – user4443
    Commented Dec 9, 2016 at 18:15

2 Answers 2

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
2
  • @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
    Commented Dec 9, 2016 at 21:46
  • @KingBoi And upvoting! Commented Dec 10, 2016 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}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .