Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ambiguous Redirect error while accessing cmd line arg in a function

135 views
Skip to first unread message

ram

unread,
Oct 10, 2012, 12:42:52 AM10/10/12
to
I am writting a script to do below activities on a numFile having a set of numbers:
1. Addition
2. Average
3. Max/Min

With Addition code , below error message is coming.

code:
sum=0

sumNumber() ## to sum the numbers from file
{
while read n
do
(( sum+=n ))
done < $1
}
while getopts ":samn" optchar
do
case $optchar in
s) shift ## s - SUM of Number
sumNumber
echo "Sum = "$sum
;;
.
.

calling: $./getOpts -s numFile
error: ./getOpts1: line 14: $1: ambiguous redirect


let me know:
In function body can't we access the command line arguments $1,$2.. after we have used "shift" before calling the function

Kaz Kylheku

unread,
Oct 10, 2012, 1:01:02 AM10/10/12
to
On 2012-10-10, ram <bhatnaga...@gmail.com> wrote:
> sumNumber() ## to sum the numbers from file
> {
> while read n
> do
> (( sum+=n ))
> done < $1
> }

$1 is the argument to the function sumNumber

> while getopts ":samn" optchar
> do
> case $optchar in
> s) shift ## s - SUM of Number
> sumNumber

You didn't pass an argument to sumNumber. So $1 expands to nothing.

Add "set -u" at the top of the script to catch references to unset parameters.

> In function body can't we access the command line arguments $1,$2.. after we

No. Functions have their own arguments, and since names are not used but
positional parameters, the ones in the function shadow the global ones.

Chris F.A. Johnson

unread,
Oct 10, 2012, 12:58:03 AM10/10/12
to
You must explicitly pass arguments to the function:

sumNumber "$1"

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Ed Morton

unread,
Oct 10, 2012, 2:36:34 PM10/10/12
to
ram <bhatnaga...@gmail.com> wrote:

> I am writting a script to do below activities on a numFile having a set
of numbers:
> 1. Addition
> 2. Average
> 3. Max/Min
>
> With Addition code , below error message is coming.
>
> code:
> sum=0
>
> sumNumber() ## to sum the numbers from file
> {
> while read n
> do
> (( sum+=n ))
> done < $1
> }
> while getopts ":samn" optchar
> do
> case $optchar in
> s) shift ## s - SUM of Number
> sumNumber
> echo "Sum = "$sum
> ;;
> ..
> ..
>
> calling: $./getOpts -s numFile
> error: ./getOpts1: line 14: $1: ambiguous redirect
>
>
> let me know:
> In function body can't we access the command line arguments $1,$2..
after we have used "shift" before calling the function

a) see the other answers, but more importantly:
b) don't do that in shell, do it in awk or perl or similar.

In awk you can do all of the above calculations with just:

awk '
{
sum += $0
max = ($0 > max ? $0 : max)
min = ($0 < min ? $0 : min)
}
END {
print "sum =", sum
print "ave =", sum / NR
print "min =", min
print "max =", max
}
' file

Regards,

Ed.

Posted using www.webuse.net

Hermann Peifer

unread,
Oct 13, 2012, 6:12:07 AM10/13/12
to
On 2012-10-10 15:36, Ed Morton wrote:
> In awk you can do all of the above calculations with just:
>
> awk '
> {
> sum += $0
> max = ($0 > max ? $0 : max)
> min = ($0 < min ? $0 : min)
> }
> END {
> print "sum =", sum
> print "ave =", sum / NR
> print "min =", min
> print "max =", max
> }
> ' file
>

As a small side-remark: in case all input values happen to be 0's, min
and max will not be set and the above will print:


sum = 0
ave = 0
min =
max =

Hermann

Ed Morton

unread,
Oct 13, 2012, 9:47:41 AM10/13/12
to
Yeah, you're right and while we're addressing that we may as well fix the END
block to print numbers even if the file is empty:

END {
printf "sum = %d\n", sum
printf "ave = %d\n", (NR ? sum / NR : 0)
printf "min = %d\n", min
printf "max = %d\n", max
}

or if we want "NaN"s printed in that situation:

END {
print "sum =", (NR ? sum + 0 : "NaN")
print "ave =", (NR ? sum / NR : "NaN")
print "min =", (NR ? min + 0 : "NaN")
print "max =", (NR ? min + 0 : "NaN")
}

Note that to force printing a numeric value (0) from a variable that may not
have been populated, you can use either:

printf "%d\n", var

or

print var + 0

I showed both ways above, it's a style choice.

Regards,

Ed.
0 new messages