开发者

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


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

See this page for a short explanation.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