2014-01-10 23 views
0

我使用的bash腳本內的代碼來構建在節的輸出:

box_full() { 
     printf '*%.0s' $(seq 1 67) 
     echo "" 
    } 
    box_content() { 
    echo -n "*" 
    if [ $# -eq 0 ] 
    then 
     printf ' %.0s' $(seq 1 65) 
    elif [ $# -eq 1 ] 
    then 
     RIGHT="$1" 
     printf ' %.0s' $(seq 1 17) 
     echo -n "${RIGHT}" 
     printf ' %.0s' $(seq 1 $[48-${#RIGHT}]) 
    elif [ $# -eq 2 ] 
    then 
     LEFT="$1" 
     RIGHT="$2" 
     printf ' %.0s' $(seq 1 3) 
     echo -n "${LEFT}" 
     printf ' %.0s' $(seq 1 $[14-${#LEFT}]) 
     echo -n "${RIGHT}" 
     printf ' %.0s' $(seq 1 $[48-${#RIGHT}]) 
    fi 
    echo "*" 
}` 

的使用是:

box_full 
box_content "Download-Process..." 
box_full` 

有沒有辦法用Ruby腳本來做到這一點?

編輯:我第一個走過來解決:

module Style 
    def self.box_full 
    puts '*' * 67 
    end 

    def self.box_content(content) 
    box_rest = 66 - content.length 
    box_rest.to_i 
    print '* ' + box_rest * '' + '*' 
    end 
end 

所以整箱的作品好。

對於第二個我想給方法框內容的字符串稱爲內容。其實它不起作用。所希望輸出爲:

* content * 

最後*應該對地方67 行的末尾來設置是否有可能解決在Ruby中?

+0

'$ [arith_expr ]'沒有記錄。使用'$((...))'代替 –

回答

0

你也許可以做到這一點在你的Ruby腳本製作一個系統調用:

system("printf '*%.0s' $(seq 1 67)") 

如果你需要引用變量,如輸入參數:

system("echo #{ARGV[0]}") 
+0

當然。那也是我的想法。我認爲還有更像Ruby的解決方案。 –

+0

實際查看這個帖子:http://stackoverflow.com/questions/2232/calling-bash-commands-from-ruby –