?
Solved

Permission

Posted on 2004-10-06
14
Medium Priority
?
298 Views
Last Modified: 2010-04-22
I am trying to write a bash script to create a directory on a directory as specified by the first parameter $1.
I know how to create a directory but then how to echo an error if the user has got no permission to create the directory on the specified directory?

Why is there permission denied? What are the requirements for the user to be able to create a directory on it?

Thanks a lot
0
Comment
Question by:cutie2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 12238269
mkdir $1
if [$? -ne 0 ]
then
    echo "error"
fi

Permission is denied if the user does not have write access to the parent directory
0
 

Author Comment

by:cutie2000
ID: 12245942
thanks..
but then how to suppress the standard error?
there could have 2 types of error.
1 is the permission denied error and the other is the directory already existed error.
how to separate them and print out the person error messages accordingly?
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12245996
You can use standart error redirection in this case I sended standart error to /dev/null.

mkdir namedir 2> /dev/null
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:cutie2000
ID: 12246019
I have this piece of code

#!/bin/bash

mkdir /abc > /dev/null
if [ ! $? -eq 0 ]
then
    echo "Error"
else
    echo "Done"
fi


But then it still shows the standard error message.
0
 
LVL 18

Accepted Solution

by:
liddler earned 100 total points
ID: 12246055
mkdir /abc > /dev/null only redirects STDOUT to redirect STDERR as well use
mkdir /abc > /dev/null 2>&1
0
 

Author Comment

by:cutie2000
ID: 12246069
Can you explain what's the 2 and the &1 for?
0
 
LVL 10

Assisted Solution

by:Luxana
Luxana earned 100 total points
ID: 12246078
to redirect standart error you need "2"

mkdir namedir 2> /dev/null

----------------

why you need to echo it standart error is going to stdin by default?
 
./script
mkdir: cannot create directory `/root/abc': Permission denied

#!/bin/bash

mkdir /root/abc
if [ ! $? -eq 0 ]
then
    echo ""
    else
        echo "Done"
        fi

to cerate directory in current directory do not use forget "."

#!/bin/bash

mkdir ./abc
if [ ! $? -eq 0 ]
then
    echo ""
    else
        echo "Done"
        fi


0
 
LVL 10

Expert Comment

by:Luxana
ID: 12246088
2>&1

redirect both stdout and stderr
0
 

Author Comment

by:cutie2000
ID: 12246091
Which is better?

mkdir /abc > /dev/null 2>&1

OR

mkdir namedir 2> /dev/null
0
 

Author Comment

by:cutie2000
ID: 12246094
then why do we use a & ?
0
 
LVL 10

Expert Comment

by:Luxana
ID: 12246378
Which is better?

mkdir /abc > /dev/null 2>&1

OR

mkdir namedir 2> /dev/null

-----------------

mkdir namedir 2> /dev/null
---------------------------------------------
this should work...you probalbly do not have permitions because you try create dir
/abc  -> this is absolute path , here you are trying make direcotry on / partition
you probali want :
/home/user/abc
or use "." to create directory in current directory where you are running script
./abc -> relative path


#!/bin/bash

mkdir ./abc
if [  $? -eq 0 ]
then
        echo "Done"
                fi
0
 
LVL 18

Expert Comment

by:liddler
ID: 12246495
Basically the & means STDOUT & STDERR - Standard output and standard error
i.e
redirect standard out (1) to /dev/null and redirect standard error (2) to the same place 1 went to (/dev/null)
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 80 total points
ID: 12246793
Hi cutie2000,
Have a look at the -p option of mkdir.

This allows you to create a full path of subdirectories in one go. Existing directories do not result in an error message, only permission denied.

Cheers!

Stefan
0
 

Author Comment

by:cutie2000
ID: 12250359
i will try to get back to you all by tomorrow.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question