0

the task is to write a shell script inputs are a string and a number

for example,

xxx.sh "Hello World" 3

the input will be

***************
* Hello World *
* Hello World *
* Hello World *
***************

and here is what have I got so far:

function mantra()   {
    echo "string is $1"
    echo "number is $2"

    echo $string
    echo $PATH
    for num in string_length; do
        echo "*"
    done
}

How do I count the number of characters in the string? Am I doing right? I am not exactly sure how to pass command-line arguments into my function.Blockquote

2 Answers 2

1

The number of characters in your input string is ${#1}

See this page for a short explanation.

Sign up to request clarification or add additional context in comments.

Comments

0
#!/bin/sh

function mantra()   {

    string=$1
    num=$2

    strlen=${#string}

    let strlen=$strlen+2


    echo -n "*"
    for (( times = 0; times < $strlen; times++ )); do echo -n "*" ; done

    echo "*";


}

mantra $1 $2

    for (( times = 0; times < $num; times++ )); do
        echo "* $string *"
    done

mantra $1 $2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.