Domanda

I am developing a script on a Solaris 10 SPARC machine to calculate how many patches got installed successfully during a patch delivery. I would like to display to the user:

(X) of 33 patches were successfully installed

I would like my script to output dynamically replacing the "X" so the user knows there is activity occurring; sort of like a counter. I am able to show counts, but only on a new line. How can I make the brackets update dynamically as the script performs its checks? Don't worry about the "pass/fail" ... I am mainly concerned with making my output update in the bracket.

for x in `cat ${PATCHLIST}`
do
    if ( showrev -p $x | grep $x > /dev/null 2>&1 ); then
        touch /tmp/patchcheck/* | echo "pass" >> /tmp/patchcheck/$x
        wc /tmp/patchcheck/* | tail -1 | awk '{print $1}'
    else
        touch /tmp/patchcheck/* | echo "fail" >> /tmp/patchcheck/$x
        wc /tmp/patchcheck/* | tail -1 | awk '{print $1}'
    fi
done
È stato utile?

Soluzione 2

Here is how I got my patch installation script working the way I wanted:

while read pkgline
do
    patchadd -d ${pkgline} >> /var/log/patch_install.log 2>&1
    # Create audit file for progress indicator
    for x in ${pkgline}
    do
        if ( showrev -p ${x} | grep -i ${x} > /dev/null 2>&1 ); then
            echo "${x}" >> /tmp/pass
        else
            echo "${x}" >> /tmp/fail
        fi
    done
    # Progress indicator
    for y in `wc -l /tmp/pass | awk '{print $1}'`
    do
        printf "\r${y} out of `wc -l /patchdir/master | awk '{print $1}'` packages installed for `hostname`.  Last patch installed: (${pkgline})"
    done
done < /patchdir/master

Altri suggerimenti

The usual way to do that is to emit a \r carriage return (CR) at some point and to omit the \n newline or line feed (LF) at the end of the line. Since you're using awk, you can try:

awk '{printf "\r%s", $1} END {print ""}'

For most lines, it outputs a carriage return and the data in field 1 (without a newline at the end). At the end of the input, it prints an empty string followed by a newline.

One other possibility is that you should place the awk script outside your for loop:

for x in `cat ${PATCHLIST}`
do
    if ( showrev -p $x | grep $x > /dev/null 2>&1 ); then
        touch /tmp/patchcheck/* | echo "pass" >> /tmp/patchcheck/$x
        wc /tmp/patchcheck/* | tail -1
    else
        touch /tmp/patchcheck/* | echo "fail" >> /tmp/patchcheck/$x
        wc /tmp/patchcheck/* | tail -1
    fi
done | awk '{ printf "\r%s", $1} END { print "" }'

I'm not sure but I think you can apply similar streamlining to the rest of the repetitious code in the script:

for x in `cat ${PATCHLIST}`
do
    if showrev -p $x | grep -s $x
    then echo "pass"
    else echo "fail"
    fi >> /tmp/patchcheck/$x
    wc /tmp/patchcheck/* | tail -1
done | awk '{ printf "\r%s", $1} END { print "" }'

This eliminates the touch (which doesn't seem to do much), and especially not when the empty output of touch is piped to echo which ignores its standard input. It eliminates the sub-shell in the if line; it uses the -s option of grep to keep it quiet.

I'm still a bit dubious about the wc line. I think you're looking to count the number of files, in effect, since each file should contain one line (pass or fail), unless you listed some patch twice in the file identified by ${PATCHLIST}. In which case, I'd probably use:

for x in `cat ${PATCHLIST}`
do
    if showrev -p $x | grep -s $x
    then echo "pass"
    else echo "fail"
    fi >> /tmp/patchcheck/$x
    ls /tmp/patchcheck | wc -l
done | awk '{ printf "\r%s", $1} END { print "" }'

This lists the files in /tmp/patchcheck and counts the number of lines output. It means you could simply print $0 in the awk script since $0 and $1 are the same. To the extent efficiency matters (not a lot), this is more efficient because ls only scans a directory, rather than having wc open each file. But it is more particularly a more accurate description of what you are trying to do. If you later want to count the passes, you can use:

for x in `cat ${PATCHLIST}`
do
    if showrev -p $x | grep -s $x
    then echo "pass"
    else echo "fail"
    fi >> /tmp/patchcheck/$x
    grep '^pass$' /tmp/patchcheck/* | wc -l
done | awk '{ printf "\r%s", $1} END { print "" }'

Of course, this goes back to reading each file, but you're getting more refined information out of it now (and that's the penalty for the more refined information).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top