LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-20-2005, 11:01 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
bash script ...


Hi i need to create a bashcript that remembers how many times it has been executed

For example
sh myscipt
This is the 1 time
sh myscript
This is the 2 time

and so on... how i can implement this? I have thought to store the counter into a file or into a shell variable?? What do u want to suggest me and how i can implement this?
Thx a lot
 
Old 07-20-2005, 12:38 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Why not have the script open itself and write the number of times to the bottom of itself? Or make it find the line starting something like "RUN: " and append the number of times it's been executed there?

Or make a file ".myscript" (a hidden file) in the home directory that contains the number of runs. You could use the env variable idea, but then that will be reset at each boot.
 
Old 07-20-2005, 05:42 PM   #3
Pestossimo
LQ Newbie
 
Registered: Jul 2005
Location: Evanston, IL
Posts: 7

Rep: Reputation: 0
I personally like the idea of the script editing itself. It sounds messy and like bad coding style, which I am all for in casual computing. Hooray for the IOCCC.

If you wanted to use an evironment variable, you could write it to a line of .bashrc or some other startup script so that the variable is restored each time the computer reboots. However, this might be even more hairy than the previous suggestions.

Last edited by Pestossimo; 07-20-2005 at 05:46 PM.
 
Old 07-21-2005, 12:45 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx a lot guys but your answers didnt help so much...... I want each time to remember the number of executes times... because i want to create each time a directory and put in there some files ....

sh myscript
creating folder 1
copying fles in the folder

sh myscript
creating folder 2
copying files in the folder

When i reboot the computer
sh myscript
creating folder 3
copying files in the folder

and so on,... is it clear?
 
Old 07-21-2005, 02:29 AM   #5
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,
the "remember" feature does not exist.
what you can do is:
if you have a standard name for you directories - i.e. all of them are called fooXX, you can just increment your variable based in how many directories foo* exists.
regards
slackie1000
 
Old 07-21-2005, 09:53 PM   #6
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
thx...and how i can do this?
 
Old 07-22-2005, 02:02 AM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,
what have you tried so far? show me what you got and i give you the directions..
regards
slackie1000
 
Old 07-23-2005, 02:55 AM   #8
seran
Member
 
Registered: Aug 2004
Location: Bangalore
Posts: 64

Rep: Reputation: 16
This thread was interesting!

I tried out some ways to achieve this. I thought of using a named pipe and I tried it also. But FIFO dont keep data after reboot/logout.

So, decided to keep the count inside a hidden file called .counter . And my .bashrc looks like this.

[seran@seran seran]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

COUNTER=`cat .counter`
export COUNTER

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

And the script which I used to make use of COUNTER is this,

[seran@seran seran]# cat prcount.sh
echo "Number of times...${COUNTER}"
COUNTER=`expr $COUNTER + 1`
echo $COUNTER > .counter

This does the work, but looks sloppy.

I was searching inside the /etc/rc.d/ scripts to find out something. I thought the different run level scripts can give some ideas for achiecing this. But I gave up after spending some hours.

Guys! Any more suggestions?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script zaicheke Programming 3 11-07-2004 06:32 PM
bash script how to? Lleb_KCir Programming 11 05-21-2004 07:03 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration