How to keep unix from evaluating a file pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to keep unix from evaluating a file pattern
# 1  
Old 09-25-2012
How to keep unix from evaluating a file pattern

How do I keep a file pattern from evaluating?

I want to pass in a file pattern to a script as a parameter, and then use that pattern in subsequent statements, but I can't get the pattern to not evaluate before I use it.

For example, if I have 3 files called file1.txt, file2.txt and file3.txt and in my script I have something like this:

num=`ll $1 | wc -l`

and I pass file*.txt in as my parameter, num always = 1. Because $1 evaluates to "file1.dat file2.dat file3.dat", all one line.

So essentially my statement says

num=`ll file1.dat file2.dat file3.dat | wc -l`

How do I get it to ask:

num=`ll file*.dat | wc -l`

so that my answer will correctly be 3?

Thanks.
# 2  
Old 09-25-2012
When you invoke your script use:
Code:
script 'file*.txt'

instead of using:
Code:
script file*.txt

Alternatively, in your script change:
Code:
num=`ll $1 | wc -l`

Code:
num=`ll "$@" | wc -l`

If you want all of the arguments passed to your script to be counted. But, I assume you have other parameters you're passing to your script or you would just use:
Code:
num=$#

which is a MUCH faster way to give you the same results.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-25-2012
It's also safer than trying to expand * inside your script, too.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

3. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

4. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

5. Shell Programming and Scripting

Unix file pattern check and replacement

HI Guys , Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp If the file exist in correct format come out from code . If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or... (8 Replies)
Discussion started by: Perlbaby
8 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

8. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

9. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies
Login or Register to Ask a Question