Putting strings into positioning array in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Putting strings into positioning array in loop
# 1  
Old 08-25-2018
Putting strings into positioning array in loop

i need to add 2 string variables into a positioning array , repeatedly - in loop.

First string in $2, second to $3 then up to the desired count incrementing the "position".
Using set -- alone does not increment the count so I end up with 2 variables in the array.



How do I increment the positioning index ?



Code:
 echo " # add loop" 
 arrayTEST=( "$@" )
 for i in {0..16..2}
 do 
 set -- "CLI command" "CLI command description "   # assign variables to ar$
# next position in array ??

 done 
 echo "# number of elements in array " 
 echo "$#"       # number of elements in array
 echo " # content  of array" 
 echo "$@"       # contennt  of array

# 2  
Old 08-26-2018
The positional parameters are limited in the sense that it can only be filled in one go, either by passing them to a script of function or using the set command.
So you can only manipulate the this to set the positional parameters, for example:
Code:
set -- "" "String in position 2" "Another string in pos 3"

The empty string as the first parameter put the other strings in position 2 and 3.
But then the number of elements is not accurate, because
echo "$#" will report 3

But why don't you use a regular array, available in bash or ksh? Then you can put strings in any position you like.
Code:
arr[2]="String in element 2" 
arr[3]="Another string in element 3"

In this case the the elements arr[0] and arr[1] remain unused
Code:
$ echo "${#arr[@]}"
2


Last edited by Scrutinizer; 08-26-2018 at 04:00 AM..
# 3  
Old 08-26-2018
Sorry, but this post does not answer / address my question.

I have no problem building the array manually, but I was just trying to use loop.

Actually I like to know if such operation is even possible since the limit of the positioning parameters in the array is not defined.
# 4  
Old 08-28-2018
Does this do what you want?
Code:
for i in {0..16..2}
do 
   set -- "$@" "CLI command" "CLI command description "
done

Andrew
# 5  
Old 08-28-2018
Maybe.

Here is the debug output as code is being processed.

It looks to me that each pass add next string and than debug prints the whole file.

But it builds multiple "strings"and it really does not matter how.

I did add the "index" so I can "see" the progress.


Thanks



Code:
+ set -- 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description '
+ echo 'index  10'
index  10
+ for i in {0..16..2}
+ set -- 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description '
+ echo 'index  12'
index  12
+ for i in {0..16..2}
+ set -- 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description '
+ echo 'index  14'
index  14
+ for i in {0..16..2}
+ set -- 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description ' 'CLI command' 'CLI command description '
+ echo 'index  16'
index  16
+ pause
+ read -p 'Press [Enter] key to continue...'
Press [Enter] key to continue...

------ Post updated at 10:26 AM ------

OK. the code does the job. Many thanks.


Minor problem - now I need to copy the positioning array to a file.

This simple verification code does reads the array in reverse.
It would make things easier if I could add the lines into the file starting with 1 and go up.



I suppose I can change the while[condition] and check for # for != 16 in this case.

Or can I start from $# = 1 and shift "up" ?


Code:
testLoopAddition(){
  55 for i in {0..16..2}
  56 do 
  57    set -- "$@" "CLI command" "CLI command description "
  58    echo "index  $i"     
  59 done
  62 echo "In reverse start with $# positional parameters"
  63 # while positional parameter not empty 
  64 while [ "$1" != "" ]; do
  65     echo "Parameter $# equals $1"
          copy $1 to file here 

  68     shift
  69 done

# 6  
Old 08-29-2018
How do you intend to copy these to a file? Is it one per line to the same file? If so, you could look at the bash built-in printf:
Code:
printf "%s\n" $* > file

This will print your parameter list as a list of space-separated strings, one per line, until your list is exhausted. Try this noddy program:
Code:
#!/bin/bash

printf "%s\n" $*

This might not be what you want, of course. As we were putting spaces in the parameters in the above examples you may prefer this:
Code:
#!/bin/bash

printf "%s\n" "$@"

Basically, if you give printf more arguments than it needs it will use as many as it can and then repeat with the next set until the list is exhausted. So if you wanted to print two parameters per line, use
Code:
printf "%s %s\n"

instead.

Andrew
# 7  
Old 08-29-2018
thanks, I'll try your approach .



I am still trying to understand how bash code is executed.
Perhaps my perception of code line executing "left to right" is basically wrong.



Look at line #277
Every "manual" I read about using "wc" states it "prints".

I do not need "print" - I need to process what I am used to call "function return value".
OK, I understand 'wc" "prints" most likely to some "stdxxx" - as output of "pipe" .
So how does lineCount value , being at the "front" of the pipe get assigned?
And why is the file to be analyzed at the "end" of the code line?



Code:
echo "$LINENO test for actual file contents" 
 277 lineCount=$(wc -l < "$PWD$DEBUG_DIR$DEBUG_MENU")
 278 echo "line count $lineCount"
 279 if [ lineCount = 0 ] 
 280 then
 281    echo "$LINENO File $DEBUG_MENU   empty "
 282    #build defaukt menu in file 
 283    defaultMenu
 284    pause 
 285 else 
 286     echo "$LINENO File $DEBUG_MENU   has data  "
 287     pause 
 288 fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing positioning array as whiptail -- menu option

I may have asked this before, so forgive OF. Problem: I can pass positioning array as -- menu option to whiptail, but it does not show in the whiptail form as an array - only single (first member "lsusb" ) entry / line shows up. Code: DynamicEntry=$(whiptail \ --title "DEBUG... (1 Reply)
Discussion started by: annacreek
1 Replies

2. Shell Programming and Scripting

Help tabulating file putting repeated strings as headers

Hi. May somebody help me with this. I´m trying to tabulate the following input file, but the desired output I´m getting is incorrect. I have access to GNU/LINUX (Ubuntu) and Cygwin Input file STAGE = 1 ID = 0 NAME = JFMSC TYPE = MLRR DFRUL = PERMISSION ADDR = 1001 RRUL =... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

3. Shell Programming and Scripting

How to strip some characters before putting in array?

Hi Gurus, my current code like below: nawk '{f1 = (NF>1)?$1:""}{print f1, $NF}'|sed -e 's/s(/,/g;s/)//g;s/ *,/,/'|nawk -F"," '{ab}END{for (i in b) if (!(i in a))print i}' I have file like below. (this is autosys job dependencies) the job with s() is dependencies, the job without s() is... (10 Replies)
Discussion started by: ken6503
10 Replies

4. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

5. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

6. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

7. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

8. Programming

Multidimensional array of strings with vector.

I've been struggling with this for quite some time. I decided I should get some help with this. Nothing is working. I'm getting a segmentation fault or out of bounds error when I try to load the entries in the for loop.I'm really frustrated. :mad: Compiling isn't the problem. It's crapping out on... (5 Replies)
Discussion started by: sepoto
5 Replies

9. Shell Programming and Scripting

How to run a loop for assigning strings which are present in a file to an array

Hi Forum, I am struggling with the for loop in shell script. Let me explain what is needed in the script. I have a file which will conatin some strings like file1 place1 place2 place3 checkpoint some other text some more text Now what my requirement is the words ... (2 Replies)
Discussion started by: siri_14
2 Replies

10. Programming

array of strings

Hi I need a better idea to implementing following in my code. I need to store 80 long strings that will be used to display one by one in my GUI application. now i am storing those 80 long string in following two dimentational array. uchar vpn_alm_long_str={ } each index will be an... (1 Reply)
Discussion started by: davidcreston
1 Replies
Login or Register to Ask a Question