While循环中,如何从文本文件的第二行读取 [英] While loop, how to read from second line of text file
问题描述
过去2个小时我一直在尝试一切以使其正常运行,但是我在shell和编程方面的经验有限.
i've tried everything for the past 2 hours to get this working but my experience in shell and programming is limited.
我有一个循环
while IFS="," read var1 var2 var3 var4 var5 ; do
statements here...
done < $file
现在,操作inside语句是从一个文本文件中读取内容,该文本文件包含5个字段,您可以看到它们,然后使用它们在Linux中使用bash创建帐户
Now the operation inside statement is to read from a text file that has 5 fields as you can see and then use them to create accounts in Linux using bash
useradd $var1 -p var2 -g var3
等
我想让脚本仅从第二行开始读取,我无法使其正常工作.像while ifs =一样,从第二行读取var 1 var 2 var3等.
I want to make the script to start reading from the second line only, i cant get it working. something like while ifs=, read (from second line) var 1 var 2 var3 etc.
这样做的原因是该文件是从excel到csv的导出数据库,因此第一行将包含标题,例如登记的名字dob等,并且不需要
the reason for this is that the file is an exported database from excel to csv so the first line would contain titles like first name dob enrolled etc etc and is not needed
感谢您的帮助
添加:
在创建用户之前,我添加了一条if语句,如果用户符合条件,则将创建它们.
Before the users are being created, I have added an if statement and if users meet a condition then they will be created.
if [ "$var5" == "fullyenrolled" ]; then
continue with account creation...
echo "$var1 successfuly created"
else
echo "Sorry user $var1 is not fully enrolled"
fi
回声的输出类似于
Full name is not fully enrolled
user1 successfuly created
user2 successfuly created
即使我添加sed 1d |而IFS =等时,似乎仍在读取第一行,这就是为什么即时通讯获得第一个输出(例如全名未完全注册")的原因
even if i add sed 1d | while IFS= etc etc. it seems that it is still reading the first line this is why im getting the first output like "full name is not fully enrolled"
推荐答案
使用sed从输入传递到while循环的输入中删除"第一行
Use sed to "delete" the first line from the input passed to the while loop
sed 1d $file | while ...
do
your statements here
done
这篇关于While循环中,如何从文本文件的第二行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!