LinuxQuestions.org
Visit Jeremy's Blog.
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 08-22-2010, 08:03 AM   #1
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Mandriva
Posts: 76

Rep: Reputation: 23
>> not working as expected


I have the following code
Code:
 
#!/bin/sh
z=0;
for f in `find $1 -wholename *.txt`
do
    let z+=1;
    echo "Found: $z "`dirname $f`
    echo "Found: $z "`dirname $f` >> log
done
This works fine.

Now I want to add some actions and a failsafe for it.
Code:
 
#!/bin/sh
z=0;
for f in `find $1 -wholename *.txt`
do
    let z+=1;
    echo "Found: $z "`dirname $f`
    echo "Found: $z "`dirname $f` >> log
  #  ... do something
    if [ $? -eq 0 ]; then   
      cd `dirname $f`
    else
       echo "Failed "`dirname $f`
    fi
done
As soon as I add the if clause the logfile only contain the first matching entry . I don't even need to add any real actions to it.
I still get all outputs to the screen through the first "echo"... it is as if the second one (with the >>) is only triggered once even though the code obviously pass by it multiple times (since i get the echo to screen).

I am sure it is a simple mistake, but what am I doing wrong?
 
Old 08-22-2010, 08:28 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 97
I don't see anything wrong with what you are doing. One option
you can try is to run the script with the -x switch to see each
command as it is being run. For example:
Code:
sh -x myscript > myscript.log 2>&1
You can also try replacing the two echo statements with one.
Code:
echo "Found: $z "`dirname $f` | tee -a log
 
Old 08-22-2010, 08:30 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Once you change the current directory, then next >> log goes to a file in the new directory.
 
1 members found this post helpful.
Old 08-22-2010, 08:33 AM   #4
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 97
Nice catch, johnsfine. I would have noticed that after another cup of coffee. ;-)
 
Old 08-22-2010, 09:43 AM   #5
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Mandriva
Posts: 76

Original Poster
Rep: Reputation: 23
That is just so darn obvious now that you mention it. Thank you!
 
Old 08-22-2010, 10:35 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
In bash you can open an fd for that:
Code:
#!/bin/bash
z=0;
for f in `find $1 -wholename *.txt`
do
    let z+=1;
    echo "Found: $z "`dirname $f`
    echo "Found: $z "`dirname $f` >&3
  #  ... do something
    if [ $? -eq 0 ]; then   
      cd `dirname $f`
    else
       echo "Failed "`dirname $f`
    fi
done 3>>log
 
  


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
ifconfig not working as expected... sheykh Linux - Newbie 13 08-18-2010 11:34 AM
find not working as expected luusac Linux - Newbie 8 08-28-2008 11:07 AM
ln command not working as expected walterbyrd Linux - Software 3 01-04-2008 01:51 PM
Crontab not working as expected nro Linux - Newbie 7 08-29-2005 12:56 PM
chmod u+s not working as expected clinton Linux - Newbie 3 03-19-2004 07:49 PM


All times are GMT -5. The time now is 08:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration