Running a script with multiple variables like 25 variables.

Hi All,

i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way??

#!/bin/ksh
##script is sample.ksh

age=$1
gender=$2
class=$3
.
.
.
.
.
.
.
title=$25

so while running scripts i have to give like below.


./sample.ksh a13 gM c4 .... tMs 

now problem is that position of these arguments can change while passing to script

You can't change the position of positional parameters without changing their "meaning". If you call your script like your second proposal, try

for i in "$@"; do case $i in a*) age=${i:1};; g*) gender=${i:1};; ... etc ... esac; done 

You also need to be away that referring to positional parameter $25 will usually end up being interpreted as ${2}5 , i.e. the value of parameter two, followed by the literal value 5.

Someone in their wisdom here managed to code scripts that look like this:-

date_req=$22013

The idea being that the month is passed in as positional parameter 2 and the year is appended. Apart from looking awful, of course we then ended up editing the script each year - oh, and the year was not necessarily the calender year that the process was running in either.

It works for us (very badly and will be replaced soon) but you might get very confused by the use of:-

title=$25
echo $title

.... giving you the output gM5

Robin
Liverpool/Blackburn
UK

The standards state that positional parameters with more than a single decimal digit must be specified with braces. I.e., ${25} will expand to the value of the 25th positional parameter and $25 will expand to the value of the 2nd positional parameter followed by the string 5 .

Rather than writing code that uses $25 to append "5" to the expansion of $2 , I write it as ${2}5 to make it less likely to confuse people who read my code; but experienced shell programmers should know how this works.