Solved

Permission

Posted on 2004-10-06
14
267 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
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 18

Expert Comment

by:liddler
Comment Utility
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
Comment Utility
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
Comment Utility
You can use standart error redirection in this case I sended standart error to /dev/null.

mkdir namedir 2> /dev/null
0
 

Author Comment

by:cutie2000
Comment Utility
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 25 total points
Comment Utility
mkdir /abc > /dev/null only redirects STDOUT to redirect STDERR as well use
mkdir /abc > /dev/null 2>&1
0
 

Author Comment

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

Assisted Solution

by:Luxana
Luxana earned 25 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Expert Comment

by:Luxana
Comment Utility
2>&1

redirect both stdout and stderr
0
 

Author Comment

by:cutie2000
Comment Utility
Which is better?

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

OR

mkdir namedir 2> /dev/null
0
 

Author Comment

by:cutie2000
Comment Utility
then why do we use a & ?
0
 
LVL 10

Expert Comment

by:Luxana
Comment Utility
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
Comment Utility
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 20 total points
Comment Utility
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
Comment Utility
i will try to get back to you all by tomorrow.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now