Sponsored Content
Full Discussion: Bash Passing An Array
Top Forums Shell Programming and Scripting Bash Passing An Array Post 302691499 by brsett on Friday 24th of August 2012 04:13:14 PM
Old 08-24-2012
Bash Passing An Array

Good grief so this should be easy. Passing an array as an argument to a function. Here is the sample code:

Code:
#/bin/bash

function foo {

  local p1=${1}
  local p2=(${2})
  local p3=${3}

  echo p1 is $p1
  echo p2 is $p2
  echo p3 is $p3

}

d1=data1
d2=data2
a=(bat bar baz)

foo "${d1}" "${a[@]}" "${d2}"

Obviously, it prints
data1
bat
bar

And what I want is:
data1
bat
data2

I would take
data1
bat bar baz
data2

As that would still be workable. I'm simply trying to stuff the array into the second argument somehow that isn't nasty and hard to understand. Obviously in the real example, there are multiple arrays being passed, so playing with the tokens isn't a great solution for me.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies

2. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

3. Programming

Passing by value a char array

Hi I am passing or want to pass value of a char array, so that even thoug the called routine is changing the values the calling function should not see the values changed, meaning only copy should be passed Here is the program #include<iostream.h> #include<string.h> void f(char a); int... (5 Replies)
Discussion started by: rkraj
5 Replies

4. Shell Programming and Scripting

passing line into array

I am doing a shell script in ksh. I have an output from grep that goes something like this: wordIWasLookingFor anotherWordIWasLookingFor yetAnotherWordIWasLookingFor I want to toss each line into an array such that: myArray = wordIWasLookingFor myArray = anotherWordIWasLookingFor... (3 Replies)
Discussion started by: mrwatkin
3 Replies

5. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

6. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

7. Shell Programming and Scripting

Passing Array to awk

I'm trying to use the following command: awk -v array1=${array1} -f "filename.awk" input.txt Then within filename.awk I want to access array1. However, awk mistakes array1 (the third element of the array) for the input file. How I can pass awk this array? It also appears that awk scripts... (3 Replies)
Discussion started by: B-Ry
3 Replies

8. UNIX for Dummies Questions & Answers

Passing values from file into array in Bash

Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"... (3 Replies)
Discussion started by: ShiGua
3 Replies

9. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

10. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies
GLIB-GENMARSHAL(1)						  [FIXME: manual]						GLIB-GENMARSHAL(1)

NAME
glib-genmarshal - C code marshaller generation utility for GLib closures SYNOPSIS
glib-genmarshal [options...] [files...] DESCRIPTION
glib-genmarshal is a small utility that generates C code marshallers for callback functions of the GClosure mechanism in the GObject sublibrary of GLib. The marshaller functions have a standard signature, they get passed in the invoking closure, an array of value structures holding the callback function parameters and a value structure for the return value of the callback. The marshaller is then responsible to call the respective C code function of the closure with all the parameters on the stack and to collect its return value. INVOCATION
glib-genmarshal takes a list of marshallers to generate as input. The marshaller list is either read from standard input or from files passed as additional arguments on the command line. Options --header Generate header file contents of the marshallers. --body Generate C code file contents of the marshallers. --prefix=string, --prefix string Specify marshaller prefix. The default prefix is `g_cclosure_marshal'. --skip-source Skip source location remarks in generated comments. --nostdinc Do not use the standard marshallers of the GObject library, and skip gmarshal.h include directive in generated header files. --g-fatal-warnings Make warnings fatal, that is, exit immediately once a warning occurs. -h, --help Print brief help and exit. -v, --version Print version and exit. Marshaller list format The marshaller lists are processed line by line, a line can contain a comment in the form of # this is a comment or a marshaller specification of the form RTYPE:PTYPE RTYPE:PTYPE,PTYPE RTYPE:PTYPE,PTYPE,PTYPE (up to 16 PTYPEs may be present). The RTYPE part specifies the callback's return type and the PTYPEs right to the colon specify the callback's parameter list, except for the first and the last arguments which are always pointers. Parameter types Currently, the following types are supported: VOID indicates no return type, or no extra parameters. If VOID is used as the parameter list, no additional parameters may be present. BOOLEAN for boolean types (gboolean) CHAR for signed char types (gchar) UCHAR for unsigned char types (guchar) INT for signed integer types (gint) UINT for unsigned integer types (guint) LONG for signed long integer types (glong) ULONG for unsigned long integer types (gulong) INT64 for signed 64bit integer types (gint64) UINT64 for unsigned 64bit integer types (guint64) ENUM for enumeration types (gint) FLAGS for flag enumeration types (guint) FLOAT for single-precision float types (gfloat) DOUBLE for double-precision float types (gdouble) STRING for string types (gchar*) BOXED for boxed (anonymous but reference counted) types (GBoxed*) PARAM for GParamSpec or derived types (GParamSpec*) POINTER for anonymous pointer types (gpointer) OBJECT for GObject or derived types (GObject*) NONE deprecated alias for VOID BOOL deprecated alias for BOOLEAN EXAMPLE
To generate marshallers for the following callback functions: void foo (gpointer data1, gpointer data2); void bar (gpointer data1, gint param1, gpointer data2); gfloat baz (gpointer data1, gboolean param1, guchar param2, gpointer data2); The marshaller list has to look like this: VOID:VOID VOID:INT FLOAT:BOOLEAN,UCHAR The generated marshallers have the arguments encoded in their function name. For this particular list, they are g_cclosure_marshal_VOID__VOID(), g_cclosure_marshal_VOID__INT(), g_cclosure_marshal_FLOAT__BOOLEAN_UCHAR(). They can be used directly for GClosures or be passed in as the GSignalCMarshaller c_marshaller; argument upon creation of signals: GClosure *cc_foo, *cc_bar, *cc_baz; cc_foo = g_cclosure_new (NULL, foo, NULL); g_closure_set_marshal (cc_foo, g_cclosure_marshal_VOID__VOID); cc_bar = g_cclosure_new (NULL, bar, NULL); g_closure_set_marshal (cc_bar, g_cclosure_marshal_VOID__INT); cc_baz = g_cclosure_new (NULL, baz, NULL); g_closure_set_marshal (cc_baz, g_cclosure_marshal_FLOAT__BOOLEAN_UCHAR); SEE ALSO
glib-mkenums(1) BUGS
None known yet. AUTHOR
glib-genmarshal has been written by Tim Janik timj@gtk.org. This manual page was provided by Tim Janik timj@gtk.org. [FIXME: source] 05/02/2010 GLIB-GENMARSHAL(1)
All times are GMT -4. The time now is 05:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy