LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2013, 01:32 PM   #1
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Rep: Reputation: Disabled
bash script to read multiple user input


Hello All,

I am trying to find all the files on patterns that are to be taken as a user input. The number of user input pattern is not fixed. it can vary from 1 to as many as i would like. after i have taken all the user input i have to find the files based on these pattrens and store the file names in an array.since the number of input is not fixed so i thought best way is to use while loop to seperate the pattrens stored in the read variable . here is my code
Code:
declare -a arr
echo " enter the pattern/patterns ->" # entering the patterns. it can be 1 or more then one
read pattern #say the patterns entered are- file123 file124 file125
while IFS='' read -d '' var ; do # if there are more then one pattren then seperating the pattrens by IFS='' in a while loop
arr=$(find "/home/dir1/"  -type f -name '$var*')#finding  all the files on each pattren and storing it in array
done < < ( $pattern )
but somewhere i am making mistake.can anyone please help
 
Old 03-31-2013, 02:09 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here are some things to look at:

1. Please explain what you have set IFS to and what the '-d' option is doing in your while / read statement?

2. Review how to create / assign to an array in bash

3. Review what is the difference between single and double quotes, especially when surrounding a variable (eg. '$var*')

4. Review how to read from a string

Answering the above may help you to understand where you have made several mistakes in the above.
 
  


Reply

Tags
ifs, read


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
Using read to prompt for editable user input in Bash 3 woodson2 Programming 4 11-28-2012 11:18 AM
Bash - Read user input: URLs without the enter key stroke make Programming 2 09-23-2010 04:29 PM
read multiple input with bash zulsolar Programming 10 05-09-2010 02:22 AM
read multiple lines from user input in c archanac07 Linux - Software 1 02-26-2010 05:15 AM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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