문제

It will eventually be part of a larger script so it needs to be shell scripted. A simple task in other languages, but I'm having trouble accomplishing it in shell. Basically I have a string and I want to insert a "." at all possible indices within the string. The output can be on newlined or separated by spaces. Can anyone help?

Example:
input: "abcd"

output: ".abcd
a.bcd
ab.cd
abc.d
abcd."

OR

output: ".abcd a.bcd ab.cd abc.d abcd."

도움이 되었습니까?

해결책

A simple for loop would do:

input=abcd
for ((i=0; i<${#input}+1; i++))
do
    echo ${input::$i}.${input:$i}
done

This just slices up the string at each index and inserts a .. You can change the echo to something else like appending to an array if you want to store them instead ouf output them, of course.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top