The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting Directory Listing Sepia UNIX for Dummies Questions & Answers 1 07-11-2007 07:44 AM
How can i get directory listing? haisubbu UNIX for Dummies Questions & Answers 2 08-25-2006 09:03 AM
Full Directory Listing... B14speedfreak UNIX for Dummies Questions & Answers 5 05-11-2006 08:06 AM
Timestamp in directory listing vijashok UNIX for Dummies Questions & Answers 2 10-06-2005 10:03 AM
Recursive directory listing without listing files psingh UNIX for Dummies Questions & Answers 4 05-10-2002 10:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
Listing a directory via a script using variables

In my script I need to list the directory, where the generic name of the files will change, in my test case its set to TEST_*.mqsc. I wrote a small test script as below, but it just does not pip the listing to a file.
Any idea why?

dir='C:/cygwin/var/log/img/aut/'
file=TEST01_*.mqsc
ls $dir | grep "$file" > test.txt


When I run it in debug I get the following:

$ bash -x test.sh
+ dir=C:/cygwin/var/log/img/aut/
+ file='TEST01_*.mqsc'
+ ls C:/cygwin/var/log/img/aut/
+ grep TEST01_080924.mqsc TEST01_080925.mqsc TEST01_080926.mqsc TEST01_080927.mq
sc TEST01_080928.mqsc TEST01_080929.mqsc TEST01_080930.mqsc TEST01_081001.mqsc T
EST01_081002.mqsc TEST01_081003.mqsc TEST01_081004.mqsc TEST01_081005.mqsc TEST0
1_081006.mqsc TEST01_081007.mqsc TEST01_081008.mqsc TEST01_081009.mqsc TEST01_08
1010.mqsc
+ rc=1
+ echo 'Return Code is: 1'
Return Code is: 1
  #2 (permalink)  
Old 10-10-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,285
I guess it can't find anything since you can't write a regular expression for grep like you use metacharacters in the shell. Try this:
Code:
file="TEST01_.*\.mqsc"
Your former code meant _* which stands for zero or <n> occurences of _. And just a dot means any single character, not a dot. So the dot has to be escaped.

Oh and maybe use "ls -1" to have 1 file per row.
  #3 (permalink)  
Old 10-10-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
I still get the same result

I executed the following:

file="TEST01_*\.mqsc"
ls -1 $dir | grep $file > test.txt
rc=$?
echo "Return Code is: $rc"

The result was:

$ bash -x test.sh
+ delete_files='TEST01_*.mqsc'
+ file='TEST01_*\.mqsc'
+ ls -1 C:/cygwin/var/log/img/aut/
+ grep TEST01_080924.mqsc TEST01_080925.mqsc TEST01_080926.mqsc TEST01_080927.mq
sc TEST01_080928.mqsc TEST01_080929.mqsc TEST01_080930.mqsc TEST01_081001.mqsc T
EST01_081002.mqsc TEST01_081003.mqsc TEST01_081004.mqsc TEST01_081005.mqsc TEST0
1_081006.mqsc TEST01_081007.mqsc TEST01_081008.mqsc TEST01_081009.mqsc TEST01_08
1010.mqsc
+ rc=1
+ echo 'Return Code is: 1'
Return Code is: 1
  #4 (permalink)  
Old 10-10-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,285
What does
Code:
ls -1 C:/cygwin/var/log/img/aut/ | grep TEST01_*\.mqsc
at the prompt show?
  #5 (permalink)  
Old 10-10-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
I was incorrect

ls -1 C:/cygwin/var/log/img/aut/ | grep TEST01_*\.mqsc produced no output.

Anyway, instead of file="TEST01_*.mqsc", file="TEST01" should suffice as I am only interested in files that start with "TEST", * is for the date and .mqsc is consistent for all files.


However, I was making a silly error in the script. All I had to do was:

file="TEST01"
echo "File = $file"
ls -1 $dir | grep "$file" > test.txt

Thanks for helping me
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0