LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-07-2005, 08:49 AM   #1
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Rep: Reputation: 30
Simple bash problem - checking for files and displaying the first line


Hi, I'm having a little problem with bash. I have gone through a couple of tutorials online for bash, but the only real language I've ever really used before is basic.

This looks nearly to right to me (famous last words!) but it isn't working.

This is a test of what I am trying to do - I am trying to list the contents of a directory of all text files with a certain extension (in this case *.txt). What I want to do is test if the files exist, and if they do, display the first line of each file.

in $HOME/lyrics are 2 files called 1.txt and 2.txt, and the first line of each contains a name of a song as a test.

Code:
#!/bin/bash


if [ ls $HOME/lyrics/*txt 1>/dev/null 2>&1 ]; then
	echo "Lyrics files found:"
	echo " "
	for i in $HOME/lyrics/*txt; do
		head -n 1 $i
	done
else
	echo "no text files found."
fi




All I ever get as a result is:

Code:
[me@mymachine me]$ /bin/bash lyrics.sh
No text files found.
Can anyone please tell me what I am doing wrong with the first part of the "if" for it not to give me any results?
 
Old 03-07-2005, 11:07 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
I would use find and a test variable like

Code:
#!/bin/bash

# test variable
found=
for file in $(find $HOME/lyrics -name "*.txt"); do
    [ -z $found ] && found=1
    head -n 1 $file
done

[ -z $found ] && echo "no text files found."
Also
Code:
if ls $HOME/lyrics/*txt 1>/dev/null 2>&1; then
will works too because the ls test is not supported with [], only the
files test or variables test work with [ ]

Last edited by keefaz; 03-07-2005 at 11:12 AM.
 
Old 03-07-2005, 11:14 AM   #3
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Original Poster
Rep: Reputation: 30
Thanks for the tip, but like I said, i'm learning bash from scratch and this script is based on one I found on http://linuxcommand.org, but I just can't seem to get it to work.

It was taken from a larger script, but there were no variables set for it, the only difference is this IF was inside a function on it's own, but as I don't need anything else from the script, I don't need to define it as a function?


Thanks,
Morrolan
 
Old 03-07-2005, 11:16 AM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
The [ ] is a test for files, variables or result of test expression, ls is not a test expression
the [] is not testing the ls return value but instead if it is a positive test expression.

see man test ([] is an alias for test) and I edited my post, see how if ls can work
 
Old 03-07-2005, 11:40 AM   #5
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Original Poster
Rep: Reputation: 30
Doh!

It was the [] test expression which was doing it - that's what I get for trying to be clever!

Thanks very much!!!


Morrolan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 02:42 PM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 06:45 PM
simple bash piping problem nadroj Linux - General 3 09-10-2005 04:12 PM
Apache problem with displaying compression/archive files. Akujin Linux - Networking 4 03-09-2005 03:18 AM
simple bash problem xushi Programming 2 04-22-2004 08:02 AM


All times are GMT -5. The time now is 10:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration