LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2009, 07:48 AM   #1
r00tb00t
LQ Newbie
 
Registered: Jul 2008
Distribution: CentOS, Ubuntu
Posts: 12

Rep: Reputation: 1
Question Simple Shell Script is b0rked!


Hey Users,


This simple shell scripts looks through all the sub folders in the given directory and in each sub folder is 2 text files, it counts how many lines are in each file and how many directories where accessed but it reports back all zeros (0)?

Code:
#!/bin/sh

f=0
d=0
u=0
t=0
x=0

find /path/to/my/folder -maxdepth 1 -type d | while read FOLDER; do
        f=`expr $f + 1`
        if [ -f $FOLDER/domains ]; then
                x=`wc -l $FOLDER/textfile1 | awk '{print $1}'`
                d=`expr $d + $x`
        fi
        if [ -f $FOLDER/urls ]; then
                x=`wc -l $FOLDER/textfile2 | awk '{print $1}'`
                u=`expr $u + $x`
        fi
done

t=`expr $d + $u`

echo "Number of folders: $f"
echo "Number of lines in textfile1: $d"
echo "Number of lines in textfile2: $u"
echo "Total number of lines for both text files: $t"
echo "$x"
When i run this test script it get the following output:
Code:
[hades@hades ~]$ sh tester
Number of folders: 0
Number of lines in textfile1: 0
Number of lines in textfile2: 0
Total number of lines in both text files: 0
0
[hades@hades ~]$
I am confused, it lookes ok to me but I'm not very good at these things?
 
Old 05-14-2009, 08:03 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The pipe after the find command executes the loop into a subshell. After the subshell terminates all the variables local to the subshell itself are lost. You have to execute the while loop without calling a new subshell. One way is:
Code:
while read FOLDER
do
  <your commands here>
done < <(find /path/to/my/folder -maxdepth 1 -type d)
where the syntax <(command) means "process substitution".
 
Old 05-14-2009, 07:24 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Alternatively use a for loop
Code:
for file in `find /path/to/my/folder -maxdepth 1 -type d`
do
    blah
done
which I find easier to read
 
Old 05-14-2009, 07:34 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
Alternatively use a for loop
Code:
for file in `find /path/to/my/folder -maxdepth 1 -type d`
do
    blah
done
which I find easier to read
set IFS for directories with spaces....or use while read loop
 
  


Reply



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
need shell-script help (very simple) oskar Linux - General 6 03-12-2008 07:08 AM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Simple Shell Script Help Kristijan Programming 3 06-13-2005 09:13 PM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 10:14 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration