将信息存储为单个记录

时间:2015-03-26 14:39:06

标签: bash

我正在尝试将输入到表格中的所有数据保存为contacts.txt文件中的单个记录,如此

Record: 12
    Bob
     Roberts
     Bobs Stuff
     Bobby
     Bos Road
     Bobsville
     BB0 B22
     01234123456

目前我的代码将每个字段保存为单个记录,如此

Record: 5
        Bob







==========================

Record: 6
       Roberts

等等。我如何解决这个问题?

这是我的代码:

#!/bin/bash

BOOK="contacts.txt"

# set field names i.e. shell variables
forename=""
surname=""
company=""
position=""
street=""
town=""
postcode=""
phone=""


# open fd
exec 3>&1

# Store data to $VALUES variable
VALUES=$(dialog --ok-label "Submit" \
      --backtitle "Contacts" \
      --title "Add New Contact" \
      --form "Create a new contact" \
15 50 0 \
    "Forename:"   1 1   "$forename"     1 10 10 0 \
    "Surname:"    2 1   "$surname"      2 10 15 0 \
    "Company:"    3 1   "$company"      3 10 45 0 \
    "Position:"   4 1   "$position"     4 10 40 0 \
    "Street:"     5 1   "$street"       5 10 50 0 \
    "Town:"       6 1   "$town"         6 10 20 0 \
    "Postcode:"   7 1   "$postcode"     7 10 8 0 \
    "Phone:"      8 1   "$phone"        8 10 11 0 \
2>&1 1>&3)





# close fd
exec 3>&-

  # Echo the answers and ask for confirmation
        echo "Should I enter the values:"
        echo -e " $VALUES";
        echo -n "y/n: "
        read answer

        # Convert the answer to lower case
        fixedanswer=`echo $answer | tr "A-Z" "a-z"`;

        if [ "$fixedanswer" = "y" ] 
        then
            # Write the values to the address book
            echo "$VALUES" >>$BOOK
            echo "Added the entry OK"
            sleep 5
        else
            # Give the user a message
            echo -e " $VALUES \n NOT written to $BOOK"
            sleep 5
        fi

        exit 0

1 个答案:

答案 0 :(得分:0)

在第

echo" $ VALUES" >> $ BOOK

删除$ VALUES周围的报价。

添加引号会将每个值列为自己的文本行。删除引号会将其视为一行,使用它们之间的空格打印每个值。