easy UNIX for loop not working...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers easy UNIX for loop not working...
# 1  
Old 07-15-2011
easy UNIX for loop not working...

So, I am trying to find and print info about all of the .txt files in a current directory. This is the information that I intend to gather:

-File number (say there are 6 .txt files in the directory...it would be incremented 1-6)
-File name
-File directory
-Number of lines

Pretty straightforward, right? But for some reason, it isn't working.

This is what I have:

Code:
for file in *.txt  #goes through all .txt files
do
  count=1;
  echo 'File #: $count';
  count=$((count + 1));   #increments count
  echo 'File name: ' {print FILENAME}; #supposed to print filename?
  echo 'File Directory:' pwd; #will pwd be appended here?
  echo 'Line count:' wc - l; #will wc -l be appended here?
done
 
I know there is at least one thing wrong with this. I am not sure if {print FILENAME} is the correct way to print the current file I am looking at, and I don't think that appended pwd or wc -l after an echo statement will cause it to print like I want it to...
 
This is my first shell script ever! Can you guys help me out?

---------- Post updated at 02:14 PM ---------- Previous update was at 02:14 PM ----------

whoops...messed up the code tags. sorry!
# 2  
Old 07-15-2011
I don't want to mess all the fun and post the answer.

through each time through the loop, $file will be set to the current file.
you're resetting count INSIDE the loop! So it won't really count at all.
To print results of a command like you are, you need `cmd` (with backticks) or $(( cmd )) syntax..

Let us know if you want more info right away though.

Edit: oh, and the echo 'count: $count' doesn't expand inside single-quotes. you need double quotes. echo "count: $count"
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 07-16-2011
Wow man...I butchered this thing! But your guidance has gotten the whole thing working for me! Thank you! I really love when people don't just post the solution, because it got me thinking and now I understand it!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

for loop not working

Hi, I have ten different files to extract. so I thought for loop will help me. but it's not working for me. Please advice what I am doing wrong. for INCI in 1 2 3 4 5 6 7 8 9 10 do tar -xvf Update_INCI11.10.002_lnx86_1of10.tar done second file name is... (3 Replies)
Discussion started by: samnyc
3 Replies

2. Shell Programming and Scripting

For loop not working

Hi All, For loop in ksh not working if it was given in the following method. simple script: for i in {1..4} do echo $i done Output: {1..4} Even below also not working :( Script: for (( c=1; c<=5; c++ )) do echo "Welcome $c times..." done Output: ./x.sh: 0403-057 Syntax error... (13 Replies)
Discussion started by: girish_satyam
13 Replies

3. Shell Programming and Scripting

Which unix is more easy with Oracle ?

Hi, currently our databasese are on AIX, but we plan to purchase new sever of unix except IBM.so colud you please tell en which unix flavor is more compatible with oracle sun solaris or HP unix and we also need list of SAN configuration. thanks a lot in advacne. (2 Replies)
Discussion started by: younusdba
2 Replies

4. Shell Programming and Scripting

Easy unix/sed question that I could have done 10 years ago!

Hi all and greetings from Ireland! I have not used much unix or awk/sed in years and have forgotten a lot. Easy enough query tho. I am cleansing/fixing 10,000 postal addresses using global replacements. I have 2 pipe delimited files , one is basically a spell checker for geographical... (4 Replies)
Discussion started by: dewsbury
4 Replies

5. UNIX for Dummies Questions & Answers

If then else loop not working

If then else segment of below code is not working. For each filename code is displaying output for if part as well as else part. Please help its urgent. for usercusttop in `echo ${filename}|sort|uniq|cut -c 1-${actualwordcount}` do ... (2 Replies)
Discussion started by: findprakash
2 Replies

6. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

7. UNIX for Dummies Questions & Answers

easy unix question

I am trying to check through all of a certain type of file in all main directories, and find the top 10 that are taking up the most space. How can I do that? I was thinking like du *.file | sort -n | head (1 Reply)
Discussion started by: wallacer
1 Replies

8. UNIX for Dummies Questions & Answers

Easy UNIX notation question

can anyone tell me what exactly the following UNIX notation code does cause I need to do the same in windows? for x in webapps/sal/*.htm* do mv $x $x.bak sed 's@bob@sal@g' $x.bak > $x done Thanks (1 Reply)
Discussion started by: lavaghman
1 Replies

9. Shell Programming and Scripting

Loop not working

Apologize if this is doesn't come under this group. I have a small script to find out users who last logged in to check there mail. (Tru 64 4.0, Netscape mail 3.6) -----> cat $1|awk -F: '$2=="SMTP-Accept" && $5~/@maildomain/ {s=$5;u++;las=substr($1)} END {for (i in u) {print... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question