简体   繁体   中英

ksh - Add command line arguments to array

I have a script that takes in three command line arguments: 1) an infile, 2) an outfile, and 3) a single number to be added to the infile.

I would like to modify this script such that it can take any number of arguments similar to argument (3) above, and create an individual outfile based on each. I believe the best way to achieve this (that I know of) is to assign all arguments after argument (2) to an array, and then use a for loop to iterate through the array.

It seems easy enough to create an array using the following code:

set -A arrayName "$@"

My question is, how exactly would I assign only arguments 3 onward to an array while allowing for any number of arguments greater than two?

forgive me if this is too simple a solution. But no need to create an array, just look through the numbers to append:

echo 'start'
echo 'usage: my_prog.ksh infile outfile numtoappend [...]'
echo '$*'
echo $*

echo 'infile: ' $1
infile=$1
shift
echo 'outfile: ' $1
outfile=$1
shift
cat $infile > $outfile

echo 'starting loop: '
until [[ $# -eq 0 ]];do
   echo '$numbers to append'
   echo $1
   echo $1 >> $outfile
   shift
done
echo 'end'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM