Reputation: 422
I have made a directory with lots of files with:
samplefile_111222015_reporting_{1..13}
I am trying to create a vi script where when I enter the directory as an argument to the command e.g.
sh myScript $HOME/theDir/*
then it copies all the files in that directory to a new one I made. Although right now, I'm having problems with the for loop alone. This is what I have in my script:
for f in $1;
do
echo "$f"
done
but when i enter sh myScript $HOME/theDir, I get back the name of the first file only (samplefile_111222015_reporting_1). why the first file? Is this not a for loop>
Upvotes: 0
Views: 395
Reputation:
This is because each file is passed as a separate argument and you're only looping over $1
, which is the first argument.
Instead, you most likely want to loop over "$@"
, which is every argument starting from $1
.
The man page for bash, under the Special Parameters
section, details the special parameters available in more detail.
Upvotes: 0
Reputation: 641
# Because of the wild card expansion, all the files in the directory are
# already made available to the script through arguments
# So do the following to get all the file listing
for f ; do echo $f; done
Upvotes: 2