Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get my bash script to work?
    text
    copied!<p>My bash script doesn't work the way I want it to:</p> <pre><code>#!/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 &lt; $FILE done echo "The Total Is: $total" echo "$FILE" </code></pre> <p>Is there another way to modify this script so that it reads arguments into <code>$1</code> instead of <code>$FILE</code>? I've tried using a <code>while</code> loop:</p> <pre><code>while [ $1 != "" ] do .... done </code></pre> <p>Also when I implement that the code repeats itself. Is there a way to fix that as well?</p> <p>Another problem that I'm having is that when I have multiple files <code>hi*.txt</code> it gives me duplicates. Why? I have files like <code>hi1.txt</code> <code>hi1.txt~</code> but the tilde file is of 0 bytes, so my script shouldn't be finding anything.</p> <hr> <p>What i have is fine, but could be improved. I appreciate your awk suggestions but its currently beyond my level as a unix programmer. </p> <p>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.</p> <pre><code>while [ "$1" != "" ]; do # Code here # Next argument shift done </code></pre> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload