Jimmy C
Jimmy C

Reputation: 1

Shell Script, commandline arguments

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

Upvotes: 0

Views: 3161

Answers (2)

Brad
Brad

Reputation: 11505

#!/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

Upvotes: 0

Reese Moore
Reese Moore

Reputation: 11640

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

See this page for a short explanation.

Upvotes: 1

Related Questions