Shell脚本中的循环 [英] Loops in Shell Scripting

查看:73
本文介绍了Shell脚本中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关此shell脚本的帮助.

I need help with this shell script.

  1. 必须使用某种循环.
  2. 必须完全按照所示使用输入数据.
  3. 输出重定向应该在脚本内完成,而不是在命令行上完成.

这是我输入的文件: http://pastebin.com/m3f783597

这是输出需要的内容: http://pastebin.com/m2c53b25a

Here's what the output needs to be: http://pastebin.com/m2c53b25a

这是我失败的尝试: http://pastebin.com/m2c60b41

然后失败的尝试输出: http://pastebin.com/m3460e78c

And that failed attempt's output: http://pastebin.com/m3460e78c

推荐答案

这是帮助.在查看以下我的解决方案之前,请尝试尽可能多地遵循这些规则.从长远来看,这将为您带来更多帮助,而从短期来看,可以肯定的是,您的教育工作者可以尽可能轻松地看到这一点.

Here's the help. Try to follow these as much as possible before looking at my solution below. That will help you out more in the long run, and in the short runsince it's a certainty that your educator can see this as easily as you can.

如果他发现您抄袭了代码,则可能意味着立即失败.

If he finds you've plagiarized code, it will probably mean an instant fail.

您所说的失败尝试"在这里.第一次尝试实际上还不错.

Your "failed attempt" as you put it is here. It's actually not too bad for a first attempt.

echo -e "Name\t\t On-Call\t\t Phone"
for daycount in 2 1 4 5 7 6 3
do
    for namecount in 3 2 6 1 7 4 5
    do
        day=`head -n $daycount p2input2|tail -n 1|cut -f 2 -d " "`
        name=`head -n $namecount p2input1|tail -n 1|cut -f 1 -d " "`
        phone=`head -n $namecount p2input1|tail -n 1|cut -f 2 -d " "`
        echo -e "$name\c"
        echo -e "\t\t$day\c"
        echo -e "\t\t$phone"
        continue
    done
done

这是提示:

  • 您有两个循环,一个循环在另一个循环中,每个循环出现7次.这意味着要输出49行而不是7行.您想每天处理并查找当天的姓名和电话(实际上是当天的姓名,然后是该姓名的电话).
  • 这不是真正适合的硬编码行号(尽管我承认这是偷偷摸摸的)-如果数据顺序发生变化怎么办?更好地搜索值.
  • 制表符使事情变得混乱,请改用空格,因为这样输出就不再依赖终端设置,并且您不必担心制表符未对齐.

为完整起见,这是两个输入文件和预期的输出:

And, for completeness, here's the two input files and the expected output:

p2input1                  p2input2
========                  ========
Dave 734.838.9801         Bob Tuesday
Bob 313.123.4567          Carol Monday
Carol 248.344.5576        Ted Sunday
Mary 313.449.1390         Alice Wednesday
Ted 248.496.2204          Dave Thursday
Alice 616.556.4458        Mary Saturday
Frank 634.296.3357        Frank Friday

Expected output
===============
Name            On-Call         Phone

carol           monday          248.344.5576
bob             tuesday         313.123.4567
alice           wednesday       616.556.4458
dave            thursday        734.838.9801
frank           friday          634.296.3357
mary            saturday        313.449.1390
ted             sunday          248.496.2204

说了这么多,并假设您已经离开了至少两个小时才能尝试运行您的版本,这是我的:

Having said all that, and assuming you've gone away for at least two hours to try and get your version running, here's mine:

 1 #!/bin/bash
 2 spc20="                    "
 3 echo "Name            On-Call         Phone"
 4 echo
 5 for day in monday tuesday wednesday thursday friday saturday sunday
 6 do
 7     name=`grep -i " ${day}$" p2input2 | awk '{print $1}'`
 8     name=`echo ${name} | tr '[A-Z]' '[a-z]'`
 9     bigname=`echo "${name}${spc20}" | cut -c1-15`
10
11     bigday=`echo "${day}${spc20}" | cut -c1-15`
12
13     phone=`grep -i "^${name} " p2input1 | awk '{print $2}'`
14
15     echo "${bigname} ${bigday} ${phone}"
16 done

以下说明应有帮助:

  • 第1行选择正确的外壳,并非总是必要的.
  • 第2行为我们提供了足够的空间来简化格式设置.
  • 第3-4行为我们提供标题和空白行.
  • 每天进行5-6个周期的循环,一次一次.
  • 第7行为我们指定了当天的名称.'grep -i"$ {day} $"'在awk中的 pinput2 行的末尾搜索给定的日期(无论大小写)语句为您提供字段1(名称).
  • 第8行只是将名称全部变为小写.
  • 第9行添加50个空格,然后除15以外的所有空格,以创建正确大小的字符串以进行输出.
  • 第11行在当天也是如此.
  • 第13行与第7行非常相似,不同之处在于它搜索 pinput1 ,在该行的开始中查找名称,并将电话号码返回为第二个字段.
  • 第15行仅输出单个项目.
  • 第16行结束循环.
  • Line 1elects the right shell, not always necessary.
  • Line 2 gives us enough spaces to make formatting easier.
  • Lines 3-4 give us the title and blank line.
  • Lines 5-6 cycles through the days, one at a time.
  • Line 7 gives us a name for the day. 'grep -i " ${day}$"' searches for the given day (regardless of upper or lower case) at the end of a line in pinput2 while the awk statement gives you field 1 (the name).
  • Line 8 simply makes the name all lowercase.
  • Line 9 creates a string of the right size for output by adding 50 spaces then cutting off all at the end except for 15.
  • Line 11 does the same for the day.
  • Line 13 is very similar to line 7 except it searches pinput1, looks for the name at the start of the line and returns the phone number as the second field.
  • Line 15 just outputs the individual items.
  • Line 16 ends the loop.

因此,您已经掌握了足够的提示来(希望)修复您自己的代码,并提供了有关专业人员的示例:-).

So there you have it, enough hints to (hopefully) fix up your own code, and a sample as to how a professional would do it :-).

明智地阅读所使用的工具 grep tr cut awk

It would be wise to read up on the tools used, grep, tr, cut and awk.

这篇关于Shell脚本中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