Operating System - HP-UX
1826302 Members
4117 Online
109692 Solutions
New Discussion

Re: Positional characters

 
SOLVED
Go to solution
Sanjay Tailor
Frequent Advisor

Positional characters

Hello,
Is there a limit to how many positional characters I can have for a script? Where can I find that out? Can I increase it? I need to run a script that would have about 13000 positional characters!!!

Thanks,
Sanjay
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: Positional characters

Hi Sanjay,

I can't tell exactly what you are trying to do.
If you are asking can the shell handle lines of 13000 character, then yes. Cut can also process lines that long. Awk cannot (I think the limit is about 3000 chars/lines depending upon implementation. If I were processing input records of that size, I think I would use
'gawk' from http://hpux.cs.utah.edu. Gnu's version of awk has options for specifying maximum input record size.

Clay
If it ain't broke, I can fix that.
Sanjay Tailor
Frequent Advisor

Re: Positional characters

Clay,

Thanks for your reply. What I need to do is run a script like this:
scriptname 1 2 3 4 5 6 7 8 9 ... 13000 ...

When I try to run this I get an error message saying parameter list too long.

Unless there is a better way to do this? My positional characters are stored in a file. Is there a way to have my script read one line in the file, process and then loop back to the second line, process and so on until the EOF? That would or might work better?

Right now I am doing scriptname `cat filename` and I am getting the error.

Thanks,
Sanjay
MANOJ SRIVASTAVA
Honored Contributor

Re: Positional characters

Hi Sanjay

I think what you are looking at is handling 13000 characters in aline , then your answer is yes. Please go ahead and use it.

Manoj Srivastava
Mladen Despic
Honored Contributor
Solution

Re: Positional characters

Sanjay,

Try this:

cat filename | while read -r line
do

done

HTH
James R. Ferguson
Acclaimed Contributor

Re: Positional characters

Hi Sanjay:

# getconf ARG_MAX

will return the maximum number of characters allowed in a argument list. Usually on 10.20 this is aboout 20,478. On 11.x this limit is 2,048,000. If you are are 10.x you can change the kernel parameter 'large_ncargs_enabled' to true to enable a similar limit as seen on 11.x. To do so requires patch PHKL_10177 on 800-series servers.

...JRF...

Sanjay Tailor
Frequent Advisor

Re: Positional characters

Hello,

The cat filename | while read -r line

seems to have worked. I am testing it now.

The ARG_MAX was also 20478 but I don't think I want to reconfigure my kernel for that!!! Good to know for the future.

Thanks,
Sanjay
A. Clay Stephenson
Acclaimed Contributor

Re: Positional characters

Hi Sanjay,

Ok I see the problem. First, be aware that aside from arg list too long errors, you can only directly address $1 through $9. You can then do a shift 9 and address the next 9 positional vars as $1 - $9.

You can make the shell read one line at a time. There are two basic techniques:

F=/tmp/myfile
cat ${F} | while read X
do
echo "${X}"
done

Sometimes this doesn't work well because you are executing with a pipe. Method 2 uses file descriptor reads and occurs in the foreground.
It's a little obscure but it's the weapon of choice.

F=/tmp/myfile
exec 9<$F
#avoid fdes 0,1,2 stdin,stdout,stderr - my
#convention is 9
X=`line <&9`
STAT=$?
while [ ${STAT} -eq 0 ]
do
echo "${X}"
X=`line <&9`
STAT=$?
done

Hope this helps, Clay
If it ain't broke, I can fix that.
Carlos Fernandez Riera
Honored Contributor

Re: Positional characters


I cant understand why that abuse of piped cat:

cat file | while read line
do
xx
done

is similar to

while read line
do
x
done < file

but this syntax not use 2 process (with 3 standard open file) and 1 pipe to do exactly the same.

About of 13000 parameters... and cat:

think that sh must create a new process for `cat file` first, store in memory all data and then evaluate all parameters. It need lots of resources that can be avoided.


unsupported
Mladen Despic
Honored Contributor

Re: Positional characters

Sanjay,

If you ever need to reference positional parameters beyond $9, I think you can use {},
e.g, ${10} , ${11} , etc.

Another useful command is 'shift' as Clay has mentioned.

Enjoy the scripting. Regards,

Mladen