Reputation: 13
I'm trying to assign all of the parameters to a shell script in the same way that $@ or $* works. Might be easier to explain with an example of what I am trying to do
if [ $# == 0 ]; then
FIELDS="$($findfields)"
else
FIELDS=$@
fi
#Show the fields
for field in "$FIELDS"
do
echo "$field"
done
When I run the script with no arguments, a separate script is called and the output is as expected
field1
field2
field3
When I run the script with parameters
$mysrcipt.sh field1 field2 field3
I get the following
field1 field2 field3
How can I assign $@ to the FIELDS variable so that it works in the same way as the external script?
Many thanks
Upvotes: 1
Views: 163
Reputation: 289725
To store the values so that they form an array, do:
FIELDS=( "$@" )
$ cat a
if [ $# == 0 ]; then
FIELDS="$($findfields)"
else
FIELDS=( "$@" )
fi
#Show the fields
for field in "${FIELDS[@]}"
do
echo "$field"
done
$ ./a a b c
a
b
c
$ ./a "a b c"
a b c
Upvotes: 1