Wildcards in file input to a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcards in file input to a script?
# 1  
Old 11-09-2012
Wildcards in file input to a script?

I have four files:

test
test2
test3
test4

I have this simple script:

Code:
#!/bin/bash
ls $1

Why does ./the_script.sh test* only list the first file, when a normal ls test* would list all four? What do I need to change in the script to be able to use wildcard?
# 2  
Old 11-09-2012
Because test* is expanded to test, test1, test2, test3, etc., where $1 is test. $@, not $1, would list all the files beginning with "test".
# 3  
Old 11-09-2012
The * character needs to be escaped:

Code:
./the_script.sh test\*
test  test2  test3  test4

# 4  
Old 11-09-2012
Quote:
Originally Posted by Scott
Because test* is expanded to test, test1, test2, test3, etc., where $1 is test. $@, not $1, would list all the files beginning with "test".
I assume I could get that into a for loop...?
# 5  
Old 11-09-2012
And best to put $@ in double quotes:
Code:
ls "$@"

# 6  
Old 11-09-2012
Quote:
Originally Posted by KidCactus
I assume I could get that into a for loop...?
Yes, you could do that.

Code:
$ cat myScript
for F in "$@"; do
  echo $F
done

$ ls filename*
filename1	filename2	filename3

$ ./myScript filename*
filename1
filename2
filename3

# 7  
Old 11-09-2012
What if would do kind of the same thing but with grep, where I would use multiple arguments, and still wanted to use wildcard for the filenames?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

2. Shell Programming and Scripting

SH Script for file name wildcards

Does anyone know how I would go about inserting text at the beginning of a file with the file name containing a daily time stamp? Essentially I need to find the file name using a wild card, and then insert 3 lines of text - one of which is the processing date. Help please!? (1 Reply)
Discussion started by: cookie33
1 Replies

3. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

4. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

5. Shell Programming and Scripting

Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux: if (-r *.) then echo " las file found" else echo " no las file found" endif If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get... (9 Replies)
Discussion started by: phudgens
9 Replies

6. UNIX for Dummies Questions & Answers

Running script on SFTP server, RMDIR and RM with wildcards

Im going insane trying to figure out what i consider a basic command on an SFTP server... Im trying to download all files from a directory *done* then remove all the files (and sometimes folders that contain files) i have downloaded on the remote directory... the command i would normally... (2 Replies)
Discussion started by: mokachoka
2 Replies

7. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

8. Shell Programming and Scripting

How to chk the file name using wildcards??

Hi, I need to find a file with name xyz.mno.1234235.msg Here the numbers can be anything and is not fixed. But the first 6 characters are fixed ie, xyz and mno are fixed and wont change. Actually this file comes to this directory every week with only the number part changing. How is it... (7 Replies)
Discussion started by: RRVARMA
7 Replies

9. Shell Programming and Scripting

Use wildcards in a script

Hello I have this script: #!/bin/ksh INPUTFILE=$1 TEMPFILE=$INPUTFILE.$$ OUTPUTFILE=$INPUTFILE.new # nr of arguments has to be 1 if then echo "\nUsage: $0 inputfile\n" return 1 fi # inputfile must exist and be readable if then (13 Replies)
Discussion started by: emferrari
13 Replies

10. UNIX for Dummies Questions & Answers

script for deletion using wildcards

find ./ -name t\* | sed "s@^./@@" > .filestobedeleted j=$(wc -l < .filestobedeleted) typeset -i cnt=0 typeset -i i=0 while read line do myarray=$line ((cnt = cnt + 1)) done < .filestobedeleted while (1 Reply)
Discussion started by: aishu
1 Replies
Login or Register to Ask a Question