LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-02-2008, 01:34 PM   #1
duparcmeur
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Question 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
 
Old 04-02-2008, 02:22 PM   #2
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by prad77; 04-17-2008 at 04:36 AM.
 
Old 04-02-2008, 02:57 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 08:42 PM
create a directory in gawk duparcmeur Linux - Newbie 6 04-03-2008 03:01 PM
getting gawk to operate on a string in a bash script Lordandmaker Linux - Newbie 3 02-26-2007 01:01 PM
no such file or directory exists when trying to execute a exe ryedawg Linux - Software 5 12-05-2005 06:42 AM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-19-2005 12:22 AM


All times are GMT -5. The time now is 12:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration