0

i have a bash code, and i need list the files of a path and save in a file

processVerification(){
    ls $1 > testFiles
}

and in the folder i have the files

transaction-2012-01-20.csv
transaction-2012-01-21.csv
transaction-2012-01-22.csv
transaction-2012-01-23.csv
transaction-2012-01-24.csv
transaction-2013-01-24.csv
transaction-2013-02-24.csv

when i call processVerification folder=/home/folder/transactions-2012* command="cat" processVerification $folder $command

$ cat testFiles

I have Only transaction-2012-01-20.csv and i need all files.

But i need only the transactions of 2012 and i create processVerification because i need pass 50 distinct folders and the command is because in some folders i have .csv and in others i have .csv.bz2 and in others i have .csv.gz

and the files in each folder are distinct

camilo soto
  • 1,211
  • 2
  • 9
  • 10
  • 1
    THe `*` expansion happens _before_ it comes to your function call. You could do a `processVerification "/home/folder/transactions-2012*"` preserving the `*` untill later. – Wrikken May 24 '13 at 19:46
  • 1
    I've voted to close this question because the asker keeps changing it. @Benito, please try to formulate your question before asking. – msw May 24 '13 at 19:58

2 Answers2

4

You edited your post, and now the error makes sense. When you call processVerification like this:

processVerification /home/folder/transactions-2012*

That passes all of the transactions-2012* files to processVerification(), but in that function you only look at the first argument. Try this instead to look at all args:

processVerification(){
    ls "$@" > testFiles
}
Markku K.
  • 3,840
  • 19
  • 20
  • 2
    For whitespace-safety, use `"$@"` – glenn jackman May 24 '13 at 19:35
  • 1
    @glennjackman, good catch. I have updated my answer. I typically use zsh, which seems to handle whitespace in $@ just fine without the quotes. – Markku K. May 24 '13 at 19:41
  • @BenitoCamelas, I don't understand your comment. – Markku K. May 24 '13 at 19:42
  • i call processVerification $folder $command and say me ls: cat: No such file or directory – camilo soto May 24 '13 at 19:47
  • because i pass 2 parameters the folder and the command to read – camilo soto May 24 '13 at 19:48
  • @MarkkuK. Benito means that as he is giving the command (`cat`, which isn't shown in the function but _is_ shown in the function call), will be an argument to `ls`. – Wrikken May 24 '13 at 19:49
  • You keep updating your question, it makes it difficult to give a good answer. Where is the "cat" argument supposed to be used in processVerification()? – Markku K. May 24 '13 at 19:51
  • 1
    Trying to read between the lines, maybe you want this: `command=cat processVerification /home/folder/transactions-2012*`. Then if you want to run "cat" on testFiles, add this line to processVerification(): `$command testFiles` – Markku K. May 24 '13 at 19:56
  • Yes i need use cat or zcat or bzcat depend of the folder, because in some folder i have compress the files and in others not – camilo soto May 24 '13 at 19:58
  • Note that if you need _all argument but the last_, there's a [question here that deals with that](http://stackoverflow.com/questions/1215538/bash-extract-parameters-before-last-parameter-in), but preventing `*` expansion untill later seems equally workable. – Wrikken May 24 '13 at 19:58
  • If you remove "command=cat" from the argument list, then it won't be a problem. Putting "command=cat" in front of the function call puts that variable "command" into the environment of that function call. – Markku K. May 24 '13 at 20:05
0

You can use ls -1 and pipe it to grep.

processVerification() {
    ls -1 | grep 'transaction-2012' > testFiles
}

Your problem is that you are calling processVerification with the wrong argument. You should be doing:

processVerification /home/folder/

Not:

processVerification /home/folder/transactions-2012*

ktm5124
  • 11,861
  • 21
  • 74
  • 119