在 Bash 中从 File 中读取行并将单词解析为 mailx 参数的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6756758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read lines from File in Bash and parse words into variables for mailx parameters
提问by Shon
I have a bash script which reads lines from a text file with 4 columns(no headers). The number of lines can be a maximum of 4 lines or less. The words in each line are separated by SPACE character.
我有一个 bash 脚本,它从一个有 4 列(没有标题)的文本文件中读取行。行数最多可以是 4 行或更少。每行中的单词由空格字符分隔。
[email protected] [email protected];[email protected] Sub1 MailBody1
[email protected] [email protected];[email protected] Sub2 MailBody2
[email protected] [email protected];[email protected] Sub3 MailBody3
[email protected] [email protected];[email protected] Sub4 MailBody4
Currently, I am parsing the file and after getting each line, I am storing each word in every line into a variable and calling mailx four times. Wondering if is there is an elegant awk/sed solution to the below mentioned logic.
目前,我正在解析文件,在获取每一行后,我将每一行中的每个单词存储到一个变量中并调用 mailx 四次。想知道下面提到的逻辑是否有一个优雅的 awk/sed 解决方案。
- find total number of lines
- while
read $line, store each line in a variable - parse each line as
i=( $line1 ),j=( $line2 )etc - get values from each line as
${i[0]},${i[1]},${i[2]}and${i[3]}etc - call
mailx -s ${i[2]} -t ${i[1]} -r ${i[0]} < ${i[3]} - parse next line and call
mailx - do this until no more lines or max 4 lines have been reached
- 找到总行数
- while
read $line,将每一行存储在一个变量中 - 解析每个行
i=( $line1 ),j=( $line2 )等 - 从各行获取值
${i[0]},${i[1]},${i[2]}和${i[3]}等 - 称呼
mailx -s ${i[2]} -t ${i[1]} -r ${i[0]} < ${i[3]} - 解析下一行并调用
mailx - 这样做直到没有更多行或最多 4 行已达到
Do awk or sed provide an elegant solution to the above iterating/looping logic?
awk 或 sed 是否为上述迭代/循环逻辑提供了优雅的解决方案?
回答by John Kugelman
Give this a shot:
试一试:
head -n 4 mail.txt | while read from to subject body; do
mailx -s "$subject" -t "$to" -r "$from" <<< "$body"
done
head -n 4reads up to four lines from your text file.readcan read multiple variables from one line, so we can use named variables for readability.<<<is probably what you want for the redirection, rather than<. Probably.
head -n 4从文本文件中最多读取四行。read可以从一行读取多个变量,因此我们可以使用命名变量来提高可读性。<<<可能是您想要的重定向,而不是<. 大概。
回答by moo
The above while loop works well as a simple alternative to sed and awk if you have a lot of control over how to display the lines of text in a file. the read command can use a specified delimiter as well, using the -d flag.
如果您对如何在文件中显示文本行有很多控制权,则上述 while 循环可以很好地作为 sed 和 awk 的简单替代方案。读取命令也可以使用指定的分隔符,使用 -d 标志。
Another simple example:
另一个简单的例子:
I had used mysql to grab a list of users and hosts, putting it into a file /tmp/userlist with text as shown:
我曾使用 mysql 获取用户和主机列表,将其放入带有文本的文件 /tmp/userlist 中,如下所示:
user1 host1
user2 host2
user3 host3
I passed these variables into a mysql command to get grant info for these users and hosts and append to /tmp/grantlist:
我将这些变量传递给 mysql 命令以获取这些用户和主机的授权信息并附加到 /tmp/grantlist:
cat /tmp/userlist | while read user hostname;
do
echo -e "\n\nGrabbing user $user for host $hostname..."
mysql -u root -h "localhost" -e "SHOW GRANTS FOR '$user'@$hostname" >> /tmp/grantlist
done

