Sponsored Content
Top Forums Shell Programming and Scripting Print arguments with the help of variable Post 302498227 by apmcd47 on Sunday 20th of February 2011 04:40:58 PM
Old 02-20-2011
Quote:
Originally Posted by hakermania
Hey thanks, this worked....!
Can you explain me please what the number 5 represents here?
I mean if I need to take the arguments from $7 and next, should I do
${@:7:5}
?
Basically the parameter expansion is ${name : offset : length}. When the parameter name is that of a variable the offset and length are string related, e.g:
Code:
$ str="123andrew789"
$ echo ${str:3:6}
andrew

When it is @ offset and length refer to positional parameters. so
Code:
${@:3:5}

will pick up parameters 3-7 (if that many exist). However
Code:
${@:3}

will pick up all positional parameters after the third.

Andrew
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies

2. Shell Programming and Scripting

How to call arguments with variable in a script??

Hello, I was wondering if it were possible to call arguments passed to a script using a variable. For example: sh script.sh yes no good bad x=$# while do echo (last argument, then second last etc until first argument) let x=($x-1) done should print out bad good no (4 Replies)
Discussion started by: VanK
4 Replies

3. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

4. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

5. Shell Programming and Scripting

Unable to print dynamic arguments in for loop

Hi All, I want to pass few dynamic arguments to shell script. The number of arguments differ each time I call the script. I want to print the arguments using the for loop as below. But not working out. for (( i=1; i<=$#; i++ )) do echo $"($i)" done /bin/sh test.sh arg1 arg2 arg3 ... (1 Reply)
Discussion started by: laalesh
1 Replies

6. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

7. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

8. Shell Programming and Scripting

Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments. Example: prargv "-e" "--examples" Inside prargv, I want to print all the arguments using echo echo "$@" This returns --examples rather than -e --examples" This problem can be fixed... (3 Replies)
Discussion started by: kristinu
3 Replies

9. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

10. Shell Programming and Scripting

Arguments in variable vs direct string

Hello Community! Let's say that we have some script which counts its arguments number: arguments_count.sh: #!/bin/sh echo "Number of arguments="$#and some test script: test.sh: #!/bin/sh my_args="1 2 3 '4 5' 6" echo "Count of arguments when using my_args:" ./arguments_count.sh $my_args... (12 Replies)
Discussion started by: break_da_funk
12 Replies
varargs(5)							File Formats Manual							varargs(5)

NAME
varargs - handle variable argument list SYNOPSIS
DESCRIPTION
This set of macros enables programmers to write portable procedures that accept variable argument lists. Routines that have variable argu- ment lists (such as but do not use are inherently nonportable, because different machines use different argument-passing conventions (see printf(3S)). is used as the parameter list in a function header. is a declaration for No semicolon should follow is a type defined for the variable used to traverse the list. is called to initialize pvar to the beginning of the list. The type of argN should be the same as the argument to the function just before the variable portion of the argument list. returns the next argument in the list pointed to by type is the type the argument is expected to be. Different types can be mixed, but it is up to the routine to know what type of argument is expected, because it cannot be determined at runtime. is used to clean up. Multiple traversals, each bracketed by are possible. NOTE: The header file is provided for compatibility with pre-ANSI compilers and earlier releases of HP C/HP-UX. It is superceded by which includes all of the macros. EXAMPLE
The following example shows a possible implementation of (see exec(2)): The next example illustrates how a function that receives variable arguments can pass these arguments down to other functions. To accom- plish this, the first routine in this example) which receives the variable argument list must pass the address pointer resulting from a call to on to any subsequent calls that need to access this same variable argument list. All routines that receive this address pointer in this example) need only to use to access the original variable argument list just as if they were the original routine to be passed the variable arguments. In this example, one can imagine that there are a series of other routines (such as a and that also call the function. #include <stdio.h> #include <varargs.h> #include <unistd.h> int error_count; /* VARARGS4 -- for lint */ int log_errors(log_fp, func_name, err_num, msg_fmt, va_alist) FILE *log_fp; char *func_name; int err_num; char *msg_fmt; va_dcl { va_list ap; /* Print error header information */ (void) fprintf(log_fp, " ERROR in process %d ", getpid()); (void) fprintf(log_fp, " function "%s": ", func_name); switch(err_num) { case ILLEGAL_OPTION: (void) fprintf(log_fp, "illegal option "); break; case CANNOT_PARSE: (void) fprintf(log_fp, "cannot parse input file "); break; ... } /* * Get pointer to first variable argument so that we can * pass it on to v_print_log(). We do this so that * v_print_log() can access the variable arguments passed * to this routine. */ va_start(ap); v_print_log(log_fp, msg_fmt, ap); va_end(ap); } /* VARARGS2 -- for lint */ int v_print_log(log_fp, fmt, ap) FILE *log_fp; char *fmt; va_list ap; { /* * If "%Y" is the first two characters in the format string, * a second file pointer has been passed in to print general * message information to. The rest of the format string is * a standard printf(3S) format string. */ if ((*fmt == '%') && (*(fmt + 1) == 'Y')) { FILE *other_fp; fmt += 2; other_fp = (FILE *) va_arg(ap, char *); if (other_fp != (FILE *) NULL) { /* * Print general message information to additional stream. */ (void) vfprintf(other_fp, fmt, ap); (void) fflush(other_fp); } } /* * Now print it to the log file. */ (void) vfprintf(log_fp, fmt, ap); } WARNINGS
It is up to the calling routine to specify how many arguments there are, because it is not always possible to determine this from the stack frame. For example, is passed a zero pointer to signal the end of the list. can determine how many arguments are present by the format. It is non-portable to specify a second argument of or to because arguments seen by the called function are not or C converts and arguments to and converts arguments to before passing them to a function. SEE ALSO
exec(2), vprintf(3S), stdarg(5). STANDARDS CONFORMANCE
varargs(5)
All times are GMT -4. The time now is 05:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy