Sponsored Content
Full Discussion: For i in loops on 2 arrays
Top Forums Shell Programming and Scripting For i in loops on 2 arrays Post 303031361 by batchenr on Wednesday 27th of February 2019 04:21:34 AM
Old 02-27-2019
For i in loops on 2 arrays

Hey ,

i have this script and i have these loops so it can find a match between 2 arrays :

Code:
ARRAY_1=(one two three)
ARRAY_2=(A B C)
VAR='B'

for NUMBERS in "${ARRAY_1[@]}"
do
        for LETTERS in "${ARRAY_2[@]}"
        do
        if      [[ $VAR == *"$LETTERS"* ]];then
                VAR='LETTERS'
                break
        fi
        if [[ $VAR == *"$NUMBERS "* ]];then
        VAR='NUMBERS'
        break

        else
        VAR='DEFAULT'

        fi
        done
done

echo "$VAR"

the thing is that the loops dont break!
after it fines a match just keep looping and gets to default:

Code:
+ ARRAY_1=(one two three)
+ ARRAY_2=(A B C)
+ VAR=B
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ B == *\A* ]]
+ [[ B == *\o\n\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\B* ]]
+ [[ DEFAULT == *\o\n\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\C* ]]
+ [[ DEFAULT == *\o\n\e* ]]
+ VAR=DEFAULT
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\A* ]]
+ VAR=LETTERS
+ break
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ LETTERS == *\A* ]]
+ [[ LETTERS == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\B* ]]
+ [[ DEFAULT == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\C* ]]
+ [[ DEFAULT == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ echo DEFAULT
DEFAULT

why ?!

i want it to work this way :
Code:
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ B == *\A* ]]
+ [[ B == *\o\n\e* ]]
+ [[ B == *\B ]]
+ [[ B == *\T\W\O* ]]
+ [[ B == *\C* ]]
+ [[ B ==  *\t\h\r\e\e* ]]


Last edited by batchenr; 02-27-2019 at 05:41 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Loops

Can anybody help please. I am trying to right a script which will loop until a certain action has been performed. For example i current have two batch jobs i would like to put into a wait status. Batch Jobs A and B . The script i am trying to get to work is below. jobs="A B" COUNT=0 while... (2 Replies)
Discussion started by: mariner
2 Replies

2. Shell Programming and Scripting

korn shell "loops & arrays"

Hi, I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares. For example: file1 has a list of user ids (about 900) from the company's e-mail server. file2 has a list of user ids (about 50 or so) from... (7 Replies)
Discussion started by: muzica
7 Replies

3. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

4. Shell Programming and Scripting

trying to learn for loops, and arrays at the same time

Ok, I've already completed the task this is for, but now I'm trying to go back and find more eloquent solutions for future reference. I have a report I've generated that is formatted like this: 1033 1 1079 4 1453 5 2205 6 1933 7 461 8 646 9 1655 12 975 13 1289 14 The first number is... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

5. Shell Programming and Scripting

arrays and while loops in bash

hi guys, i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array.. i get a =: command not found error.. i=0 while do ${master_array}=${ARRAY} ((i++)) done is there something i am missing? (4 Replies)
Discussion started by: npatwardhan
4 Replies

6. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

7. UNIX for Dummies Questions & Answers

Help with for loops

Hi, I am starting to enhance my scripting knowledge and need some assistance with simple 1 line for loops. This may help to do a mass permissions change on a big apache doc root while I have an insane customer on my phone. What is the best resource to learn this skill and what are some that... (5 Replies)
Discussion started by: Oshie74
5 Replies

8. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

9. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

10. UNIX for Dummies Questions & Answers

Dealing with Double Loops, Arrays and GREP

Can someone please help me to learn how to deal with loops, arrays and grep? I have two arrays (lets say I and j) each in a separate file And have file with lines of data I need to extract, such as Ruby Smith: some text here Ruby Smith: some other text here Ruby Brown: some text here Ruby... (10 Replies)
Discussion started by: A-V
10 Replies
PAM_ENV.CONF(5) 						 Linux-PAM Manual						   PAM_ENV.CONF(5)

NAME
pam_env.conf - the environment variables config file DESCRIPTION
The /etc/security/pam_env.conf file specifies the environment variables to be set, unset or modified by pam_env(8). When someone logs in, this file is read and the environment variables are set according. Each line starts with the variable name, there are then two possible options for each variable DEFAULT and OVERRIDE. DEFAULT allows and administrator to set the value of the variable to some default value, if none is supplied then the empty string is assumed. The OVERRIDE option tells pam_env that it should enter in its value (overriding the default value) if there is one to use. OVERRIDE is not used, "" is assumed and no override will be done. VARIABLE [DEFAULT=[value]] [OVERRIDE=[value]] (Possibly non-existent) environment variables may be used in values using the ${string} syntax and (possibly non-existent) PAM_ITEMs may be used in values using the @{string} syntax. Both the $ and @ characters can be backslash escaped to be used as literal values values can be delimited with "", escaped " not supported. Note that many environment variables that you would like to use may not be set by the time the module is called. For example, HOME is used below several times, but many PAM applications don't make it available by the time you need it. The "#" character at start of line (no space at front) can be used to mark this line as a comment line. EXAMPLES
These are some example lines which might be specified in /etc/security/pam_env.conf. Set the REMOTEHOST variable for any hosts that are remote, default to "localhost" rather than not being set at all REMOTEHOST DEFAULT=localhost OVERRIDE=@{PAM_RHOST} Set the DISPLAY variable if it seems reasonable DISPLAY DEFAULT=${REMOTEHOST}:0.0 OVERRIDE=${DISPLAY} Now some simple variables PAGER DEFAULT=less MANPAGER DEFAULT=less LESS DEFAULT="M q e h15 z23 b80" NNTPSERVER DEFAULT=localhost PATH DEFAULT=${HOME}/bin:/usr/local/bin:/bin :/usr/bin:/usr/local/bin/X11:/usr/bin/X11 Silly examples of escaped variables, just to show how they work. DOLLAR DEFAULT=$ DOLLARDOLLAR DEFAULT= OVERRIDE=$${DOLLAR} DOLLARPLUS DEFAULT=${REMOTEHOST}${REMOTEHOST} ATSIGN DEFAULT="" OVERRIDE=@ SEE ALSO
pam_env(8), pam.d(5), pam(7) AUTHOR
pam_env was written by Dave Kinchlea <kinch@kinch.ark.com>. Linux-PAM Manual 09/19/2013 PAM_ENV.CONF(5)
All times are GMT -4. The time now is 10:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy