How to display a counter in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to display a counter in shell script?
# 1  
Old 07-08-2011
How to display a counter in shell script?

Hi,
I am writing a script which processes large number of files in a directory. I wanto display a counter which increment after processing each file. I am processing each file in a for loop. If I echo a variable with its value increasing with each file, I will get around 5000 lines as output. Instead I wish to display a count - like a digital clock- which increases at the same position and line.

Can anyone help with any command to achieve this?

Thanks
Jaise
# 2  
Old 07-08-2011
Something like this?
Code:
for ((i=0; i<120; i++))
do
    printf '\r%3d' $i
    sleep .5
done
echo

# 3  
Old 07-08-2011
Code:
START=$( date +%s ); while true; do CURRENT=$( date +%s ) ; echo $(( CURRENT-START )) ; sleep 1 ; echo -n  ; done

# 4  
Old 07-08-2011
Hi,
Thanks for the quick replies.
here is the code I wrote.

Code:
ls -lrt *.txt |awk '{print $9}' > $HOME/file.list                                                                          
for file in `cat $HOME/file.list`                                  
   do                                                                  
     echo " Processing $file" >>$HOME/file_process.log
     $EXEC/xxx.exe $file      >>$HOME/file_process.log                             

   done


In the file.list, there will be around 5000 entries. If I put a normal print or echo command inside the loop, the output will be displayed 5000 times. I dont want this to happen. Still I want to know th eprogress of the activity.

If I use a normal counter by using a variable increment by one on each execution of the loop, output will be like this
Code:
1
2
3
4
5
6
7
.
.
.

I want to know if it is possible to display 1, then 2 at the position of 1, then 3 at the position of 2 and so on... I mean like a digital clock.

Any luck?

Moderator's Comments:
Mod Comment Please start using [CODE] tags for posting source listings, console output, ...

Last edited by pludi; 07-08-2011 at 11:11 AM..
# 5  
Old 07-08-2011
Hi Jaiseaugustine,

take a perameter file with counter. once each file process completed then update the parameter file.

code :
Code:
cd $src_dir_path
TXT1=`cat Parameter.txt | grep "Counter=" | head -1` ;
FTXT1=`cat Parameter.txt .txt | grep "Counter=" | awk -F"=" '{print $1}' | head -1` ;
FTXT2=`cat Parameter.txt .txt | grep "Counter=" | awk -F"=" '{print $2}' | head -1` ;
NTXT=`expr ${FTXT2} + 1` ;
sed "s/$TXT/$FTXT"="$DT/g" Parameter.txt > Parameter_temp.txt ;
cp Parameter_temp.txt Parameter.txt
sed "s/$TXT1/$FTXT1"="$NTXT/g" Parameter.txt > Parameter.txt_temp.txt;
cp Parameter_temp.txt Parameter.txt .txt
chmod 777 Parameter.txt.txt
chmod 777 Parameter_temp.txt


parameter file should be like this:
Parameter.txt:
Code:
Counter=0


regadrs
rajesh
# 6  
Old 07-08-2011
Did you read the above posts? Did you try some of the things posted there? If you want a read-to-go solution without having to do anything yourself, say so, but this will probably put people off from helping you.
# 7  
Old 07-08-2011
This works with ksh, bash and dash
Code:
cnt=0                        
# if you need only  files which names are *.txt, then no need ls+awk                                    
for file in *.txt                           
do                    
     cnt=$((cnt+1))
    # -n = no newline and -e needed if your echo not accept \r
    # \r carriage return - cursor to the position 1
     echo -n -e  "\r$cnt $file                                     "                                              
     $EXEC/xxx.exe $file      >>$HOME/file_process.log                             
done
echo 
echo "The End"

This User Gave Thanks to kshji For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display calendar in correct format using shell script

Hi All, I'm trying to print calendar using shell script and i'm able to print it. But the format is not good. Here is the script. #!/bin/bash echo $(date) echo "Hello $USER" echo Hostname $(hostname) echo Working in $(pwd) echo Here is this month calender echo $(cal) $ sh first.sh... (7 Replies)
Discussion started by: chandrakanth
7 Replies

2. Shell Programming and Scripting

Help with Display Shell Script for date

hi friends, I am working on extracting data from sar logs for analysis. What is want to do is insert the date. The way i am doing is incrementing the date if i see the hour gets reduced (i.e. from 23 hours to 00 hours). Somehow the script which i have made is not able to handle the logic w.r.t... (3 Replies)
Discussion started by: kunwar
3 Replies

3. Shell Programming and Scripting

Help with Display Shell Script

hi Friends, I am writing a shell script to extract data from sar logs for my daily analysis. I have this in sar logs HP-UX dhpcdbe1 B.11.31 U ia64 05/09/12 01:22:42 device %busy avque r+w/s blks/s avwait avserv 01:27:59 disk30 8.79 13.72 26 427 36.47 ... (3 Replies)
Discussion started by: kunwar
3 Replies

4. Shell Programming and Scripting

Reset the counter in shell script

I am executing the following script using 'awk -f process.awk out' where 'out' is the input file which consists of 5000 sequences. Each time it takes one sequence, run the below program by creating a directory, run the mfold command within that directory, running another shell script 'final5' and... (2 Replies)
Discussion started by: kswapnadevi
2 Replies

5. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

6. Shell Programming and Scripting

How to write a shell script to display files in single path?

Hello friends, I am a script which dispalys a multiple files with their contents. for exm: suppose two file test1.txt and test2.txt. when I run my script it have to display the below O/P. test1.txt -rw-r----- 1 sranga staff 91 Sep 23 02:18 calc.sh -rw-r----- 1 sranga ... (2 Replies)
Discussion started by: sivaranga001
2 Replies

7. UNIX Desktop Questions & Answers

Shell script to display user logged in within given time

how can i know which users have logged in at specified given start and end time in 24 hour format? (3 Replies)
Discussion started by: meherzad4u
3 Replies

8. Shell Programming and Scripting

shell script that would display installing.... when installign something

Hi, I am developing a shell script that would install a software when it is run. I want to displaying Installing <so and so>........ when it is actually installing it. I want it to be dynamic, in the sense, I want the dots after installing grow and drop. Is there a way we can do it.... (1 Reply)
Discussion started by: eamani_sun
1 Replies

9. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies

10. UNIX for Dummies Questions & Answers

Shell Script Display?

I remember learning that there is a way to make a shell script display the script itself to standard output while the script is being executed but I can't find how to do that. Any pointers? (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question