LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find if a directory exists in a gawk script (https://www.linuxquestions.org/questions/linux-newbie-8/find-if-a-directory-exists-in-a-gawk-script-632500/)

duparcmeur 04-02-2008 12:34 PM

Find if a directory exists in a gawk script
 
Hi,

I try to find out if a directory exists before executing a cp. It tried with if (-d $dirname) but it is not working. Can you help me?
Thank you.

Duparcmeur

prad77 04-02-2008 01:22 PM

if [ -d $i ];
then
echo " folder exists"
else
echo " folder does not exits "
fi

or
if it escapes out you can try it with,

find . -type d -print

Gentoo

colucix 04-02-2008 01:57 PM

In gawk you can do something like this, using "getline into a variable from a coprocess"
Code:

BEGIN { DIR = "dirname"

  ( "ls -d " DIR " 2>/dev/null" ) |& getline dummy

  if ( dummy != "" )
 
    print "directory exist"
   
  else
 
    print "directory not found"

}

You get the input from a coprocess (a shell command) and put the result into a variable. In the above example the output of "ls -d dirname 2>/dev/null" is put into the variable dummy. Then you check if the variable is not an empty string and act accordingly.


All times are GMT -5. The time now is 02:57 PM.