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
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
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?
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
mkdir namedir 2> /dev/null
ASKER
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.
#!/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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you explain what's the 2 and the &1 for?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2>&1
redirect both stdout and stderr
redirect both stdout and stderr
ASKER
Which is better?
mkdir /abc > /dev/null 2>&1
OR
mkdir namedir 2> /dev/null
mkdir /abc > /dev/null 2>&1
OR
mkdir namedir 2> /dev/null
ASKER
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
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)
i.e
redirect standard out (1) to /dev/null and redirect standard error (2) to the same place 1 went to (/dev/null)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i will try to get back to you all by tomorrow.
if [$? -ne 0 ]
then
echo "error"
fi
Permission is denied if the user does not have write access to the parent directory