The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accessing Shell Variables in awk or sed nasersh Shell Programming and Scripting 3 05-05-2008 04:44 AM
shell variables and sed substitution duderonomy UNIX for Advanced & Expert Users 2 05-04-2008 11:54 PM
Using shell variables In awk nortypig Shell Programming and Scripting 11 08-23-2006 09:48 PM
Shell Script Variables ragha81 Shell Programming and Scripting 3 08-22-2006 12:43 AM
bash shell variables xNYx UNIX for Dummies Questions & Answers 3 04-15-2002 01:36 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-03-2008
Registered User
 

Join Date: Feb 2008
Posts: 13
Stumble this Post!
variables in shell

hi, i'm new in shell scripting and i'm working on bash on solaris 5.9

after try many stuff with unexpected results, i wonder:
it is not posible in bash, to use a variable that was created inside a loop, out of it?

i mean, for instance:
cat mytext | \
while read text
do
viko[$i]=$text
i=$i+1
done

echo ${viko[1]}
echo $i

those variables, seem to be empty


isn't posible to use any of those (array elements, and $i) outside the loop, before the done statement?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-03-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 625
Stumble this Post!
Wink Move the input file to < at end of loop

Loops are done as sub-processes. Maybe someone else can give all the details, but if you change from your "cat file" then "do" logic to simply "do" and "done <file" you should be fine.


Quote:
> cat sample.txt
line 1 apple
line 2 banana
line 3 carrot
Code:
> cat set_val
#! /bin/bash
# array usage
# do no cat the file to the read, rather do a 
#   while .... done<file

i_ct=0
while read text 
   do
   viko[$i_ct]=$text
   i_ct=$(( i_ct + 1 ))
done <sample.txt

echo ${#viko[*]}
echo ${viko[*]}
echo $i_ct

echo ${viko[1]}
Reply With Quote
  #3 (permalink)  
Old 03-03-2008
Registered User
 

Join Date: Feb 2008
Posts: 13
Stumble this Post!
thanks joeyg
that's great

I was trying not to use an external file, but i think this way will work great for me, at least for a while
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:57 PM.


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

Content Relevant URLs by vBSEO 3.2.0