LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-19-2012, 09:10 AM   #1
tommya
LQ Newbie
 
Registered: Mar 2012
Distribution: opensuse
Posts: 2

Rep: Reputation: Disabled
Reading variables in loop in bash.


Hi,

Got a script that will eventually run something that takes some takes time, so I want it to check directories exist before it does some other stuff. The variables are input into the directory DIRS on the command line, but now I want to check they all exist, but unfortunately this doesn't work:

Code:
for i in "${DIRS[@]}" 
do
 echo "$i"
 while [ ! -d $i ]; 
  do 
    echo "*************************************************"
    echo "$i"
    echo "*************************************************"
    echo "This directory does not exist, please re-enter..."
    read $i
  done
done
and I get the error:

Code:
./directory-input.sh: line 22: read: `/home/tom/work': not a valid identifier
Any ideas how to make this work? I'm thinking I've got something pretty fundamental wrong with the way it reads the variable...
 
Old 03-19-2012, 11:47 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You need a conditional - not another while loop inside your for loop:

Code:
for i in "${DIRS[@]}" 
do
 echo "$i"
 if [ ! -d $i ]; 
 then 
    echo "*************************************************"
    echo "$i"
    echo "*************************************************"
    echo "This directory does not exist, please re-enter..."
    read $i
  fi  
done
 
Old 03-19-2012, 12:27 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The main problem is read takes a variable name, not a value:
Code:
read i #good
read $i # bad
@MensaWater: I think the OP wants to keep asking for directory until the user gets it right, thus the while loop.
 
1 members found this post helpful.
Old 03-19-2012, 12:50 PM   #4
tommya
LQ Newbie
 
Registered: Mar 2012
Distribution: opensuse
Posts: 2

Original Poster
Rep: Reputation: Disabled
Yes, the while loop is in there to keep asking. Removed the $, working fine now, thanks guys!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
tcl foreach loop not reading variables Stuart07 Programming 1 03-11-2011 02:45 PM
bash: variables inside while loop J_Szucs Programming 5 07-19-2009 08:24 AM
Assigning variables in a bash for loop JDska55 Linux - Newbie 6 06-18-2009 03:37 PM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 07:14 AM

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

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