The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read is not pausing to read davegerdt Shell Programming and Scripting 3 04-07-2008 07:20 AM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 12:02 PM
Cannot read in variable using read on first try normie UNIX for Advanced & Expert Users 1 10-04-2005 10:42 PM
read line and read next ariuscy UNIX for Dummies Questions & Answers 7 09-21-2005 04:04 AM
read() Deepa IP Networking 1 06-07-2002 05:50 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-28-2006
Banned
 

Join Date: Oct 2006
Posts: 10
about READ...please help!

hi guys i have a question:

i am creating an interactive programme and i need some help on the READ command. let's say that i use the echo command and ask something to my user in the form:

echo 'Please, insert filenames: \c'

then i read his/her answer with:

read answer

i need to test the answer given to see, for example, if the user has really chosen files and not directories for example so i use:

if [[ -d $answer ]]; then
echo 'Attention: you have selected a directory!'
fi

now my problem is this: if the user, instead of just one file, writes 2 or more files?
i know i can write:

read answer1 answer2 etc.

but then it becomes too difficult to check all variables in the IF STATEMENT...and also...what if the user uses more variables than those i have specified? too complicated...

any suggestion on how to include all the possible filenames the user could specify in just one variable to check? (a bit like $@ instead of "$1" "$2" etc.)

hope my question is clear...

thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-28-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Try this:
Code:
#!/bin/ksh
echo 'Please, insert filenames: \c'
read filenames
filelist=""
for file in $filenames; do
        if [ -d $file ]; then
                echo "Oops, $file is a directory! Leaving that out of the final list"
        elif [ -f $file ]; then
                echo "Adding $file to the list"
                filelist="$filelist $file"
        else
                echo "$file is something I can't handle"
        fi
done
echo "Final list: $filelist"
-Edit
Oops, I missed a space there...
-/Edit

Last edited by blowtorch; 12-28-2006 at 05:40 PM. Reason: missed a space
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0