0

I have this bash code:

  if [[ ! -f "$HOME/.oresoftware/package.json" ]]; then
       cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json";
    fi

but is there a way to only redirect to a file if it doesn't exist in just one line of code? something like this:

   cat "node_modules/@oresoftware/package.json" ?> "$HOME/.oresoftware/package.json";

that would be fun I guess

4

If you set the noclobber option, bash will refuse to overwrite an existing file:

$ set -C    # or set -o noclobber
$ touch foo
$ cat bar > foo
bash: foo: cannot overwrite existing file

So:

set -C
cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json"

If you want to ignore the exit status, you can add a ! before the cat command

set -C; ! cat "node_modules/@oresoftware/package.json" > "$HOME/.oresoftware/package.json"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.