0

I have multiple files starting with the same name within a folder, but there are also other files. Let's say they start with 'plot'. I want to echo the names in such a template

"plot-abc";"plot-dcb";"plot-asd";...

There is no order in the rest of the names. I tried it with

for file in /home/user/*;
do
  echo '"'
  echo ${file##*/}
  echo '";'
done

but this is putting the quotation marks at the very beginning and the end. And can't eliminate the unrelated files.

I'd appreciate if we can find a solution.

Thanks in advance.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
mrq
  • 274
  • 1
  • 2
  • 14
  • As an aside, [the POSIX spec for `echo`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) itself advises using `printf` instead in developing new code; see the APPLICATION USAGE and RATIONALE sections. – Charles Duffy Sep 11 '18 at 20:40

1 Answers1

2

printf lets you provide a template, which is repeated as many times as necessary to process all arguments:

#!/usr/bin/env bash
#              ^^^^- important: not /bin/sh; bash is needed for array support

shopt -s nullglob                 ## if no files match the glob, return an empty list
files=( /home/user/plot-* )       ## store results in an array

# if that array is non-empty, then pass its contents as a list of arguments to printf
(( ${#files[@]} )) && { printf '"%s";' "${files[@]##*/}"; printf '\n'; }
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thank you for the help. This didn't turn anything. Sorry for my lack of knowledge. – mrq Sep 11 '18 at 20:46
  • See `/home/user/plot-*` in the pattern. – Charles Duffy Sep 11 '18 at 20:46
  • sorry I just saw it. But it didn't return anything anyway. – mrq Sep 11 '18 at 20:47
  • The obvious answer is that the glob doesn't match anything. You'll need to adjust it to match files you actually have. – Charles Duffy Sep 11 '18 at 20:48
  • ...I can make the code a [mcve] that actually creates files before matching them, if you need such a demonstration that it would work if deployed correctly. – Charles Duffy Sep 11 '18 at 20:49
  • Ok, it worked now. For the code example I gave you, `/home/user/*` was working. But for yours, I have to change it to the name of the folder. Thank you for the help. – mrq Sep 11 '18 at 20:50
  • 1
    `/home/user/*` only looked like it worked because your quoting was wrong, and was re-expanding the glob *after* stripping the directory name... so when given an invalid directory name, it expanded the glob in the current directory instead. – Charles Duffy Sep 11 '18 at 20:52
  • 1
    See [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo) about side effects of using `echo $foo` instead of `echo "$foo"` – Charles Duffy Sep 11 '18 at 20:55
  • 1 more question related to this: I tried to declare a string variable such as `sampleType = 'temp'` and tried to implement this as `files=( /home/user/plot-*${sampleType}* ) ` and then `printf 'the file is'+${sampleType}` .Can you tell me what is wrong with this? It says: `sampleType: command not found` `the file is+` – mrq Sep 13 '18 at 16:35
  • 1
    Doing that correctly would look more like: `sampleType='temp'; files=( /home/user/plot-*"$sampleType"* ); printf 'The file is: %s\n' "${files[@]}"`. Note that you can't have spaces around the `=` in an assignment in shell; that expansions should always be quoted; and that `printf` data and format strings should be separate from each other. – Charles Duffy Sep 13 '18 at 16:35
  • 1
    BTW, you could have found existing knowledgebase issues about all those problems, if you searched -- see for example https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment – Charles Duffy Sep 13 '18 at 16:37
  • That's great. Your are very helpful. Thank you! – mrq Sep 13 '18 at 16:40
  • You are right. I couldn't see the issue and was close to be blaming `#!/usr/bin/env bash`. That's why I asked here, just in case. – mrq Sep 13 '18 at 16:42