files=$(pwd | ls -m )
Um, what? Trying to pipe the output of pwd
? ls doesn't read from stdin, so this is pointless. Oh, and since the -m
option adds a comma to each entry, any subsequent command that tries to read the name will probably fail to find it. Perhaps you want ls "$PWD"
But in any case, this isn't a good way to go about it. first of all, Parsing ls
for filenames is generally a bad idea. Try using simple globs
instead. Second, when working on lists. you should be storing them in arrays
rather than scalar variables.
Since arrays are indexed by number, it also becomes much easier to count and track them without the need for external tools like wc
simply takes the undefined variable "file
" and adds "s
" to it, so you get the output "s
". I'm assuming this is a typo?
But in addition, always
quote your variable expansions, unless you want word-splitting to occur. It's vital in scripting to understand exactly how the shell handles arguments and whitespace:
Although as written, your script actually depends
on the variable "files" being word-split so that cp can grab the individual filenames. The first name with a space in it is going to mess it up. Again, use an array instead.
Finally, learn the value of clear formatting
and commenting. Lining things up in neat blocks would help you notice and avoid simple syntax problems like missing terminators.
Edit: One more.
until [ $counter=numberfiles ] do
Check out bash pitfalls 4, 7, and 10 to understand what's wrong here.