-1

Hi Can any one help me to clear problem to assign the mkdir command output to Variable.

While execute the script I am able to create the directory with following command

#!/bin/bash
echo -n " Which Name needs to create? (y/n): "; read dom       


     if [ "$dom" == "y" ]; then
    echo -n " Type an Domain name: " ; read domTemp

path=/home/rakesh/$domTemp

a=`mkdir -p -- "$path"`

echo "$a"

fi

By reading documents and stack issue, I know how to assign linux command to variable in shell script but the mkdir command not give expected result by printing $a and my expectation is to use the variable $a is to set the path in next command in the scrip.

/home/rakesh/foo.com

My intention is to collect the exact path of created directory to a variable. Though I can use the path to assign permission in further steps

Below solution is to make directory from variable only Assign output of mkdir command to variable

Appreciate your knowledge on this.

  • 4
    `not give expected result` What do you expect? I would expect `a` to be empty. – KamilCuk Aug 26 '22 at 12:41
  • 4
    `mkdir` never writes to `stdout`. So the value of `a` will always be null string after a=\`mkdir -p -- "$path"\` – M. Nejat Aydin Aug 26 '22 at 12:51
  • 2
    What's the difference from the [question linked by you](https://stackoverflow.com/questions/43254508/assign-output-of-mkdir-command-to-variable)? To me that looks like an _exact_ duplicate? (with pretty much the same answer) – knittl Aug 26 '22 at 12:57
  • 1
    @RakeshVijayan, why do you think it's expected for mkdir to print anything on stdout? What part of its documentation says it _should_? – Charles Duffy Aug 26 '22 at 17:06
  • @knittl can you run the above scrip from your end and see what is the difference – Rakesh Vijayan Aug 26 '22 at 17:12
  • @RakeshVijayan, nobody but you has a `/home/rakesh`, so nobody but you can run your script exactly as-given. – Charles Duffy Aug 26 '22 at 17:13
  • @CharlesDuffy accepted your suggestion, but please edit the above script with your any of folder and then run n see what its give you. Now I understood mkdir can't stdout after reading you referral link. If this post deleted from SOF person like me waist time for search the possibilities in internet. – Rakesh Vijayan Aug 26 '22 at 17:40
  • 2
    If `path=/home/rakesh/foo.com`, why do you need a `$a` at all? I still don't understand the point of this question in the first place. I don't know what running the code in the question is supposed to show me; just because I can see what the output _actually is_ doesn't mean I know _what you want_. – Charles Duffy Aug 26 '22 at 18:31

1 Answers1

3

The solution is:

mkdir_output=$(mkdir -v -p "$path")

NB: the -v option does the difference.

Edit: as stated by Charles Duffy in the below comment, you could emulate the -v option by:

mkdir_output=$(mkdir -p "$path" && echo "Successfully creted folder \"$path\".")
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • 2
    Note that `-v` is not guaranteed by POSIX to be present: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html -- thus, tools that need to be portable across different UNIXlike operating systems should not rely on it. – Charles Duffy Aug 26 '22 at 17:07
  • As you suggested I updated my code but it print like **mkdir: created directory '/var/rakesh/foo.com'** expect some valuable information like you suggest – Rakesh Vijayan Aug 26 '22 at 17:10
  • @RakeshVijayan, what other information do you think is supposed to exist? – Charles Duffy Aug 26 '22 at 17:12
  • @CharlesDuffy I expect like other commands mkdir can also assign to a variable. let me read your suggested post – Rakesh Vijayan Aug 26 '22 at 17:28
  • 1
    @RakeshVijayan, `variable=$(somecommand)` writes the output of `somecommand` to `variable`. But if that command _has no output_, it's completely normal for the variable to be empty. An expectation that every command will have output is frankly wrong; that's not how UNIX works: the normal convention is that when a command (which doesn't have a specific job of producing output) works correctly it's silent; and if it has only informational or status output meant for humans, that goes to stderr not stdout so it isn't captured by command substitutions or pipelines. – Charles Duffy Aug 26 '22 at 17:37
  • @CharlesDuffy I got solution by reading your post https://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-a-bash-script/1371283#1371283 Works as expected :) – Rakesh Vijayan Aug 26 '22 at 17:58
  • @RakeshVijayan, that solution answers a very different question than the one you appeared to be asking here, so I'm confused about the relationship between the two. – Charles Duffy Aug 26 '22 at 18:28
  • @CharlesDuffy by reading that answer i got solution by move that maked directory and pwd to take the output to variable. So I got the exact location **a=$(mkdir -v -p "$path") cd $path d=$(pwd) echo $d** my intention is to get maked directory path to a variable – Rakesh Vijayan Aug 27 '22 at 03:52
  • 1
    @RakeshVijayan You already have this directory in the variable `path`. Do you suspect `mkdir` will create a directory different from the `$path`? No, it won't! – M. Nejat Aydin Aug 27 '22 at 07:50
  • @RakeshVijayan, so what you want is the _absolute path_ of the directory, with symlinks replaced with the names they point to? You don't need output from mkdir from that; `path=$(readlink -m "$path")` on systems with GNU readlink. You can do that successfully even before the `mkdir` has happened, at a point in time when the directory doesn't yet exist. – Charles Duffy Aug 27 '22 at 16:48
  • 1
    @RakeshVijayan, ...by the way, the fact that we had to do all this back-and-forth question-and-answer to figure out what you were really asking for in your question (assuming that my current understanding _is_ in fact correct) is part of why I don't think this question would be helpful to others if left in the knowledge base in its current state; if it's not obvious to existing readers what the question is trying to determine how to accomplish, why would it be obvious to future ones? – Charles Duffy Aug 27 '22 at 16:52