How to set a variable with a count variable i.e. VARIABLE$COUNT


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to set a variable with a count variable i.e. VARIABLE$COUNT
# 1  
Old 08-28-2010
How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All

I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one.

I have a while count loop that checks checks two consecutive values then increments the count by two.

What the script has to do is then check all the 'pairs' of values match, if they do the check is true and the script can continue.

so what I thought was.....

f=1
while [ "$f" -le "$totalofchecks" ]; do

blah.....
blah.....
OK test is true so lets save this pass result.

1STCHECK$f="$CHECKVALUE"

done

the script is borking at the 1STCHECK$f line with a "command not found" error.

I've tried to enclose the 1STCHECK$f part in all the usual ' " ( ` options hoping it was a syntax problem, however it appears that I cant set a variable that has a variable as part of the name...is that right?

Could someone perhaps suggest an alternate way of doing this?

Thanks in advance.

---------- Post updated at 07:21 AM ---------- Previous update was at 07:19 AM ----------

Sorry I should add there is a f=`expr $f + 2` before the done

---------- Post updated at 07:25 AM ---------- Previous update was at 07:21 AM ----------

I think I've just realised that I can't start a variable with a number.......Dohhhhhhh
# 2  
Old 08-28-2010
I think you want to use arrays:

Arrays
# 3  
Old 08-28-2010
Quote:
Originally Posted by jim mcnamara
I think you want to use arrays:

Arrays
I'd already read that section but you can't put a variable in the array field either....

i.e.

$1STCHECK[$f] doesn't work
# 4  
Old 08-28-2010
Quote:
Originally Posted by Bashingaway
I'd already read that section but you can't put a variable in the array field either....
Yes you can.

Code:
$ X=blah
$ Y[$X]=more_blah
$ echo ${Y[$X]}
more_blah

Although you are correct about variables starting with numbers.
# 5  
Old 09-09-2010
OK I'm now back o this question.

I originally sorted it as suggested using arrays and that works fine.

However I now want to post a similar script to an embedded device that uses busybox and doesn't support arrays.

So back to my original question could someone suggest a way to structure something like

Code:
while [ "$COUNT" -le "10" ]; do
  COUNT=1
  $VARIABLE$COUNT="Some stuff read here"
  $OUTPUT$COUNT=`sed -e '/keep this line/!d' -e 's/now delete these bits//g'`
  COUNT=`expr $COUNT + 1`
done

Thanks in advance

---------- Post updated at 12:25 PM ---------- Previous update was at 12:24 PM ----------

Ignore the deliberate error, the COUNT=1 should be before the while....do

Last edited by Scott; 09-09-2010 at 10:12 AM.. Reason: Please use code tags
# 6  
Old 09-09-2010
I'm not sure but probably this is something what you're looking for:
Code:
#!/bin/ksh

num1="one"
num2="two"

count=1
while [ $count -le 2 ]
do
  eval "val${count}=\$num${count}"
  count=$(( $count + 1 ))
done

echo $val1	# output : one
echo $val2	# output : two

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

Getting file count in a variable

Hi All I am checking for the presence of certain no of files in a directory. Only if the required no of files are present should I continue with my processing. For e.g. in the temp directory below, there are 4 files of the format sample_aa(bb)(cc)(dd)_test. I need to check the count of these... (17 Replies)
Discussion started by: swasid
17 Replies

3. Shell Programming and Scripting

[Solved] Count characters of variable from right

My variable is something like: f="/Volumes/VERVE/MOOTON_CALL/01_shots/XX/xx0195/Projects/program/rs0195_v400001.aep" I use ${f:63:6} to call "rs0195" as characters counted from the left, but it'd be so much easier to count from the right. If ${f:95:10} counts from the left, what would... (2 Replies)
Discussion started by: scribling
2 Replies

4. Shell Programming and Scripting

count lines in file to variable

I have a text file in which you need to identify the number of lines that looks like this: awk '{x + +} END {print x}' filename The problem is that I do not know how this data to any variable in which then need to continue to work in a cycle for .. do not know someone help? Sorry for my... (4 Replies)
Discussion started by: gizmo16
4 Replies

5. Shell Programming and Scripting

To count a string with in a variable

I am writing a ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or... (6 Replies)
Discussion started by: vpv0002
6 Replies

6. Shell Programming and Scripting

count of a string within a variable

I am writing ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or... (3 Replies)
Discussion started by: vpv0002
3 Replies

7. Shell Programming and Scripting

Dynamic Variable Based on Count

I'm trying to assign variables that include the current value of a count, but I can't seem to get it working... this script is incomplete, but some guidance on how to use a dynamic variable would be helpful: Sample Input: bash-2.03$ more sg2.txt Results for group6 443 1394 Results for... (3 Replies)
Discussion started by: earnstaf
3 Replies

8. Shell Programming and Scripting

Storing count(*) into unix variable

Hi, I've just started on unix shell scripting a few days ago. I tried look for the solution to my problem over the net but to no avail. The objective of my script is to count the number of records in a particular table from a Oracle database. Ok, here's my problem, when I run this script: ... (1 Reply)
Discussion started by: mervinboyz
1 Replies

9. Shell Programming and Scripting

setting file count to a variable

Hey guys. My goal here is to count the number of .dat files in in a directory(28 files). If 28 files exist I am ok. Having trouble doing this. Any help would b e greatly appreciated. #!/usr/bin/ksh #============================================================================= ### Define local... (3 Replies)
Discussion started by: ecupirate1998
3 Replies

10. Shell Programming and Scripting

select count(*) into a variable

Hi, Could anybody help me how can I assign the value of "select count(*) from table1" to a variable in an unix shell script. Thanks. (4 Replies)
Discussion started by: nguda
4 Replies
Login or Register to Ask a Question