The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compound variable in korn shell ZINGARO Shell Programming and Scripting 5 10-03-2007 10:57 AM
compound variable in korn shell ZINGARO Shell Programming and Scripting 0 08-30-2007 11:30 AM
awk compound statements gfhgfnhhn Shell Programming and Scripting 7 07-17-2006 01:15 PM
Compound indirect variable references tkrussel UNIX for Advanced & Expert Users 5 08-21-2005 10:03 AM
Trying to use 'compound variable' in a script irina Shell Programming and Scripting 1 01-29-2004 10:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-16-2004
neemic neemic is offline
Registered User
  
 

Join Date: Jul 2001
Posts: 2
Trying to use 'compound variable' in a script

Hi there - am newish to shell scripting and would appreciate some advice on this...

Am trying to use what I have seen called 'compound variables' in other langs but with no success in my shell script. This is the kind of thing I'm trying to do:

base_val=123
stop=3
x=1
while [ $x -le $stop ]
do
array_var[$x] = `expr $base_val + $x`
echo $x $array_var[$x]
x=`expr $x + 1`
done

The value of x is being incremented, but it is not being substituted as an index in the array variable called array_var. Here's the output:

./t2.ksh[8]: array_var[1]: not found
1 [1]
./t2.ksh[8]: array_var[2]: not found
2 [2]
./t2.ksh[8]: array_var[3]: not found
3 [3]

Is this completely the wrong approach, or is it an issue with the syntax?. What's the best way to achieve this?
Thanks in advance,

Neemic........:
  #2 (permalink)  
Old 01-16-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Syntax Problems

You need to remove the spaces either side of the equals sign during the array assignment. Also, you have to use curly brackets {} when referencing the array elements.
Code:
base_val=123
stop=3
x=1
while [ $x -le $stop ]
do
  array_var[$x]=`expr $base_val + $x`
  echo $x ${array_var[$x]}
  x=`expr $x + 1`
done
  #3 (permalink)  
Old 01-16-2004
tine tine is offline
Registered User
  
 

Join Date: Apr 2003
Location: Amsterdam - the Netherlands
Posts: 40
Quote:
array_var[$x] = `expr $base_val + $x`
I think you should leave the spaces out in this line before and after the = .
  #4 (permalink)  
Old 01-16-2004
neemic neemic is offline
Registered User
  
 

Join Date: Jul 2001
Posts: 2
Ygor, Tine,

Thanks very much for the help - and thanks for not laughing!

:-)

Cheers,
neemic
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0