我怎样才能让我的bash脚本工作? [英] How can I get my bash script to work?

查看:183
本文介绍了我怎样才能让我的bash脚本工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的bash脚本不工作的方式,我希望它:

My bash script doesn't work the way I want it to:

#!/bin/bash

total="0"
count="0"
#FILE="$1" This is the easier way
for FILE in $*
do
    # Start processing all processable files
    while read line
    do
        if [[ "$line" =~ ^Total ]];
        then
            tmp=$(echo $line | cut -d':' -f2)
            count=$(expr $count + 1)
            total=$(expr $total + $tmp)
        fi     
    done < $FILE
done
echo "The Total Is: $total"
echo "$FILE"

有另一种方式来修改这个脚本以便读取参数到 $ 1 而不是 $文件的?我已经使用,而循环试过:

Is there another way to modify this script so that it reads arguments into $1 instead of $FILE? I've tried using a while loop:

while [ $1 != "" ]
  do ....
done

当我实现了code重演也。有没有办法来解决这个问题呢?

Also when I implement that the code repeats itself. Is there a way to fix that as well?

这是我遇到的另一个问题是,当我有多个文件喜* .TXT 它让我重复。为什么?我有一个像 hi1.txt hi1.txt〜文件,但波浪线的文件为0字节,所以我的剧本不该'T是找到任何东西。

Another problem that I'm having is that when I have multiple files hi*.txt it gives me duplicates. Why? I have files like hi1.txt hi1.txt~ but the tilde file is of 0 bytes, so my script shouldn't be finding anything.

我有什么是好的,但可以改进。我AP preciate你的awk建议,但目前它超出了我的水平作为UNIX程序员。

What i have is fine, but could be improved. I appreciate your awk suggestions but its currently beyond my level as a unix programmer.

Strager:我的文本编辑器会自动生成包含nothing..it是0 bytes..But是的,我径自删除它们只是为了确保这些文件。但是,没有我的脚本实际上是在读两遍。我想再次的循环,当它真的不应该。我试图用沉默出口的行动commands..But wasnt成功。

Strager: The files that my text editor generates automatically contain nothing..it is of 0 bytes..But yeah i went ahead and deleted them just to be sure. But no my script is in fact reading everything twice. I suppose its looping again when it really shouldnt. I've tried to silence that action with the exit commands..But wasnt successful.

while [ "$1" != "" ]; do
    # Code here

    # Next argument
    shift
done

这code是pretty甜蜜,但我一次指定所有可能的命​​令。例如:喜[145] .TXT
如果提供一次性读取所有三个文件。
假设用户输入喜* .TXT;
然后我得到我所有的喜文件中读取两次,然后再次添加。

This code is pretty sweet, but I'm specifying all the possible commands at one time. Example: hi[145].txt If supplied would read all three files at once. Suppose the user enters hi*.txt; I then get all my hi files read twice and then added again.

我怎样才能在喜* .TXT规范code,以便它读取我的文件(仅一次)?
我真的认为,这是因为没有$ 1。

How can I code it so that it reads my files (just once) upon specification of hi*.txt? I really think that this is because of not having $1.

推荐答案

它看起来像你正试图从标线加起来的总数总计:在文件中提供。它总是说出你想要做什么是一个好主意 -​​ 以及你如何试图做到这一点(见的如何提问聪明的办法)。

It looks like you are trying to add up the totals from the lines labelled 'Total:' in the files provided. It is always a good idea to state what you're trying to do - as well as how you're trying to do it (see How to Ask Questions the Smart Way).

如果是这样,那么你正在做左右的,我可以看到的一种方式复杂。什么是错的:

If so, then you're doing in about as complicated a way as I can see. What was wrong with:

grep '^Total:' "$@" |
cut -d: -f2 |
awk '{sum += $1}
     END { print sum }'

这不打印的总是等;为什么你在呼应你的版本结尾$ FILE目前尚不清楚。

This doesn't print out "The total is" etc; and it is not clear why you echo $FILE at the end of your version.

您可以在 AWK 的使用Perl或任何其他合适的方案;你可以做整个工作在Perl或Python - 的确,剪切的工作可以由 AWK 来完成:

You can use Perl or any other suitable program in place of awk; you could do the whole job in Perl or Python - indeed, the cut work could be done by awk:

grep "^Total:" "$@" |
awk -F: '{sum += $2}
         END { print sum }'

两者更进一步,整个工作可以用 AWK 来完成:

awk -F: '$1 ~ /^Total/ { sum += $2 }
         END { print sum }' "$@"

Perl中的code不会更难,其结果可能会更快:

The code in Perl wouldn't be much harder and the result might be quicker:

perl -na -F: -e '$sum += $F[1] if m/^Total:/; END { print $sum; }' "$@"

当过在shell脚本,你应该使用所提供的文件名参数迭代$ @代替 $ * ,因为后者的符号并不preserve空格的文件名。

When iterating over the file name arguments provided in a shell script, you should use '"$@"' in place of '$*' as the latter notation does not preserve spaces in file names.

您点评一下 $ 1 '是混乱给我。你可能会问,从他的名字是在 $ 1 在每次迭代文件中读取;正在使用完成的:

Your comment about '$1' is confusing to me. You could be asking to read from the file whose name is in $1 on each iteration; that is done using:

while [ $# -gt 0 ]
do
     ...process $1...
     shift
done

心连心!

这篇关于我怎样才能让我的bash脚本工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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