LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-06-2008, 09:40 AM   #1
duparcmeur
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Question create a directory in gawk


Hi,

I tried to create a directory in my gawk script using mkdir but it is not working. Does someone know how to do it?
Thank you so much.

Duparcmeur
 
Old 03-06-2008, 11:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

From OReilly's Sed&Awk book:
Quote:
The system() function executes a command supplied as an expression.[3] It does not, however, make the output of the command available within the program for processing. It returns the exit status of the command that was executed. The script waits for the command to finish before continuing execution. The following example executes the mkdir command:

[3] The system() function is modeled after the standard C library function of the same name.

BEGIN { if (system("mkdir dale") != 0)
print "Command Failed" }


The system() function is called from an if statement that tests for a non-zero exit status. Running the program twice produces one success and one failure:

$ awk -f system.awk
$ ls dale
$ awk -f system.awk
mkdir: dale: File exists
Command Failed

The first run creates the new directory and system() returns an exit status of 0 (success). The second time the command is executed, the directory already exists, so mkdir fails and produces an error message. The "Command Failed" message is produced by awk.
Hope this gets you going again.
 
Old 04-03-2008, 09:44 AM   #3
duparcmeur
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
I did

BEGIN { if (system("mkdir dale") != 0)
command

but I don't what to create the directory if it doesn't exist. is there a command to do that?
Thanks,

Duparcmeur
 
Old 04-03-2008, 09:45 AM   #4
duparcmeur
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
did

BEGIN { if (system("mkdir dale") != 0)
command

but I don't want to create the directory if it doesn't exist. is there a command to do that?
Thanks,

Duparcmeur
 
Old 04-03-2008, 10:29 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I probably don't understand what it is you want to do:

- You do _not_ want to create the dir if it does _not_ exist.

What is the point in creating it if it does exist?
 
Old 04-03-2008, 12:36 PM   #6
masinick
Member
 
Registered: Apr 2002
Location: Greenville, SC
Distribution: Debian, antiX, MX Linux
Posts: 636
Blog Entries: 16

Rep: Reputation: 104Reputation: 104
mkdir -p dir will only create dir if it does not exist; if dir exists this command will do nothing. Alternatively you can create shell logic with a statement containing something like if ![ -d $directory ]; then mkdir $directory; fi --- double check me on the location of that negation (!) - basically this reads "If the directory defined by the variable $directory does not exist, then run the command mkdir $directory.

If I messed up the syntax, double check me on that, but from an algorithm standpoint, this is the idea. You have a couple choices. The mkdir -p sidesteps the entire issue, as long as you are confident of the name of the directory you are creating, because with -p you will not get any feedback whether the command worked or not.

With the if logic, you could put in some diagnostic print statements to let you know if the directory got created or not, plus you could insert an ls statement to list your directories to see what you have - either the specific directory you are interested in or anything around it in the directory hierarchy.

Hope this is useful.
 
Old 04-03-2008, 02:01 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by duparcmeur View Post
but I don't want to create the directory if it doesn't exist. is there a command to do that?
I gave you an answer in your other thread about checking if a directory exists in gawk. You can try to elaborate (not just ignoring) and use a similar syntax to create a directory.
 
  


Reply



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
How do i create a directory ? vskgopu Programming 1 07-26-2006 03:25 AM
create a directory in c++ crapodino Programming 12 02-01-2006 08:23 PM
How to create new directory?? Jorine Programming 4 12-01-2004 12:38 AM
Can't create new directory!!?? catty Linux - Newbie 7 10-06-2003 05:33 PM
Create a directory SnowSurfAir Linux - Software 15 07-21-2003 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration