Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.