LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-30-2009, 01:48 AM   #16
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

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" )'
Unbalanced if-fi and do-done. Easier to find problem if use indentation. Here is untested correction
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"
        fi
    done <<< $( find . -type f -name "$name" )
elif [ no -gt 1 ] ; then
    for j in {1..$no}
    do
       echo " enter file names "
       read name
       fn="-o -name $name"
       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 )
    done
fi
#fi
But it would be easier for the user and the code if you simply looped until the user entered an empty line.
Code:
while true
do
    echo " enter file name (or no name to finish)"
    read name
    [[ "$name" == '' ]] && \exit 0
    while read filepath
    do
        if [[ "$filepath" == '' ]]; then
            echo "$name not found"
        else
            echo "$filepath found"
        fi
    done <<< $( find . -type f -name "$name" )
done
 
Old 12-01-2009, 04:22 AM   #17
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Hi Catkin,

Is this possible to read more than one file at a time .. instead of reading one file at a time in this code ..

Code:
while true
do
    echo " enter file name (or no name to finish)"
    read name
    [[ "$name" == '' ]] && \exit 0
    while read filepath
    do
        if [[ "$filepath" == '' ]]; then
            echo "$name not found"
        else
            echo "$filepath found"
        fi
    done <<< $( find . -type f -name "$name" )
done
Regards

Bala

Quote:
Originally Posted by catkin View Post
Unbalanced if-fi and do-done. Easier to find problem if use indentation. Here is untested correction
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"
        fi
    done <<< $( find . -type f -name "$name" )
elif [ no -gt 1 ] ; then
    for j in {1..$no}
    do
       echo " enter file names "
       read name
       fn="-o -name $name"
       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 )
    done
fi
#fi
But it would be easier for the user and the code if you simply looped until the user entered an empty line.
Code:
while true
do
    echo " enter file name (or no name to finish)"
    read name
    [[ "$name" == '' ]] && \exit 0
    while read filepath
    do
        if [[ "$filepath" == '' ]]; then
            echo "$name not found"
        else
            echo "$filepath found"
        fi
    done <<< $( find . -type f -name "$name" )
done

Last edited by balakrishnay; 12-01-2009 at 04:23 AM.
 
Old 12-01-2009, 10:16 AM   #18
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
Don't understand what you want. The code reads a file name each time, not a file.
 
Old 12-01-2009, 09:33 PM   #19
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
I am sorry catkin,

Yes the same code should read more than one file name at one time ..

Regards


Quote:
Originally Posted by catkin View Post
Don't understand what you want. The code reads a file name each time, not a file.
 
Old 12-02-2009, 01:02 AM   #20
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 am sorry catkin,

Yes the same code should read more than one file name at one time ..

Regards
OK. Where from? From command line, user input or find command?
 
Old 12-02-2009, 02:20 AM   #21
balakrishnay
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
From the user input and the same has to be passed to find command dynamically .

I will explain to you in detail.

Initially i will ask the user how many files does he need to find . for Ex:- user enters 2 then we should allow him to enter 2 file names .This shell script should find these 2 files along with the path.

Like this .. the user might enter any no of files to find .....

I have tried manually to find multiple files using find command it worked but i have problem taking file names dynamically.

Manually we can find like this ..

find /apps12i -type f -name filename1 -o -name filename2

Thank you !!



Quote:
Originally Posted by catkin View Post
OK. Where from? From command line, user input or find command?
 
  


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 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 10:56 PM.

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