LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-27-2009, 12:02 AM   #1
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Rep: Reputation: 15
Shell scripts issue


Hi all,

My scripts looks like this .. when i execute this script its says file does not exist even though file is present physically and if the file does not present in the file system then the output is null.

I am wrong somewhere .. ??

echo " enter file "
read name
for i in `find /apps12i -type f -name $name -printf %f `
do
if [ -e $i ]
then
echo "file exists"
else
echo "not exists "
fi
done

2. I want to send multiple attachments using mutt is possible ?

3. I want to change from mail address when i am sending mails through mutt.


Regards
 
Old 11-27-2009, 12:05 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use a while read loop instead
Code:
find ... | while read file
do
  echo $file
done
 
Old 11-27-2009, 12:29 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You will get a better response to questions 2 and 3 if you put them in separate threads with relevant titles.

Regards the existing file(s) that is/are reported as not existing you can see which files are being tested by changing the code to
Code:
echo " enter file "
read name
for i in `find /apps12i -type f -name $name -printf %f `
do
    echo "DEBUG: file name is '$i'"
    if [ -e $i ]
    then
        echo "file exists"
    else
        echo "not exists "
    fi
done
File names including whitespace characters such as space will not be handled correctly by the script.

Regards null output for non-existing files, that is expected. The find command will not produce any output so the loop code will not be executed.

BTW:
 
Old 11-27-2009, 12:52 AM   #4
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for your reply .. but i am not able to understand proplery

I have rewritten like this .. but still the same problem exists . can you please correct me.

I request you to please elaborate this while IFS= read -r -d '' this stmt.


echo " enter file "
read name
i=`find /apps12i -type f -name $name -print0` | while IFS= read -r -d '' $i
do
echo $i
done



Quote:
Originally Posted by catkin View Post
You will get a better response to questions 2 and 3 if you put them in separate threads with relevant titles.

Regards the existing file(s) that is/are reported as not existing you can see which files are being tested by changing the code to
Code:
echo " enter file "
read name
for i in `find /apps12i -type f -name $name -printf %f `
do
    echo "DEBUG: file name is '$i'"
    if [ -e $i ]
    then
        echo "file exists"
    else
        echo "not exists "
    fi
done
File names including whitespace characters such as space will not be handled correctly by the script.

Regards null output for non-existing files, that is expected. The find command will not produce any output so the loop code will not be executed.

BTW:
 
Old 11-27-2009, 01:08 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo " enter file "
read name
find /apps12i -type f -name "$name"  | while read -r FILE
do
    echo $FILE
done
 
Old 11-27-2009, 02:03 AM   #6
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for your reply ..... what if file does not exist .. my requirement is , if the file does not exist it should give me files does not exist message.

Regards



Quote:
Originally Posted by ghostdog74 View Post
Code:
echo " enter file "
read name
find /apps12i -type f -name "$name"  | while read -r FILE
do
    echo $FILE
done
 
Old 11-27-2009, 02:14 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You changed
Code:
find /apps12i -type f -print0 | while IFS= read -r -d '' i
to
Code:
i=`find /apps12i -type f -name $name -print0` | while IFS= read -r -d '' $i
The change will prevent it working.

while IFS= read -r -d '' means "set IFS to the empty string and with that setting in use for this command only, run the "read" shell built-in command not treating any \ characters specially and only accepting the newline character as end-of-line indicator. The shell uses the characters in IFS as word-separators when parsing input into "words". The net effect of all this gobbledygook is to transfer exactly the file names that "find" prints, regardless of any weird characters in file names.

It is very defensive and robust. Most of the time ghostdog74's suggestion works perfectly well -- and is a lot simpler!
 
Old 11-27-2009, 02:18 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by balakrishnay View Post
Thanks for your reply ..... what if file does not exist .. my requirement is , if the file does not exist it should give me files does not exist message.

Regards
Code:
echo " enter file "
read name
found='NO'
find /apps12i -type f -name "$name"  | while read -r FILE
do
    echo $FILE
    found='YES'
done

[[ "$found" == 'NO' ]] && echo "File '$name' not found"
 
Old 11-27-2009, 02:33 AM   #9
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
i have executed this but .. the output is wrong ..

even though file exists .. its giving me file not found. i am really sorry please bare with me ..

testinst1.koel.co.in/apps12i]./check.sh
enter file
bala.txt
File 'bala.txt' not found
testinst1.koel.co.in/apps12i]./check.sh
enter file
APXVDRAL.rdf
/apps12i/oracle/KIRAN/apps/apps_st/appl/ap/12.0.0/reports/US/APXVDRAL.rdf
File 'APXVDRAL.rdf' not found

Regards



