Question about sorting -- how to pass an array to a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about sorting -- how to pass an array to a function
# 1  
Old 06-28-2013
Question Question about sorting -- how to pass an array to a function

Hi, guys

I just wanted to sort the elements of an array ascendingly.
I know the following code does work well:
Code:
array=(13 435 8 23 100)
for i in {0..4}
do
	j=$((i+1))
	while [[ $j -le 4]]
	do
		if [[ ${array[$i]} -le ${array[$j]} ]]
		then :
		else
			min=${array[$j]}
			${array[$j]}=${array[$i]}
			${array[$i]}=$min
		fi
		((j++))
	done
done

However, I wanted to write a function named "sort" and pass the array as argument to the function.
I know the following code doesn't work:
Code:
array=(13 435 8 23 100)
sort()
{
for i in {0..4}
do
	j=$((i+1))
	while [[ $j -le 4]]
	do
		if [[ ${$1[$i]} -le ${$1[$j]} ]]
		then :
		else
			min=${$1[$j]}
			${$1[$j]}=${$1[$i]}
			${$1[$i]}=$min
		fi
	done
done
}
sort array

And I know it'll be better by using the command "eval" to pass the array to the function like this:
Code:
eval first_num=\${$1[$i]}

but I can't put it to the correct place in the function.
So please give me some ideas~~ Thx
# 2  
Old 06-28-2013
Using eval means you can pass names as string parameters, put that string into a command string, and eval theat command string to get the shell to re-parse it as script. For instance:
Code:
pick_an_arg(){
  arg_no=$1
  shift
  eval 'echo "$'"$arg_no"'"'
 }

lets you print the fourth arg using arguments: 3 ? ? fourth_arg ? ? ? ?

Last edited by DGPickett; 06-28-2013 at 05:03 PM.. Reason: better quoting for safe eval !
# 3  
Old 06-28-2013
Be very very careful using eval, however. It will parse any valid shell syntax it finds -- including things you might not have intended, such as string contents. Someone putting `rm -Rf ~/` into your array could be very bad.
# 4  
Old 06-28-2013
Quote:
Originally Posted by Corona688
Be very very careful using eval, however. It will parse any valid shell syntax it finds -- including things you might not have intended, such as string contents. Someone putting `rm -Rf ~/` into your array could be very bad.
So how about my question? How to pass the array as argument to the function?
SmilieSmilie
# 5  
Old 06-28-2013
You've been given the answer. Pass the array name into the function, assemble the statement you want as text in a string, then use eval to parse that text as a shell statement (since shell will not do that kind of double-think unless you ask).
# 6  
Old 06-28-2013
Quoting of anything going into eval is helpful, and if the input is human, maybe a check for meta-characters. It is very like preventing a SQL Injection attack (which were allowed by sloppy SQL tool choices).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

3. UNIX for Dummies Questions & Answers

How to pass first array value?

Hi, I am creating filesystem for block device, but I want to pass array value one by one acording to block device count. $tmp1 = block device count 3 $blockdevice = So I want to first pass sdb1 alone in loop, how to take only block device seprately from $blockdevice array. (1 Reply)
Discussion started by: stew
1 Replies

4. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

5. Shell Programming and Scripting

Split list of files into an array and pass to function

There are two parts to this. In the first part I need to read a list of files from a directory and split it into 4 arrays. I have done that with the following code, # collect list of file names STATS_INPUT_FILENAMES=($(ls './'$SET'/'$FOLD'/'*'in.txt')) # get number of files... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

6. Shell Programming and Scripting

How to pass an array to a function in shell script.?

hi, I have a array say SAP_ARRAY="s1.txt" SAP_ARRAY="s2.txt" how can i pass this full array to a function. here is the sample code i am using.. CHECK_NO_FILES() { FARRAY=$1 echo "FARRAY = $FARRAY" echo "FARRAY = $FARRAY" ............... (5 Replies)
Discussion started by: Little
5 Replies

7. Shell Programming and Scripting

pass function as argument to a function

I have the following code : function1 () { print "January" } function2() { case $1 in January) print "Dzisiaj mamy styczen" ;; *) ;; } main() { (1 Reply)
Discussion started by: presul
1 Replies

8. Shell Programming and Scripting

How to pass an array from SHELL to C function

Hi, I have an output generated from a shell script like; 0x41,0xF2,0x59,0xDD,0x86,0xD3,0xEF,0x61,0xF2 How can I pass this value to the C function, as below; int main(int argc, char *argv) { unsigned char hellopdu={above value}; } Regards Elthox (1 Reply)
Discussion started by: elthox
1 Replies

9. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

10. Shell Programming and Scripting

Can we pass array with call by value in function

I want to pass an array in my function, And my function will be changing the elements of the array in the fuction, but it should not affect the values in my array variable of main function (1 Reply)
Discussion started by: ranjithpr
1 Replies
Login or Register to Ask a Question