Logo 
Search:

Unix / Linux / Ubuntu Articles

Submit Article
Home » Articles » Unix / Linux / Ubuntu » Homework HelpRSS Feeds

Shell script to check dir directory if it exists then change the path, if not then create dir with that name and also check for file name not exists

Posted By: Jarvinia Miller     Category: Unix / Linux / Ubuntu     Views: 13812

Suppose a full path like dir1/dir2/dir3/mydir is supplied during the execution of a shell script where dir1,dir2,dir3 and mydir are directory names. Write a shell script, which creates these directories if they do not exist and change to the last directory in the list. Also check for the possibility that there might exist a file with the same name as any of the directory names present in the supplied path.

Code for Shell script to check dir directory if it exists then change the path, if not then create dir with that name and also check for file name not exists in Unix / Linux / Ubuntu

echo "Enter dir path : "
read path
set `echo $path | tr '/'' '`

for dir in $*
doif [ -d $dir ]
    then
        echo "Direcoty present"
        cd $dir
    elif [ -f $dir ]
    then
        echo "Their is file"
        exit 1    
    else
        echo "Not present dir so Create dir"
        mkdir $dir
        cd $dir
    fi    
done 
  
Share: 



Jarvinia Miller
Jarvinia Miller author of Shell script to check dir directory if it exists then change the path, if not then create dir with that name and also check for file name not exists is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!