Solved

Permission

Posted on 2004-10-06
14
281 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 25 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 25 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 20 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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