简体   繁体   English

从文件中读取行到Bash中的变量

[英]Reading lines from a file into variables in Bash

I'm creating a script to automate adding users from a text file in Unix using Bash 我正在创建一个脚本,使用Bash自动从Unix中的文本文件添加用户

I've read a file into a script which is being held in $FILE 我已经将文件读入了一个以$ FILE保存的脚本

The file is in the following format: e-mail;birthdate;groups;sharedFolder 该文件采用以下格式:电子邮件; birthdate; groups; sharedFolder

I want to ignore the first line in the file and read the data into variables 我想忽略文件中的第一行并将数据读入变量

I have already set the IFS to ';' 我已经将IFS设置为';' earlier. 早。 Currently I have 目前我有

sed 1d $FILE | while read EMAIL BIRTH GROUPS SHAREDFOLDER
do
echo "$EMAIL"
done < $FILE

But when I echo out the EMAIL variable, I get nothing 但当我回应EMAIL变量时,我什么也得不到

You are attempting to redirect standard input twice. 您正在尝试将标准输入重定向两次。 The pipeline and the < can't both be connected to while read . 管道和<不能while read连接。 On my Bash the < wins: 在我的Bash上<胜利:

bash$ echo moo | cat <<<bar
bar

Apparently you want simply 显然你想简单

sed 1d "$FILE" | while read -r EMAIL BIRTH GROUPS SHAREDFOLDER
do
    echo "$EMAIL"
done

though this can meaningfully be reduced to just 虽然这可以有意义地减少到公正

sed 1d;s/;.*//' "$FILE"

You also really want to use lower case for your private variables , but that's a separate discussion. 您还真的想为您的私有变量使用小写 ,但这是一个单独的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM