Extracting a part of a string

Hi,

I needed to extract some specific characters from a string based on user input. For example: After the script executes the user enters the following details:

Please enter the string: This is a shell script
Please enter the starting position: 11
Please enter the number of characters to be extracted: 4
The output will be displayed as below
Output: shel

So I wrote a script as below:

!/bin/ksh

echo "Please enter the string: "
read str
echo "Please enter the starting position: "
read start_position
echo "Please enter the number of characters to be extracted: "
read char_num

output=`echo $str | cut -c $start_position-char_num`
echo "Output: "$output

But it is throwing error for incorrect usage of cut command. Can anyone please help me in completing the script?

Thanks in advance,
Chandan

try like this

`echo $str | cut -c $start_position-$((start_position+char_num-1))`

Thanks I got the solution. The code needs to be as below:

!/bin/ksh

echo "Please enter the string: "
read str
echo "Please enter the starting position: "
read start_position
echo "Please enter the number of characters to be extracted: "
read char_num

end_position=$start_position + $char_num

output=`echo $str | cut -c${start_position}-${end_position}`
echo "Output: "$output

Note: a shebang should be written like this:

#!/bin/ksh

And the #! need to be the two very first characters in the script, otherwise it will not work, i.e. the script will not be called with the shell it is intended to be called with.

What version of ksh is it? ksh88 or ksh93. If it is ksh93 you could also try:

output=${str:start_position-1:char_num}

Thanks Scrutinizer. That was a typo.