Progress bar using lsof and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Progress bar using lsof and awk
# 1  
Old 11-18-2009
Progress bar using lsof and awk

Hey all. I added this to my script, hoping for a progress bar or percentage complete
Code:
lsof -o0 -o -p $$ |
awk '
            BEGIN { CONVFMT = "%.2f" }
            $4 ~ /^[0-9]+r$/ && $7 ~ /^0t/ {
                    offset = substr($7, 3)
                    fname = $9
                    "stat -f %z '\''" fname "'\''" | getline
                    len = $0
                    print fname, offset / len * 100 "%"
            }
    '

Code:
unfortunately,  this is what i see in my terminal:
Please wait while the task completes
/bin/sh: Syntax error: Unterminated quoted string
pipe nan%
/home/bj/Desktop/Final_JCATS_userin2.sh inf%

can anybody out there see what i'm doing wrong? I'm pretty sure that the error is in the awk portion of the script, considering it wasn't there prior to the addition of it. Thanks!
# 2  
Old 11-18-2009
Try :
Code:
lsof -o0 -o -p $$ |
awk '
            BEGIN { CONVFMT = "%.2f" }
            $4 ~ /^[0-9]+r$/ && $7 ~ /^0t/ {
                    offset = substr($7, 3)
                    fname = $9
                    "stat -f %z " Q fname Q | getline
                    len = $0
                    print fname, offset / len * 100 "%"
            } 
    ' Q="'"

Jean-Pierre.

Last edited by aigles; 11-18-2009 at 08:49 AM.. Reason: Variable definition of Q misplaced
# 3  
Old 11-18-2009
That was by far the quickest reply I have every gotten on a forum. I really appreciate it aigles!!!

