0

As title,

Assume I have a file name : test.txt , also have another file in the directory (ex tet.txt , ttst.txt)

I want to write a script in order to not replace the content if file exist

Here is the script

#!/bin/bash

[[ -e /home/paul/*est.txt ]] || cat > "/home/paul/test.txt" << EOF
16
EOF 

but I don't know why *(wildcard) doesn't work, it always change the content as file exists.

I also try add '' and "" or $ with wildcard but doesn't work

How should I use the wildcard * ?

Thank you in advance for any help you can provide!!

Victor Lee
  • 2,467
  • 3
  • 19
  • 37
  • Show us how the script is started. Note that globs are expanded -- replaced with specific file names -- _before_ the program they're passed to runs. – Charles Duffy Oct 18 '21 at 11:58
  • Beyond that, `[[` intentionally suppresses glob expansion in most contexts. This is deliberate behavior -- if it didn't, as `[` doesn't, then you'd simply have syntax errors whenever the expansion came out to the wrong number of words. – Charles Duffy Oct 18 '21 at 12:00
  • You may find [BashFAQ #4](https://mywiki.wooledge.org/BashFAQ/004) informative. – Charles Duffy Oct 18 '21 at 12:01
  • Unclear. You want to not replace the content if it exists? And if it does not exist, what, create it? Avoid double negatives, it gets confusing fast! For your `*`, the `-e` test can only work with 1 argument, not a list of files. but that `*` can match anything. It would be best to use `find`. – Nic3500 Oct 20 '21 at 02:32
  • Does https://stackoverflow.com/questions/2937407/test-whether-a-glob-has-any-matches-in-bash answer your question? – KamilCuk Oct 21 '21 at 08:25

0 Answers0