Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
It's easier to read 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).
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
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.
It's easier to read 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).
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
Code:
echo " enter file "
read name
find /apps12i -type f -name "$name" | while read -r FILE
do
echo $FILE
done
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!
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"
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
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"
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
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
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" )
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).
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).
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" )'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.