unfortunately, now i'm getting this error. I apologise if it's something simple. I'm still new to scripting and trying to feel my way through.
Code:
./Final_JCATS_userin2.sh: line 110: unexpected EOF while looking for matching `"'
./Final_JCATS_userin2.sh: line 114: syntax error: unexpected end of file

# 4  
Old 11-18-2009
Sorry, the definition of the variable Q is misplaced.
The correction have been made in my previous post.

Jean-Pierre.
# 5  
Old 11-18-2009
Well Sir, I'm back to the Unterminated quoted string...

Code:
/bin/sh: Syntax error: Unterminated quoted string
pipe nan%
/home/bj/Desktop/Final_JCATS_userin2.sh inf%

# 6  
Old 11-18-2009
The problem is not in the portion of script of the post.
Code:
$ cat wrk.sh
lsof -o0 -o -p $$ |
awk '
            BEGIN { CONVFMT = "%.2f" }
            $4 ~ /^[0-9]+r$/ && $7 ~ /^0t/ {
                    offset = substr($7, 3)
                    fname = $9
                    "stat -f %z " Q fname Q | getline
                    len = $0
                    print fname, offset / len * 100 "%"
            } 
    ' Q="'"
$ sh -n wrk.sh
$

Show us the full script code.

Jean-Pierre.
# 7  
Old 11-18-2009
Here is everything i have in my script right now...

Code:
#!/bin/bash

echo
echo "This script will find a string in the JCATS .fplan file and replace the next line with"
echo "the next line with info from a csv file."
echo "Written by Brian Biller with help from the internets Nov 2009"
echo
echo "Please enter the name of the fplan you would like to change"


cnt=0
while true
do
read -r fplan
if [[ -f "$fplan" || -f ../"$fplan" ]]
then
echo "File exists"
break
else
echo "Try again"
fi

((cnt+=1))
if [[ $cnt -eq 5 ]]
then
echo "exceeded 5 tries. quitting"
exit
fi
done

echo
echo "Please enter the name of the csv file you would like to import from"
cnt=0
while true
do
read -r csv
if [[ -f "$csv" || -f ../"$csv" ]]
then
echo "File exists"
break
else 
echo "Try again"
fi

((cnt+=1))
if [[ $cnt -eq 5 ]]
then
echo "exceeded 5 tries. quitting"
exit
fi
done

echo
echo "Please enter the string you would like to replace"
read -r string

echo
echo "Please wait while the task completes"

lsof -o0 -o -p $$ |
awk '
            BEGIN { CONVFMT = "%.2f" }
            $4 ~ /^[0-9]+r$/ && $7 ~ /^0t/ {
                    offset = substr($7, 3)
                    fname = $9
                    "stat -f %z " Q fname Q | getline
                    len = $0
                    print fname, offset / len * 100 "%"
            } 
    ' Q="'"

sleep 5
 
I=1

while read -r LINE
do                                                
    if [[ "${LINE}" =~ "SystemDeclarationData" ]] #~ this is the first string to search for.  constant unless the parameters change
    then
    echo "$LINE"
        read -r LINE                              #~ if the string on the next line has the string needing replaced then replace it with the string in line 1.
        if [[ ${LINE} =~ "$string" ]]            
        then
            head -n$I $csv | tail -n1            
            (( I ++ ))                            #~ the next instance of the pattern would be replaced by the string in line 2 and so on.
        else echo "$LINE"
        fi
    else echo "$LINE"
    fi

done < "$fplan" > $PWD/complete



sleep 5
echo
echo
echo  "backing up original fplan"

cp "$fplan" "$fplan".bak

sleep 5
echo
echo "renaming the changed file to work with jcats"

mv $PWD/complete "$fplan"

sleep 3
echo
echo "operation completed"
echo
echo

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Progress bar

Hi Experts; Im in the process of writing a shell script for enabling an IT operations to run archiving.We use netbackup. The script is complete, though there is one bit that i need help on. Im trying to have a progess bar for the procedure.I have gone through the man page of the command in... (5 Replies)
Discussion started by: maverick_here
5 Replies

2. Programming

C progress bar issues

-First- Hi guys im trying to create a small C app that'll run PING/NETSTAT and such and generate a report... I want to create a progress bar so I figure since I was gonna use multiple commands I was better of to create a function and call the bar when needed to print on the command line My... (7 Replies)
Discussion started by: Jess83
7 Replies

3. Shell Programming and Scripting

Progress bar for cp

I'm trying to use this code to get a progress bar for cp: "Can you get cp to give a progress bar like wget?" But I'm getting these errors: stat: illegal option -- c usage: stat awk: division by zero input record number 1, file source line number 4 I'm using Mac OS X 10.6... (1 Reply)
Discussion started by: pcwiz
1 Replies

4. Programming

A progress bar in C

Hello, it's me again...:eek: I need to create a progress bar in C, but i have no idea on how to do it. i want it to output something like this: Progress: 58% But i can't get it to work. Could you please post an example progress bar written in ANSI C? Thanks (4 Replies)
Discussion started by: Zykl0n-B
4 Replies

5. Shell Programming and Scripting

progress bar

hi all, in shell script (ksh), how do i write a progress bar ?? i have a script which searches files and while its searching i am currently printing out "." and if it finds what its searching for the script prints out the name of the file e.g .................. firstFile.txt... (2 Replies)
Discussion started by: cesarNZ
2 Replies

6. Shell Programming and Scripting

Progress bar

Hi friends, how can I show a progress bar for any running process in the shell script. For example when I am copying or compressing a file. Thanks. (1 Reply)
Discussion started by: dwiravi
1 Replies

7. UNIX for Advanced & Expert Users

how to have a cp progress bar?

Hi all, This is a reformed post to my earlier ones!!!!!! I would like to know how to include a progress bar while using the cp... I am copying a few huge files from cdrom but am unable to figure out ,how to give a progress bar!!!!! I checked out other sites as well,but the issue here is... (1 Reply)
Discussion started by: wrapster
1 Replies

8. UNIX for Advanced & Expert Users

progress bar

Hi all, I want to print # like that in a progress bar.. For e.g We can notice that during installation ... but,how to do that? Thnx, sakthi. (4 Replies)
Discussion started by: sakthi.abdullah
4 Replies

9. Shell Programming and Scripting

progress bar

i am trying to write a script where in it will connect to remote servers and execute remote scripts to fetch some data and ftp it back to a main server. i would like to add a script where it will show some sort of status bar until such time that the expected files have been recieved. something... (3 Replies)
Discussion started by: inquirer
3 Replies
Login or Register to Ask a Question