Trouble Assigning Variable with Function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble Assigning Variable with Function
# 1  
Old 09-30-2014
Trouble Assigning Variable with Function

OSX 10.9

Good morning/afternoon/evening.

I'm hoping to get some insight on assigning a variable when calling a function. The code below looks at my array and checks if the path exists. My actual code will have multiple arrays and I would like to define a specific array when I call the function, but I am getting a bad substitution error.

This is the code without assigning variables in function
Code:
## MY ARRAY
array=(
/Library/Application\ Support/
/Library/LaunchAgents/folder_that_doesnt_exist
)
## MY FUNCTION
results () 
{
    for i in "${!array[@]}"; do 
       if [[ -e "${array[$i]}" ]]; then 
       echo "**Found**: "${array[$i]}""
       else echo "Not Found: "${array[$i]}""
       fi
    done
}

results

##Output:
**Found**: /Library/Application Support/
Not Found: /Library/LaunchAgents/folder_that_doesnt_exist

Example attempt of assigning the variables, which failed:

Code:
array=(
/Library/Application\ Support/
/Library/LaunchAgents/folder_that_doesnt_exist
)
results () 
{
    for i in $1; do 
       if [[ -e $2 ]]; then 
       echo "**Found**: $2"
       else echo "Not Found: $2"
       fi
    done
}
results "${!array[@]}" "${array[$i]}"

# 2  
Old 09-30-2014
Use "${array[@]}" not "@{!array[@]}

Also, for does not work that way. i becomes a string, not a number index.

Also, you can put a ! in the expression instead of an empty 'if'.

Code:
for i in "${array[@]}"
do
        if [[ ! -e "$i" ]]
        then
                echo "$i not found">&2
        fi
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-30-2014
Try:
Code:
array=(
/Library/Application\ Support/
/Library/LaunchAgents/folder_that_doesnt_exist
)
results () 
{
    for i in "${@}"	; do 
       if [[ -e "$i" ]]; then 
       echo "**Found**: $i"
       else echo "Not Found: $i"
       fi
    done
}
results "${!array[@]}" "${array[$i]}"

This User Gave Thanks to sea For This Post:
# 4  
Old 09-30-2014
Yes, that is a good idea for combining arrays -- pass them all into the function as arguments, they will be available as "$@".
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-30-2014
Quote:
Originally Posted by Corona688
Use "${array[@]}" not "@{!array[@]}

Also, for does not work that way. i becomes a string, not a number index.

Also, you can put a ! in the expression instead of an empty 'if'.

Code:
for i in "${array[@]}"
do
        if [[ ! -e "$i" ]]
        then
                echo "$i not found">&2
        fi
done

I'm not sure where "@{!array[@]} is coming from, I used "${array[@]}" in my original example. Do you know how I can pass "${array[@]}" into the function? I tried SEA's example but it outputs:
Code:
Not Found: 0
Not Found: 1
**Found**: /Library/Application Support/

To clarify, I have several arrays that I would like to call independently. What I'm trying to achieve is- pass an array into that function by just typing:
Code:
results "${!array1[@]}"
results "${!array2[@]}"

and have it output:

Code:
**Found**: /path/defined/in/array1
Not Found: /path/that does/not/exist/defined/in/array1
**Found**: /path/defined/in/array2
Not Found: /path/that does/not/exist/defined/in/array2

But I am having trouble defining the array to the function, if I type in "${array[@]}" & "${array[$i]}" manually into the for loop, it works fine. But because I will have 10-20 arrays, I want to call them independently without typing and maintaining all that embedded code.
# 6  
Old 09-30-2014
Quote:
Originally Posted by sudo
To clarify, I have several arrays that I would like to call independently.
This is not obvious when you post an example containing one and exactly one array used directly.

What do you mean by 'call independently'? Are you trying to make an array-full-of-arrays kind of thing or what?

"${array[$i]}" is wrong, for does not work that way, see my above post.

Quote:
Do you know how I can pass "${array[@]}" into the function?
By doing function "${array[@]}" It all ends up in $@ inside the function. To show further:

Code:
dosomething () {
        for i in "$@"
        do
                echo "dosomething:  $i"
        done
}

array1=( "a b" c "d e" )
array2=( q w e r t )

dosomething "${array1[@]}" extra stuff "${array2[@]}"

This is not pseudo-code. Paste it and it should work.

Last edited by Corona688; 09-30-2014 at 04:26 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 09-30-2014
Yes, I was not clear in my initial post in an attempt to keep things concise, but thank you for your last post it exemplified exactly what I was looking for Smilie

Cheers
Patrick
This User Gave Thanks to sudo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Trouble assigning user to group in OpenLDAP

Hello, I am working on setup LDAP Server and facing issue related to assigning user to a group. Below is the LDAP structure i am using. I have created Users,Groups and Servers ou's and sub ou's added to the same or Users as well as Groups OU. Logged in as: cn=Manager,dc=bebolabs,dc=net ... (0 Replies)
Discussion started by: sunnysthakur
0 Replies

2. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

3. UNIX for Dummies Questions & Answers

Trouble Assigning AWK variables

Hi, I made an executable file in terminal and it looks like this. echo Enter the name of the file without the .wig extension read NAME echo Enter the ratio read RATIO awk '{$2*=$RATIO;{print $0}}' ${NAME}.wig > ${NAME}normalized.wig I have a file with several million lines that look... (6 Replies)
Discussion started by: wyarosh
6 Replies

4. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

5. Shell Programming and Scripting

variables not assigning in a function

Hi GUYS, I have function. I am assigning a line count to count variable. But it is throwing an error at this line. function_recur (){ #file being created in this function lenth = `wc -l function_outpu.dat`; echo $lenth; } this is the error i got rec.ksh: lenth: not found. ... (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

7. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

8. UNIX for Dummies Questions & Answers

Trouble with UNIX tr (translate) function

UNIX script - problem. I want the spaces in my Item variable to be replaced with a question mark. Can you tell me what I am doing wrong? This is the whole code line. Item | tr -s " " "?" Why is this not making any changes to the Item value? Thanks for any help you can give! tg (2 Replies)
Discussion started by: by_tg
2 Replies

9. Shell Programming and Scripting

Assigning Value of variable

Hi In my shell script, I'm trying to find the line count of a file and assign it to a variable. LINE_COUNT=$(wc -l $FILE_NAME) But when i display LINE_COUNT, i'm getting the linecount concatenated with the file name. I want only the number. How can i get the line count alone ? Someone... (2 Replies)
Discussion started by: janemary.a
2 Replies

10. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies
Login or Register to Ask a Question