hey all. Im writing a small script that is supposed to check for a file or directory and when its not there create it. After it creates the file or directory I would like the script to loop to the beginning but I cant figure it out. What am I doing wrong? Here is my script that works.

#!/bin/bash

# Michael Duquette, Edward Correa, Paul Crew, Arthur Cramer

# Checks to see if directory exists and if not creates the directory


echo -e "Please Enter the directory you wish to create: "
read dir

if [ ! -d ./$dir ]

then

mkdir ./$dir
echo -n "The directory "
echo -n "$dir"
echo -n " has been created. "
else

echo -n "I am sorry "
echo -n "$dir"
echo -n " already exists."
fi


Any help would be greatly appreciated. We havent learned loops in UNIX but I have in JAVA but I cant get it to work. This script doesnt require a loop but I believe it would be easier than initalizing it over and over again. Thanks alot.

You're missing the loop. Think "while"

>creates the file or directory
A menu to choose what to create could be an option in this situation.
Read here how to create menu

Meanwhile if a loop will do you need to think how to exit the loop.
Something like...

until [ "1" = "0" ]; do #forever loop to be stopped by Quit
    echo -e "Please enter the directory you wish to create: "
    read dir
    if [ ! -d ./$dir ]; then
        mkdir ./$dir
        echo "The directory $dir has been created."
    else
        echo "I am sorry $dir already exists."
    fi
    echo -n "Write \"Quit\" if you don't want to continue: "
    read answer
    if [ "$answer" == "Quit" ]; then
        echo -e "Exiting..."
        exit 0
    fi
done

hey all. Im writing a small script that is supposed to check for a file or directory and when its not there create it. After it creates the file or directory I would like the script to loop to the beginning but I cant figure it out. What am I doing wrong?

A slightly different way. Just blindly create all the directories in the path and check the command's return code for success or failure. It's not exactly what you posted, but does serve to illustrate a little more of shell programming.

#!/bin/bash

# Michael Duquette, Edward Correa, Paul Crew, Arthur Cramer

# Checks to see if directory exists and if not creates the directory

PROMPT="Please Enter the directory you wish to create:"
echo "Press <CTRL/D> when done..."

echo -n $PROMPT
while read dir; do
  mkdir -p ./$dir
  if [ $? -ne 0 ]; then
    echo "The directory './$dir' could not be created. So sorry!"
  fi
  echo -n $PROMPT
done

echo
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.