【问题标题】:csh issue retrieving input from multiple linescsh 问题从多行检索输入
【发布时间】:2013-12-09 19:24:11
【问题描述】:

以下脚本正在只有 csh 的 HP-UX 机器上运行。

#!/bin/csh -fx
set todaysDate = `date +%m%d%y-%H:%M` 
echo -n Paste the email information here: 
set input = $<
set info = `echo $input > jcc.$todaysDate`
sed 's/.*\(PL[0-9]*\).*/\1/' jcc.$todaysDate > sample2
echo $info
echo -n Type y if the output is correct or n if it is incorrect
set answer = $<
if($answer == y || $answer == Y) then
#send to other script
else if($Answer == n || $Answer == N) then
exit
else
echo "Invalid input"
endif

目前没有检索到所有需要的值。

这是输入的信息:

Sample
1.-PL000000002002124215                               12                     DAY 3/11/2013
2.-PL000000002002365287                              67                     DAY 22/11/2013
3  PL000000002002745214                               35                     DAY 27/11/2013

根据电子邮件中的信息,第一行是唯一存储到变量中的内容。

预期输出是:

PL000000002002124215
PL000000002002365287
PL000000002002745214

感谢@keith-thompson 和@shellter 的回复!

这是基于您的信息和我所做的更改的输出:

set todaysDate = `date +%m%d%y-%H:%M`
date +%m%d%y-%H:%M
echo -n Paste the email information here:
Paste the email information here:Sample
1.-PL000000002002124215                               12                     DAY 3/11/2013
set input = Sample
2.-PL000000002002365287                              67                     DAY  22/11/2013
echo Sample
3  PL000000002002745214                               35                     DAY  27/11/2013sed s/.*\(PL[0-9]*\).*/\1/ jcc.120913-15:10
echo Sample
Sample
echo -n Type y if the output is correct or n if it is incorrect
Type y if the output is correct or n if it is incorrectset answer = 1.- PL000000002002124215                               12                     DAY 3/11/2013
if ( 1.-PL000000002002124215 12 DAY 3/11/2013 == y || 1.-PL000000002002124215 12 DAY   3/11/2013 == Y ) then
if: Badly formed number.
homes/ 32% 2.-PL000000002002365287                               67                     DAY 22/11/2013
2.-PL000000002002365287: Command not found.
homes/ 33% 3  PL000000002002745214                                35                     DAY 27/11/2013

【问题讨论】:

  • "只有 csh"?如果它没有至少/bin/sh,我会感到惊讶,Bourne shell,通常被认为更适合脚本编写。
  • 请考虑编辑您的问题以包含您当前的输出。从您的主题行中,我猜您只会回显输入的第一行。是的?通过set input = $&lt; 分配数据,对于多行输入可能根本不起作用,或者几乎肯定需要您在每行输入的末尾使用 `\` 字符转义换行符。祝你好运!

标签: unix csh hp-ux


【解决方案1】:
set info = `echo $input > jcc.$todaysDate`

这会将$info 设置为命令echo $input &gt; jcc.$todaysDate 的输出。由于它的输出被重定向到一个文件,所以没有剩余的输出存储在$info

无论如何都不需要捕获该echo 命令的输出,因为您已经在$input 中拥有它。

我没有分析你的整个脚本,但对于初学者,我将上面的内容替换为:

echo $input > jcc.$todaysDate`

并将对$info 的所有引用替换为$input

我强烈建议缩进你的代码;它使它更易于阅读和维护。

最后,您应该阅读Csh Programming Considered Harmful。你说你的系统只有 csh,但我敢肯定它安装了某些版本的 Bourne shell 为/bin/sh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多