简体   繁体   中英

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

I've read a file into a script which is being held in $FILE

The file is in the following format: e-mail;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 ';' 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

You are attempting to redirect standard input twice. The pipeline and the < can't both be connected to while read . On my Bash the < wins:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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