Ambiguous Redirect error while accessing cmd line arg in a function
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: comp.unix.shell
From:
ram <bhatnagar.bhav... @gmail.com>
Date: Tue, 9 Oct 2012 21:42:52 -0700 (PDT)
Local: Wed, Oct 10 2012 12:42 am
Subject: Ambiguous Redirect error while accessing cmd line arg in a function
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.unix.shell
From:
Kaz Kylheku <k... @kylheku.com>
Date: Wed, 10 Oct 2012 05:01:02 +0000 (UTC)
Local: Wed, Oct 10 2012 1:01 am
Subject: Re: Ambiguous Redirect error while accessing cmd line arg in a function
On 2012-10-10, ram <bhatnagar.bhav
... @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.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.unix.shell
From:
"Chris F.A. Johnson" <cfajohn... @gmail.com>
Date: Wed, 10 Oct 2012 00:58:03 -0400
Local: Wed, Oct 10 2012 12:58 am
Subject: Re: Ambiguous Redirect error while accessing cmd line arg in a function
On 2012-10-10, ram 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
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)
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.unix.shell
From:
"Ed Morton" <mortons... @gmail.com>
Date: Wed, 10 Oct 2012 18:36:34 GMT
Local: Wed, Oct 10 2012 2:36 pm
Subject: Re: Ambiguous Redirect error while accessing cmd line arg in a function
ram <bhatnagar.bhav
... @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
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.unix.shell
From:
Hermann Peifer <pei... @gmx.eu>
Date: Sat, 13 Oct 2012 07:12:07 -0300
Local: Sat, Oct 13 2012 6:12 am
Subject: Re: Ambiguous Redirect error while accessing cmd line arg in a function
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.unix.shell
From:
Ed Morton <mortons... @gmail.com>
Date: Sat, 13 Oct 2012 08:47:41 -0500
Local: Sat, Oct 13 2012 9:47 am
Subject: Re: Ambiguous Redirect error while accessing cmd line arg in a function
On 10/13/2012 5:12 AM, Hermann Peifer wrote:
> 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
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.
You must
Sign in before you can post messages.
You do not have the permission required to post.