declare, assign variables using array, counter, loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting declare, assign variables using array, counter, loop
# 1  
Old 08-06-2004
declare, assign variables using array, counter, loop

using bash Tru64...

converting DCL to shell...

any tips to make this work would be greatly appreciated.

Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call the newly created variables in functions, hence the export.

When I run the script I receive the following error...

./newfile1.shl: param1=1: command not found
./newfile1.shl: param2=2: command not found
./newfile1.shl: param3=3: command not found
./newfile1.shl: param4=4: command not found
./newfile1.shl: param5=5: command not found


#!/bin/bash
LIMIT=6
PARAM="$1 $2 $3 $4 $5"
COUNTER=1

while [ $COUNTER -lt "$LIMIT" ];
do
for n in ${PARAM[@]}
do
param$COUNTER=`echo $n`
export param$COUNTER

let COUNTER=COUNTER+1

done
done

echo $param1
echo $param2
echo $param3
echo $param4
echo $param5
# 2  
Old 08-06-2004
This code looks very strange and I'm not able to imagine what result you're attempting to achieve.

But to set up an array in bash it's:
PARAM=($1 $2 $3 $4 $5 $6)

I *think* that you need ti increment the counter between the two done statements.

It is possible that you want:
eval param$COUNTER=$n
but I'm not real sure.

Also an instance of bash will be running this script. That instance of bash will be a process. If you run export commands in the script, you will affect the environment of the process that is running your script. This environment will be inherited by any child processes you launch. But when the script ends, so process will exit. This environment is gone. You will probably go back to an interactive shell with its old unchanged environment.
# 3  
Old 08-09-2004
The arguments kind of come into the shell script as an array -
$@ is all of them, $# is the number of arguments. Some of the folks here still read DCL, you may want to post the parts of your code you're having problems with.

To get the arguments to show up in a loop you could try something like this:
Code:
echo "args = $@"
let i=0
for arg in `echo $@`
do
   echo "Argument $i = $arg "
   let i=i+1
done

# 4  
Old 08-09-2004
Seems that the bash way to build an array to contain the script's parameters is simply...
Code:
declare -a param="$@"

See the Advanced Bash-Scripting Guide: Chapter 26. Arrays

Last edited by Ygor; 08-09-2004 at 12:06 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (3 Replies)
Discussion started by: spflanze
3 Replies

2. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

3. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

4. Shell Programming and Scripting

For loop; how to construct a array with variables

Hi everybody!! Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have: ./mycommand.sh aa bb cc dd I need to take an array like this: myarray=(aa bb cc dd) So I use a simple for loop like this: for... (4 Replies)
Discussion started by: andresgom
4 Replies

5. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

6. Shell Programming and Scripting

How to declare variables once and reuse them in other scripts?

Hello everyone. I'm trying to create a conf file with variables that my other scripts will use. I have several scripts that use the same variables, and since I don't know how to read them from an external file, i define them in each script (and then if i want to change one's value i need to... (4 Replies)
Discussion started by: moshe88
4 Replies

7. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

8. Shell Programming and Scripting

BASH : Using declare in a loop

I have a config file that needs to be formatted before it's sourced to a script. I've been formatting it with sed and dropping it in /tmp then sourcing it from there. I'd like to just format the config file with sed then declare the variables without having to create a temporary file in /tmp. So... (2 Replies)
Discussion started by: crs6785
2 Replies

9. Shell Programming and Scripting

How to declare an array to take more than 10,000 characters

Hi Guys Need some help I am reading the string values from the text files into the shell script and had them feed into array I have declared an associative array as TYPE t_user_id_tab IS TABLE OF VARCHAR2(3000);\n my_user_id t_user_id_tab;\n varchar2 is limiting me to take only... (0 Replies)
Discussion started by: pinky
0 Replies

10. UNIX for Dummies Questions & Answers

How to declare global variables for shell script

Hi, I do have some variables accessed in more than one script. I want to have those variables in a command file anduse them. Something like a header file that we use in C programs. I dont want to set them as environment variables. Is there any other option, like header file in scripting ?? ... (2 Replies)
Discussion started by: risshanth
2 Replies
Login or Register to Ask a Question