Joining string arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining string arguments
# 1  
Old 11-01-2009
Joining string arguments

Hi,

how can I join given arguments (not starting from the first one) to form one string, each argument separated by a space. For example, out of 5 given arguments, I'll like to start joining from the 3rd to the last. In python there exists something like ' '.join(sys.argv[3:]) and it starts joining from the 3rd argument to the last, each separated by a space. I looked for something similar to "join()" but couldn't find one, so I decided to write a script which joins the arguments.
My script looks like this:
Code:
#!/bin/bash

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

And the result I get using 4 arguments is:
Code:
$3 $4

Could someone please help me ?
Thanks.

Last edited by pludi; 11-01-2009 at 10:26 AM.. Reason: code tags, please...
# 2  
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.
# 3  
Old 11-01-2009
Hey,

this is great, it works. I think I have to read more on "eval".
Thanks for the prompt reply.
# 4  
Old 11-01-2009
Code:
#!/bin/bash
params="$@"
echo ${params#* * }



---------- Post updated at 06:58 AM ---------- Previous update was at 06:55 AM ----------

Code:
#!/bin/bash
shift;shift
echo "$@"

# 5  
Old 11-01-2009
Code:
#!/bin/bash

shift 2
echo "$@"

or

Code:
#!/bin/bash
shift 2
for var in "$@"
do
  result=$result" "$var
done

echo $result

Woops sorry, as has been pointed out should be shift 2 not 3. I must make a note to read the posts before replying to them in future.... Smilie

Last edited by steadyonabix; 11-01-2009 at 11:36 AM..
# 6  
Old 11-01-2009
Wow,
the code is getting better and better.
I'm just soo happy.

Thanks to you all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question