how to use while


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to use while
# 1  
Old 09-13-2002
how to use while

I was trying to use while loop
and try to read it from text file and was not able to do it.

db.txt while read db
do
if [ "$1" = "dev"] then
script
else
echo"message"
fi
done

i am getting the error message.
# 2  
Old 09-13-2002
I believe that you need something after "while".

while true
read etc...


or


while x=3
read etc....


db.txt while true read db
do
if [ "$1" = "dev"] then
script
else
echo"message"
fi
done


Smilie
# 3  
Old 09-13-2002
they syntax for a while loop is as followes for the ksh shell.

Code:
while [ <condition> ]; do
<some stuff....>
<some cool stuff....>
<some awsome cool stuff....>
done

# 4  
Old 09-13-2002
Quote:
Originally posted by Optimus_P
they syntax for a while loop is as followes for the ksh shell.

Code:
while [ <condition> ]; do
<some stuff....>
<some cool stuff....>
<some awsome cool stuff....>
done


Gotta love the awesome cool stuff Smilie
# 5  
Old 09-13-2002
What error are you getting?
Just by looking at what you posted, you might try:
Code:
cat db.txt | while read db something_else; do
 if [ $db = "expected" ]; then
    echo "$something_else"
 else
    echo "Nope"
 fi
done

This won't work with all shells, though. Most likely, it will work with ksh and zsh, but not sh or bash. Also, you can remove the "cat" at the beginning, and redirect the file into done:
Code:
 
[...]
done < db.txt

# 6  
Old 09-16-2002
MySQL

Thanks cat worked like charm.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question