Sponsored Content
Full Discussion: Joining string arguments
Top Forums Shell Programming and Scripting Joining string arguments Post 302367104 by pludi on Sunday 1st of November 2009 09:34:45 AM
Old 11-01-2009
The only way I could think of was using evil eval:
Code:
#!/bin/bash

result=""
for ((  i = 3 ;  i <= $#;  i++  ))
do
    result=$result" "$( eval "echo \${$i}" )
done
echo $result

Be careful, because any errors inside the eval aren't caught at parse time, but show up during run time, which can lead to bad things when you don't expect them.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Joining string on multiple files

Hi guys, I am a forum (and a bit of a unix) newbie, and I currently have a tricky problem lying ahead of me. I have multiple files, and I am looking to join the files on the first column. Example: File 1 andy b 100 amy c 200 amy d 300 File 2 andy c 200 amy c 100 clyde o 50 ... (3 Replies)
Discussion started by: jdr0317
3 Replies

2. Shell Programming and Scripting

Need help joining two files with a common string

Hi all, I have one file that is in the form: S0243K05_T7_S0243K05_|_BASS2243.C7_K05 groupVI. 88.76 S0137F20_SP6_S0137F20_|_BASS2137d.SPB2.2_C10 groupXXI 88.06 S0056F03_T7_S0056F03_|_BASS256c.C7_C02 groupXIX 85.99 S0056F03_T7_S0056F03_|_BASS256c.C7_C02 groupXIX 83.23... (3 Replies)
Discussion started by: repiv
3 Replies

3. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

4. Shell Programming and Scripting

string manipulation in arguments

Hi all, I have a requirement where I am taking the first argument as argument name and storing the second argument in argument name as value. Thanks to ppl here, i learnt to do it.:p while ( $1 != "" ) set arg = $1 shift set val = "$1" echo "set... (2 Replies)
Discussion started by: animesharma
2 Replies

5. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

6. Shell Programming and Scripting

Removing command line arguments from string list

I am passing a list of strings $list and want to remove all entries with --shift=number, --sort=number/number/..., --group=number/number/... Also are removed whether upper or lower case letters are used For example the following will all be deleted from the list --shift=12 --shift=2324... (7 Replies)
Discussion started by: kristinu
7 Replies

7. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

8. 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

9. Shell Programming and Scripting

Processing arguments in a string

Hi The following code works when reading the arguments from the command line but fails when I try to read from a string. So this works while ; do case $1 in -dbversion) if '`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift;... (6 Replies)
Discussion started by: user052009
6 Replies

10. Shell Programming and Scripting

ksh parsing arguments in a string rather than from the cmdln

Hi. I have a piece of code that reads and parses command line options. I'd like to alter it slightly to read from a string (that's set elsewhere in the script) rather than directly from the command line (arg). Can somebody show me how to do this? Many thanks. My code is as follows: typeset... (6 Replies)
Discussion started by: user052009
6 Replies
GETOPT(1)						    BSD General Commands Manual 						 GETOPT(1)

NAME
getopt -- parse command options SYNOPSIS
args=`getopt optstring $*` set -- `getopt optstring $*` DESCRIPTION
getopt is used to break up options in command lines for easy parsing by shell procedures, and to check for legal options. [Optstring] is a string of recognized option letters (see getopt(3)); if a letter is followed by a colon, the option is expected to have an argument which may or may not be separated from it by white space. The special option ``--'' is used to delimit the end of the options. getopt will place ``--'' in the arguments at the end of the options, or recognize it if used explicitly. The shell arguments ($1, $2, ...) are reset so that each option is preceded by a ``-'' and in its own shell argument; each option argument is also in its own shell argument. getopt should not be used in new scripts; use the shell builtin getopts instead. EXAMPLES
The following code fragment shows how one might process the arguments for a command that can take the options [a] and [b], and the option [c], which requires an argument. args=`getopt abc: $*` if [ $? -ne 0 ]; then echo 'Usage: ...' exit 2 fi set -- $args while [ $# -gt 0 ]; do case "$1" in -a|-b) flag=$1 ;; -c) carg=$2; shift ;; --) shift; break ;; esac shift done This code will accept any of the following as equivalent: cmd -acarg file file cmd -a -c arg file file cmd -carg -a file file cmd -a -carg -- file file IEEE Std 1003.2 (``POSIX.2'') mandates that the sh(1) set command return the value of 0 for the exit status. Therefore, the exit status of the getopt command is lost when getopt and the sh(1) set command are used on the same line. The example given is one way to detect errors found by getopt. DIAGNOSTICS
getopt prints an error message on the standard error output when it encounters an option letter not included in [optstring]. SEE ALSO
sh(1), getopt(3) HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Behavior believed identical to the Bell version. BUGS
Whatever getopt(3) has. Arguments containing white space or embedded shell metacharacters generally will not survive intact; this looks easy to fix but isn't. The error message for an invalid option is identified as coming from getopt rather than from the shell procedure containing the invocation of getopt; this again is hard to fix. The precise best way to use the set command to set the arguments without disrupting the value(s) of shell options varies from one shell ver- sion to another. BSD
November 28, 2009 BSD
All times are GMT -4. The time now is 09:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy