Listing a directory via a script using variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing a directory via a script using variables
# 1  
Old 10-10-2008
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  
Old 10-10-2008
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  
Old 10-10-2008
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  
Old 10-10-2008
What does
Code:
ls -1 C:/cygwin/var/log/img/aut/ | grep TEST01_*\.mqsc

at the prompt show?
# 5  
Old 10-10-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Listing all local variables for unset

I have tried to thoroughly search other threads before posting this question... I have a shell script (bsh) that I'd like to "re-execute" if the user chooses to. Before the program is executed again the local variables (those set within the script) need to be unset. Is there a command that... (6 Replies)
Discussion started by: powwm
6 Replies

2. Homework & Coursework Questions

Listing the files in a directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A script that takes any number of directories as command line arguments and then lists the contents of each of... (3 Replies)
Discussion started by: Phaneendra G
3 Replies

3. Shell Programming and Scripting

Directory listing

Hi, I have a directory with a bunch of files say around 150K. I want the directory's path and the filenames printed to a text file. Example: If I am in the directory /path/test and the files in this directory are My output file should be like this Thanks in advance ----------... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Script for listing specific filename in 3 directory

I used to work in perl but I would like to try now bash. Here's my problem I wanted to search for specific filename (ls -ltrh) on 3 directories.. Example ls -ltrh | grep "string" /dir1 /dir1/dir2 /dir1/dir2/dir3 I wanted to get the result to be copied on a different directory. Sample... (2 Replies)
Discussion started by: lhareigh890
2 Replies

5. Shell Programming and Scripting

how to create and access a directory in the same script using variables

I have a working script that defines the paths using variables which is used to move a rename files that are being archived. Is there a way to create a directory in the path with the date as the name and then reference it when moving the file to it? Here is what I have tried with no luck.... ... (1 Reply)
Discussion started by: Snickyt0310
1 Replies

6. Shell Programming and Scripting

Directory Listing Help

i have searched through this site and have found some useful information but i'm struggling with one thing. In my script i am created a start and end file so I can get a listing of the files within those two files. However I want to exclude any sub-directories in this listing. Below are the... (8 Replies)
Discussion started by: J-DUB
8 Replies

7. Shell Programming and Scripting

AWK script for directory listing using GREP

Hi, All - script1.awk contains the following script: BEGIN { printf "The following are directory files:" "ls -l | grep '^d' | {print $0}" printf "There were "NR" directories in this list." } When I run the script, Here is what I get: $ awk -f... (4 Replies)
Discussion started by: ora_umair
4 Replies

8. Shell Programming and Scripting

Shell Script Passing Parameters For Directory Listing

i have this basic piece of code that i am trying to debug to accept input parameter to be able to display a directory listing of files. cd /u02/app/eatv/dev/out CURDIR=`pwd` echo directory listing of $CURDIR echo if ; then ls -latr else ls -latr $1 fi basically if the script... (9 Replies)
Discussion started by: wtolentino
9 Replies

9. UNIX for Dummies Questions & Answers

How can i get directory listing?

Hai friends is there any command in unix that display only directories... (I have 5 directories in my home directory, and i also have some files along with directories...But when i tried to show the directory listing using the command ls -d i wasn't presented by the directory listing...Please... (2 Replies)
Discussion started by: haisubbu
2 Replies

10. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies
Login or Register to Ask a Question