多分初めてbashに触ってみたのですが戻り値がないのが違和感あってなかなかロジック組み立てられずにいます。
やりたいことは未入力があれば入力があるまでreadさせ続けるというものなのですが、、、
function check_blank () { if [ "$1" = "" ]; then echo "It is not yet input." # 入力させる ## 問題は入力させる変数名。メソッドを個別に分けるのはいやだ。 read Clone_url # 入力されていなかったらcheck_blankを呼び出す。 if [ "$Clone_url" = "" ]; then check_blank fi fi } ## Input setting echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter the URL of the remote repository of the clone to be created.\e[0;39m" read Clone_url check_blank $Clone_url return echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter an alias for managing the clone to be created.\e[0;39m" read Project_name check_blank $Project_name echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter the domain name to reflect the setting.\e[0;39m" read Domain_name check_blank $Domain_name
check_blank1
check_blank2
check_blank3
と作れば解説しますが、それは個人的に嫌で、引数を利用して共通メソッドを作れればと思っているのですがロジックが思い浮かばず、、、
## 問題は入力させる変数名。メソッドを個別に分けるのはいやだ。 read Clone_url
の部分なのですが共通メソッドを作りたいので変数名が可変しなくてはならないはずです。
function check_blank () { if [ "$2" = "" ]; then echo "It is not yet input." # 入力させる read $1 # 入力されていなかったらcheck_blankを呼び出す。 if [ "${$1}" = "" ]; then check_blank "$1" fi fi } check_blank "Clone_url" $Clone_url
みたいなことをやってもうまくいかず、、、
知恵をお貸しいただきたく、、、
本題から離れていってる気がするので、、、教えていただいた記述を例にメソッドを切り分けたいのですが、入力時も未入力時も動作がおかしいのです、、、
function read_input () { read input rtn_check_blank=$(check_blank) $input # 関数の返却値を標準出力に出力 echo $rtn_check_blank } function check_blank () { input=$1 while [ "$input" = "" ]; do echo "It is not yet input." read input done echo $input } ## Input setting echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter the URL of the remote repository of the clone to be created.\e[0;39m" Clone_url=$(read_input) echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter an alias for managing the clone to be created.\e[0;39m" Project_name=$(read_input) echo -e "\e[5;36m* \e[0m\e[1;36mPlease enter the domain name to reflect the setting.\e[0;39m" Domain_name=$(read_input) echo $Clone_url,$Project_name,$Domain_name
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/29 02:30
2018/01/29 05:20
2018/01/29 13:08
2018/01/30 00:44
2018/01/30 02:16