Quote:
Originally Posted by catkin View Post
Code:
echo " enter file "
read name
found='NO'
find /apps12i -type f -name "$name"  | while read -r FILE
do
    echo $FILE
    found='YES'
done

[[ "$found" == 'NO' ]] && echo "File '$name' not found"
 
Old 11-27-2009, 06:52 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by balakrishnay View Post
i have executed this but .. the output is wrong ..

even though file exists .. its giving me file not found. i am really sorry please bare with me ..

testinst1.koel.co.in/apps12i]./check.sh
enter file
bala.txt
File 'bala.txt' not found
testinst1.koel.co.in/apps12i]./check.sh
enter file
APXVDRAL.rdf
/apps12i/oracle/KIRAN/apps/apps_st/appl/ap/12.0.0/reports/US/APXVDRAL.rdf
File 'APXVDRAL.rdf' not found

Regards
Better we "bear" with you -- "bare" is something different!

Sorry my stupid mistake -- the "while" loop is run in a subshell so any variables set in it are lost when it exits.

This one is tested
Code:
echo " enter file "
read name
while read FILE
do
    if [[ "$FILE" == '' ]]; then
        echo "$name not found"
    else
        echo "$FILE found"
    fi
done <<< $( find . -type f -name "$name" )

Last edited by catkin; 11-27-2009 at 06:53 AM. Reason: Removed duplicated text
 
1 members found this post helpful.
Old 11-27-2009, 10:00 PM   #11
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Thank you !! very much. Finally it worked.

But i didn't understand the logic behind this .. can you please explain this.

echo " enter file "
read name
while read FILE
do
if [[ "$FILE" == '' ]]; then
echo "$name not found"
else
echo "$FILE found"
fi
done <<< $( find . -type f -name "$name" )

Regards


Quote:
Originally Posted by catkin View Post
Better we "bear" with you -- "bare" is something different!

Sorry my stupid mistake -- the "while" loop is run in a subshell so any variables set in it are lost when it exits.

This one is tested
Code:
echo " enter file "
read name
while read FILE
do
    if [[ "$FILE" == '' ]]; then
        echo "$name not found"
    else
        echo "$FILE found"
    fi
done <<< $( find . -type f -name "$name" )
 
Old 11-27-2009, 10:51 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by balakrishnay View Post
can you please explain this.
The input to the read comes, non-intuitively, at the end of the do-done from a here string (scroll down to section 3.6.7) with the string itself being the output of $( find ...).

If the file does not exist, there is no output from the find command so the string is empty, hence the if [[ "$FILE" == '' ]].

It's easier to read your code if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 11-27-2009, 11:12 PM   #13
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks .. nice explanation .

Regards



Quote:
Originally Posted by catkin View Post
The input to the read comes, non-intuitively, at the end of the do-done from a here string (scroll down to section 3.6.7) with the string itself being the output of $( find ...).

If the file does not exist, there is no output from the find command so the string is empty, hence the if [[ "$FILE" == '' ]].

It's easier to read your code if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 11-29-2009, 10:29 PM   #14
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Hi Catkin,

My requirements seems to be changed again . I will explain you what i am trying to achieve.

I want to find multiple files in different directories with one find command and send this files to developers. This is my requirement and changed the code accordingly but it seems to be not working .

Code:
echo " enter how many files "
read no
echo " enter file "
read name
if [ $no = 1 ] ; then
while read FILE
do
    if [[ "$FILE" == '' ]]; then
        echo "$name not found"
    else
        echo "$FILE found"
done <<< $( find . -type f -name "$name" )
else
if [ no -gt 1 ] ; then
for j in {1..$no}
do
echo " enter file names "
read name
fn="-o -name $name"
done
while read FILE
do
    if [[ "$FILE" == '' ]]; then
        echo "$name not found"
    exit 1
    else
        echo "$FILE found"
    fi
done <<< $( find . -type f -name "$name" $fn )
fi
fi
Tried executing this code but it giving me error ..

testinst1.koel.co.in/apps12i]./check.sh
enter how many files
1
enter file
bala.txt
./check.sh: line 12: syntax error near unexpected token `done'
./check.sh: line 12: `done <<< $( find . -type f -name "$name" )'

Regards

Bala


Quote:
Originally Posted by balakrishnay View Post
Thanks .. nice explanation .

Regards
 
Old 11-30-2009, 12:14 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
while
do
.
.
. 
done < file
takes only one '<'
http://tldp.org/LDP/abs/html/redircb.html
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
need shell scripts ARsenthil Linux - General 3 05-20-2007 12:55 PM
shell scripts in RT nipunos Linux - Software 0 05-09-2007 02:06 AM
Shell scripts...then some. Freestone Programming 3 04-23-2006 12:13 PM
Shell Scripts benwy Linux - Software 1 06-09-2003 02:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:50 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