-2

Here i am creating a directory, if the dir already exists, it should skip the step, i tried one but its failing. can any one help in that?

build_major=$(cat build.major)
build_minor=$(cat build.minor)
build_servicePack=$(cat build.servicePack)
build_patch=$(cat build.patch)
build_hotfix=$(cat build.hotfix)
build_number=$(cat build.number)
if[ -d "${build_major}.${build_minor}.${build_servicePack}.${build_patch}.${build_hotfix}_${build_number}" ];
then
    cp file.txt /${build_major}.${build_minor}.${build_servicePack}.${build_patch}.${build_hotfix}_${build_number}
else
    mkdir ${build_major}.${build_minor}.${build_servicePack}.${build_patch}.${build_hotfix}_${build_number}
    cp file.txt /${build_major}.${build_minor}.${build_servicepack}.${build_patch}.${build_hotfix}_${build_number}
fi

But is failing command not found errors

Naik
  • 37
  • 9
  • https://stackoverflow.com/questions/793858/how-to-mkdir-only-if-a-dir-does-not-already-exist – tosdanielle Jun 14 '19 at 09:48
  • 3
    you need to add space between `if` and `[` – ymonad Jun 14 '19 at 09:48
  • The script has problems and does not execute. Please fix it. Also see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to use Shellcheck](http://github.com/koalaman/shellcheck). – jww Jun 14 '19 at 09:49
  • 7
    Just do a `mkdir -p` and forget about the `if ... else ... fi` – JGK Jun 14 '19 at 10:04
  • Thanks guys for you valuable help. i just used mkdir -p option and its working. – Naik Jun 14 '19 at 10:14
  • 2
    By the way, every one of those `cat` calls is an unnecessary external child process. Instead of `build_major=$(cat build.major)` you can use `read build_major < build.major` which only uses shell builtins. – cdarke Jun 14 '19 at 10:19
  • Quote your variables, see https://mywiki.wooledge.org/Quotes, and create/use a variable like `path="/${build_major}.${build_minor}.${build_servicePack}.${build_patch}.${build_hotfix}_${build_number}"` rather than duplicating that whole string in multiple places in your code - that's just asking for a typo (as you already have - the mkdir is using a different path from the rest of your code due to no leading `/`). – Ed Morton Jun 14 '19 at 12:04
  • @cdarke: Your `read` command only reads the first line of a file so it's not equivalent, strictly speaking. – Dennis Williamson Jun 14 '19 at 20:56
  • @DennisWilliamson: that's correct, but I'll wager there is only one line in these files. – cdarke Jun 14 '19 at 21:32
  • @cdarke and `read` will also collapse and trim spaces, process and remove backslashes and return a non-zero status if the file is not terminated by newline. Using (plain) `read` is rarely a good idea. –  Jun 15 '19 at 05:39
  • Instead of `cp` you cound use `install(1)`: `install -Dt /path/to/dest/dir file.txt` –  Jun 15 '19 at 05:39

1 Answers1

0

Assuming relative paths for now:

if [ ! -d "./mydirname" ]
then
  mkdir ./mydirname
fi
Feakster
  • 558
  • 4
  • 14