2010-11-09 168 views
0

的任务是写一个shell脚本 输入是一个字符串和数字Shell脚本,命令行参数

例如,

xxx.sh的 “Hello World” 3

的投入将是

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

,这里是我有什么至今:

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

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

如何计算字符串中的字符数? 我做对了吗?我不完全知道如何通过命令行参数到我function.Blockquote

回答

0

字符数在你输入字符串为${#1}

一个简短的说明,请参见this页。

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