LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   create a directory in gawk (https://www.linuxquestions.org/questions/linux-newbie-8/create-a-directory-in-gawk-626163/)

duparcmeur 03-06-2008 09:40 AM

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

druuna 03-06-2008 11:22 AM

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.

duparcmeur 04-03-2008 09:44 AM

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

duparcmeur 04-03-2008 09:45 AM

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

druuna 04-03-2008 10:29 AM

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?

masinick 04-03-2008 12:36 PM

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.

colucix 04-03-2008 02:01 PM

Quote:

Originally Posted by duparcmeur (Post 3109505)
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.


All times are GMT -5. The time now is 02:55 AM.