我想在文本文件中包含以下内容
name=missy email=missy@example.com
是否可以将其读入文本文件并能够调用变量$ name和$ email
或者这样做的最佳方式是什么?
感谢您的所有帮助
答案 0 :(得分:1)
当然,这是第一种想到的方式:
#!bash
# set for loop separator to newline
IFS=$'\n'
# loop through each line in the file
for userline in $(cat email_list.txt); do
# Cut delimiter is a space for the next two lines
# echo the line into cut, grab the first field as the user
user=$(echo "$userline" | cut -d' ' -f1)
# echo the line into cut, grab the second field as the email
email=$(echo "$userline" | cut -d' ' -f2)
# Set the delimiter an =, grab field 2
user=$(echo "$user" | cut -d'=' -f2)
# Set the delimiter an =, grab field 2
email=$(echo "$email" | cut -d'=' -f2)
echo "Username: ${user}, email address: ${email}"
done
email_list.txt:
name=missy email=missy@example.com
name=joe email=joe@smoe.com
输出:
Username: missy, email address: missy@example.com
Username: joe, email address: joe@smoe.com