[Solved] How to refer more than 9 command line inputs for a scripts in korn shell?

Hi all,

I have a script which should take more than 9 command line inputs while running. Like

script.sh a s d f g h j j k l o p i u y t r e w

Now in the script if I have to access one of the input which is at position after 9, in this case say 'p' then how can I do that?

echo $12

will not work as 1 & 2 would be taken as two independent numbers. I found a suggestion to mention

echo ${12}

but I guess it is for Bourne shell and for me on Korn shell it it is giving error as: script.sh: bad substitution

Can you please help me on this or direct me to a place when I can get more information.

Thanks,
Pat

That should work on any POSIX-compliant shell, including ksh (unless it's quite old). What's your OS?

It's SunOS 5.8 on UNIX server. Should {} work here :confused:

#!/bin/ksh
i=1
for arg in $*
do
  eval param$i=$arg;
  i=$(($i + 1))
done

echo $param12

${12} worked in the Bourne shell and has been supported by all versions of ksh since day 1.

Please show us your entire script, the command line you're using to invoke your script, and the EXACT diagnostic message you're getting when you run it.

Thank you for your input everyone. I am trying this and the output is also pasted here.

/export/home/pj317q> cat displayname.sh
echo $1
echo $2
echo $9
echo ${15}
echo ${10}
/export/home/pj317q> sh displayname.sh a b c d e f g h I j k l m n o p q r s t u v
a
b
I
displayname.sh: bad substitution
/export/home/pj317q>

Not sure whats going wrong.

Hi,
A solution:

$ /usr/bin/sh
$ set A B C D E F G H I J K L M N O P Q R S
$ echo $#
19
$ echo $19
A9
$ echo ${19}
bad substitution
$ echo $9
I
$ shift 10
$ echo $9
S

Regards.

What does sh resolve to? I doubt it's ksh or bash by default on SunOS.

Try

ksh displayname.sh a b c d e f g h I j k l m n o p q r s t u v

probably not a POSIX shell as the standard describes this situation and tells to use braces.

Note that you're using sh; not ksh to run the script (as marked in red above). But the old Bourne shell on Solaris 8 should also be happy with ${15} and ${10}. What is the output from the command:

type sh

For information, from my "solution" with shift command, it was on solaris 10 (/usr/bin/sh).

It is possible that a shopt or set -o option is missing ?

Regards.

Note that neither Solaris 10 /usr/bin/sh nor Solaris 8 /bin/sh or /usr/bin/sh (I thought it was /bin/sh on a Solaris 8 system, but it has been many years since I had access to a Solaris 8 system) is a Korn shell; they are Bourne shells. But both Bourne and Korn shells should be happy with ${12} when there are that many command line arguments no matter what shell options are enabled.

Of course you can use shift 10 to access positional parameters 11 through 19 as $1 through $9, respectively, in a Bourne or Korn shell. But after the shift, the original positional parameters $1 through ${10} (if it worked) will no longer be available unless they were saved into other variables before the shift 10 . If ${10} isn't working and you need to access the 10th positional parameter, presumably you'd want to use shift 9 rather than shift 10 so you could process groups of 9 positional parameters at a time. Even after a shift, $0 should remain unchanged (but if ${11} doesn't work, I won't make any claims about how $0 will behave after any invocation of shift ).

Have you or pat_pramod tried running any of these scripts with /usr/xpg4/bin/sh or with ksh instead of using /usr/bin/sh or /bin/sh?

In Solaris, /usr/bin/sh is not a POSIX shell, it is some sort of Sun homebrew shell based on the bourne shell. If you want a POSIX shell, use /usr/xpg4/bin/sh or the /usr/bin/ksh as Don Cragun already suggested.

$ /usr/bin/sh -c 'set a b c d e f g h i j k l m n o; echo ${12}'
/usr/bin/sh: bad substitution
$ /usr/xpg4/bin/sh -c 'set a b c d e f g h i j k l m n o; echo ${12}'
l
$ /usr/bin/ksh -c 'set a b c d e f g h i j k l m n o; echo ${12}'
l

The result is the same on Solaris 8 and 10.

BTW: In Solaris, /bin is a symbolic link to /usr/bin/ , so /bin/sh and /usr/bin/sh are identical.

1 Like

It's ok for /usr/xpg4/bin/sh , ${20} for exemple work fine.
I check my /usr/bin/sh and /bin/sh , and they are a link to /sbin/sh

Regards.

/sbin/sh (and it's iinks /bin/sh and /usr/bin/sh ) isn't a homebrew; it came straight out of UNIX System V Release 4 from AT&T.

/usr/bin/ksh isn't a POSIX conforming shell either (although it is closer than /sbin/sh . /usr/bin/ksh is based on ksh88 source and is missing some ksh93 features required by the POSIX standards.

Thanks for that reminder.

1 Like

Thank you everyone (Specially Don and CarloM), it worked with ksh instead of sh. Also sorry for being late to respond.
Here are some of the details I could find.....

/export/home/pj317q> type sh
sh is a tracked alias for /usr/bin/sh
/export/home/pj317q> uname -s
SunOS
/export/home/pj317q> uname -r
5.8
/export/home/pj317q> echo $SHELL
/bin/ksh

and finally the script is good and I can make use of this in my code.
Here is the output now:

/export/home/pj317q> cat displayname.sh
#!/bin/ksh
echo $1
echo $2
echo $9
echo ${15}
echo ${10}
/export/home/pj317q>
/export/home/pj317q> ksh displayname.sh a b c d e f g h I j k l m n o p q r s t u v
a
b
I
o
j
/export/home/pj317q>

Also please let me know if you want me to check anything more on the this system which might help you in some way.