You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-user@hadoop.apache.org by L N <nk...@gmail.com> on 2013/03/23 18:42:34 UTC

Delete a hdfs directory if it already exists in a shell script.

Hi,

I am trying to delete a hdfs directory if it already exists in a shell
script.

I wrote below command in a shell script

DIRECTORY=`date +%m%d%Y`
TestDir= hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
echo $TestDir
if [ $TestDir  -eq  0 ]; then
               hadoop fs -rmr /user/lnindrakrishna/$DIRECTORY
               echo "Directory Deleted"
else
               echo "Directory does not Exist"
           fi
exec hadoop fs  -mkdir /user/lnindrakrishna/`date +%m%d%Y`



I get below output

[lnindrakrishna@lvshdc2en0011 ~]$ sh PXP_EAP_Process.sh

PXP_EAP_Process.sh: line 9: [: -eq: unary operator expected
Directory does not Exist
mkdir: cannot create directory /user/lnindrakrishna/03232013: File exists



Looks like  hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY is returning
NULL and that is the reason it throws null value for echo $TestDir  and it
goes to else part and displays  "Directory does not Exist"


What is wrong in the above shell script that I have written


-

Re: Delete a hdfs directory if it already exists in a shell script.

Posted by Harsh J <ha...@cloudera.com>.
The -test utility of the "fs" shell does not return outputs, but is to
be used via exit codes. See
http://tldp.org/LDP/abs/html/exit-status.html for more reading
material on what an exit code implies.

Hence, you should be doing it this way:

hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
TestDir=$?
…

… for your script to work.

On Sat, Mar 23, 2013 at 11:12 PM, L N <nk...@gmail.com> wrote:
> Hi,
>
> I am trying to delete a hdfs directory if it already exists in a shell
> script.
>
> I wrote below command in a shell script
>
> DIRECTORY=`date +%m%d%Y`
> TestDir= hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
> echo $TestDir
> if [ $TestDir  -eq  0 ]; then
>                hadoop fs -rmr /user/lnindrakrishna/$DIRECTORY
>                echo "Directory Deleted"
> else
>                echo "Directory does not Exist"
>            fi
> exec hadoop fs  -mkdir /user/lnindrakrishna/`date +%m%d%Y`
>
>
>
> I get below output
>
> [lnindrakrishna@lvshdc2en0011 ~]$ sh PXP_EAP_Process.sh
>
> PXP_EAP_Process.sh: line 9: [: -eq: unary operator expected
> Directory does not Exist
> mkdir: cannot create directory /user/lnindrakrishna/03232013: File exists
>
>
>
> Looks like  hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY is returning
> NULL and that is the reason it throws null value for echo $TestDir  and it
> goes to else part and displays  "Directory does not Exist"
>
>
> What is wrong in the above shell script that I have written
>
>
> -



--
Harsh J

Re: Delete a hdfs directory if it already exists in a shell script.

Posted by Harsh J <ha...@cloudera.com>.
The -test utility of the "fs" shell does not return outputs, but is to
be used via exit codes. See
http://tldp.org/LDP/abs/html/exit-status.html for more reading
material on what an exit code implies.

Hence, you should be doing it this way:

hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
TestDir=$?
…

… for your script to work.

On Sat, Mar 23, 2013 at 11:12 PM, L N <nk...@gmail.com> wrote:
> Hi,
>
> I am trying to delete a hdfs directory if it already exists in a shell
> script.
>
> I wrote below command in a shell script
>
> DIRECTORY=`date +%m%d%Y`
> TestDir= hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
> echo $TestDir
> if [ $TestDir  -eq  0 ]; then
>                hadoop fs -rmr /user/lnindrakrishna/$DIRECTORY
>                echo "Directory Deleted"
> else
>                echo "Directory does not Exist"
>            fi
> exec hadoop fs  -mkdir /user/lnindrakrishna/`date +%m%d%Y`
>
>
>
> I get below output
>
> [lnindrakrishna@lvshdc2en0011 ~]$ sh PXP_EAP_Process.sh
>
> PXP_EAP_Process.sh: line 9: [: -eq: unary operator expected
> Directory does not Exist
> mkdir: cannot create directory /user/lnindrakrishna/03232013: File exists
>
>
>
> Looks like  hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY is returning
> NULL and that is the reason it throws null value for echo $TestDir  and it
> goes to else part and displays  "Directory does not Exist"
>
>
> What is wrong in the above shell script that I have written
>
>
> -



--
Harsh J

Re: Delete a hdfs directory if it already exists in a shell script.

Posted by Harsh J <ha...@cloudera.com>.
The -test utility of the "fs" shell does not return outputs, but is to
be used via exit codes. See
http://tldp.org/LDP/abs/html/exit-status.html for more reading
material on what an exit code implies.

Hence, you should be doing it this way:

hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
TestDir=$?
…

… for your script to work.

On Sat, Mar 23, 2013 at 11:12 PM, L N <nk...@gmail.com> wrote:
> Hi,
>
> I am trying to delete a hdfs directory if it already exists in a shell
> script.
>
> I wrote below command in a shell script
>
> DIRECTORY=`date +%m%d%Y`
> TestDir= hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
> echo $TestDir
> if [ $TestDir  -eq  0 ]; then
>                hadoop fs -rmr /user/lnindrakrishna/$DIRECTORY
>                echo "Directory Deleted"
> else
>                echo "Directory does not Exist"
>            fi
> exec hadoop fs  -mkdir /user/lnindrakrishna/`date +%m%d%Y`
>
>
>
> I get below output
>
> [lnindrakrishna@lvshdc2en0011 ~]$ sh PXP_EAP_Process.sh
>
> PXP_EAP_Process.sh: line 9: [: -eq: unary operator expected
> Directory does not Exist
> mkdir: cannot create directory /user/lnindrakrishna/03232013: File exists
>
>
>
> Looks like  hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY is returning
> NULL and that is the reason it throws null value for echo $TestDir  and it
> goes to else part and displays  "Directory does not Exist"
>
>
> What is wrong in the above shell script that I have written
>
>
> -



--
Harsh J

Re: Delete a hdfs directory if it already exists in a shell script.

Posted by Harsh J <ha...@cloudera.com>.
The -test utility of the "fs" shell does not return outputs, but is to
be used via exit codes. See
http://tldp.org/LDP/abs/html/exit-status.html for more reading
material on what an exit code implies.

Hence, you should be doing it this way:

hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
TestDir=$?
…

… for your script to work.

On Sat, Mar 23, 2013 at 11:12 PM, L N <nk...@gmail.com> wrote:
> Hi,
>
> I am trying to delete a hdfs directory if it already exists in a shell
> script.
>
> I wrote below command in a shell script
>
> DIRECTORY=`date +%m%d%Y`
> TestDir= hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY
> echo $TestDir
> if [ $TestDir  -eq  0 ]; then
>                hadoop fs -rmr /user/lnindrakrishna/$DIRECTORY
>                echo "Directory Deleted"
> else
>                echo "Directory does not Exist"
>            fi
> exec hadoop fs  -mkdir /user/lnindrakrishna/`date +%m%d%Y`
>
>
>
> I get below output
>
> [lnindrakrishna@lvshdc2en0011 ~]$ sh PXP_EAP_Process.sh
>
> PXP_EAP_Process.sh: line 9: [: -eq: unary operator expected
> Directory does not Exist
> mkdir: cannot create directory /user/lnindrakrishna/03232013: File exists
>
>
>
> Looks like  hadoop fs -test -d /user/lnindrakrishna/$DIRECTORY is returning
> NULL and that is the reason it throws null value for echo $TestDir  and it
> goes to else part and displays  "Directory does not Exist"
>
>
> What is wrong in the above shell script that I have written
>
>
> -



--
Harsh J