Link to home
Start Free TrialLog in
Avatar of cutie2000
cutie2000

asked on

Permission

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
Avatar of liddler
liddler
Flag of Ireland image

mkdir $1
if [$? -ne 0 ]
then
    echo "error"
fi

Permission is denied if the user does not have write access to the parent directory
Avatar of cutie2000
cutie2000

ASKER

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

mkdir namedir 2> /dev/null
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.
ASKER CERTIFIED SOLUTION
Avatar of liddler
liddler
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you explain what's the 2 and the &1 for?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
2>&1

redirect both stdout and stderr
Which is better?

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

OR

mkdir namedir 2> /dev/null
then why do we use a & ?
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
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)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i will try to get back to you all by tomorrow.