bash - Add all arguments except first to a string -
trying parse arguments 1 string code giving me errors can't find i
:
test: line 3: =: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found test: line 7: [i: command not found
code bellow
#!/bin/sh $msg="" in $@ if [i -gt 1]; msg="$msg $i" fi done
edit: thx help, got work. final solution if anyones interested:
#!/bin/sh args="" in "${@:2}" args="$args $i" done
your specific error messages showing because:
assigning variable not done
$
character in$msg=""
, instead should usingmsg=""
; and[
command, 1 should separated other words white space, shell doesn't think you're trying execute mythical[i
command.
however, have couple of other problems. first value of i
needs obtained $i
, not i
. using i
on own give error along lines of:
-bash: [: i: integer expression expected
because i
not numeric value.
secondly, neither i
nor $i
going index can compare 1, $i -gt 1
expression not work. word $i
expand value of argument, not index.
however, if want process first element of argument list, bash
has c-like constructs make life lot easier:
for ((i = 2; <= $#; i++)) ; # 2 argc inclusive. echo processing ${!i} done
running arguments hello name pax
, result in:
processing processing name processing processing pax
for constructing string containing arguments, use like:
msg="$2" # second arg (or blank if none). ((i = 3; <= $#; i++)) ; # append other args. msg="$msg ${!i}" done
which give (for same arguments above):
[my name pax]
although, in case, there's simpler approach doesn't involve (explicit) loops @ all:
msg="${@:2}"
Comments
Post a Comment