您好我想创建一个将联系人插入contacts.csv
文件的脚本。它要求用户键入名字,姓氏和电话号码,然后输出将保存在名为John,Dawson,2102983187
的文件中。
例如:#!/bin/bash
IFS=','
touch contacts.csv
echo "What's your contact's first name?"
read fname
echo "$fname" | grep '^[[:upper:]]\+[a-z]\{0,\}' >> contacts.csv
echo "What's your contact's last name?"
read lname
echo "$lname" | grep '^[[:upper:]]\+[a-z]\{0,\}' >> contacts.csv
echo "Whats your contact's phone number"
read phone
echo "$phone" | grep '[0-9]\{10\}$' >> contacts.csv
试过这个,但是想要在示例中用逗号分隔输出,并且它必须处于循环中。
java -Duser.timezone=Europe/Sofia com.acme.Main
答案 0 :(得分:0)
#!/bin/ksh
read fname?"What's your contact's first name? "
read lname?"What's your contact's last name? "
read phone?"What's your contact's phone number? "
[[ $fname =~ ^[[:upper:]][a-z]+$ ]] || { echo "$fname should be uppercase first char followed by lowercase chars" ; exit 1; }
[[ $lname =~ ^[[:upper:]][a-z]+$ ]] || { echo "$fname should be uppercase first char followed by lowercase chars" ; exit 2; }
[[ $phone =~ ^[[:digit:]]+$ ]] || { echo "$fname should be digits only" ; exit 3; }
echo "$fname,$lname,$phone" >> contacts.csv
更改IFS是不必要的。 IFS = 输入分隔符,我认为您的输入中不需要,
。
Read可以提供提示,不需要使用echo。
我先检查所有输入,然后写入输出。
如果你把块放在
之间while true; do
...
done
空输入将停止处理