2016-11-30 6 views
0

ユーザー入力を収集して後でテキストファイルに追加するシェルスクリプトを作成しようとしています。 は、ここに私が働いているものです:ファイルに追加する変数へのユーザー入力のコレクション

note_file=~/project_2/file 
loop_test=y 
while test "$loop_test" = "y" 
do 
    clear 
    tput cup 1 4; echo "  Note Sheet Addition  " 
    tput cup 2 4; echo "=============================" 
    tput cup 4 4; echo "Note: " 
    tput cup 5 4; echo "Add Another? (Y)es or (Q)uit: " 
    tput cup 4 10; read note 
if test $note = "q" 
then 
clear; exit 
fi 
if test "$note" != "" 
then 
    echo "$note:" >> $note_file 
fi 
    tput cup 5 33; read hoop_test 
if [ "$hoop_test" = "q" ] 
then 
    clear; exit 
fi 
done 

今私の問題は、私はノート変数だけではなく、単一のarguementに文全体を保存したいということです。 すなわち音符=

明らか hoop loopスペルの問題以外に
+1

私はあなたが何を意味するかわからないんだけど: 'note'フルラインがスペースを含む単一の文字列、すなわち、として入力され続ける読んでいないのですか? – Evert

+0

'read loop_test'の代わりに明白な' read hoop_test'以外のもの –

答えて

0

「これはノートや他のいくつかの一般的なテキストのものである」、と:あなたの全体の問題が原因、各note(つまりは知らん、意図的でもよい)に追加1つ以上の単語を入力するとエラーが発生しますあなたの変数test、たとえば

if test "${note,,}" = "q" ## always quote variables in test 

"..."お知らせ(引用符は、それがエラーを促しtest some words = "q"です:?test: too many arguments - 聞き覚え)が正しく、それは結構ですtest "some words" = "q"で引用されました。

,,とパラメータ展開がちょうど終了するためQ又はqを処理するためにnoteと小文字の内容を変換)

これらの問題以外

(および終了時に明示的yloop_testをリセットするために必要)、スクリプトが正常に動作します:

#!/bin/bash 

note_file=~/project_2/file 
note="" 
loop_test=y 

while test "$loop_test" = "y" 
do 
    clear 
    tput cup 1 4; echo "  Note Sheet Addition  " 
    tput cup 2 4; echo "=============================" 
    tput cup 4 4; echo "Note: " 
    tput cup 5 4; echo "Add Another? (Y)es or (Q)uit: " 
    tput cup 4 10; read note 

    if test "${note,,}" = "q" ## always quote variables in test 
    then 
     clear; exit 
    fi 

    if test "$note" != "" 
    then 
     echo "$note" >> "$note_file" 
    fi 

    tput cup 5 34; read loop_test 

    if [ "${loop_test,,}" = "q" ] 
    then 
     clear; exit 
    else 
     loop_test=y 
    fi 
done 
関連する問題