Skip to main content

Bash Scripting - Script execution counter

This Linux shell script does nothing than just increasing n value every time it is executed and then keep the value in a file. When the value goes greater than 10, it will drop back to 1. Uhhh... this reminds me to the increasing of the fuel price. But that fuel price will just increase and will never drop back... Lol... Anyway, I wrote this bash script just to make a permanent variable to be used as bash script running counter. With this code, I can count how many time the script have been executed.

Here's the code:


touch $nfilename
. $nfilename

echo "current n value is $n"

n=$(expr $n + 1)

if [ $n -gt 10 ]; then

echo "new n value is $n"

echo "n=$n" > $nfilename

Here's the step for testing the script for newbies bash scripter.

1. Create one text file in your home folder. In this example I'm using ncounter as the file name:
apogee@apogee-persiasys:~ gedit ncounter

2. Copy and paste the codes in this file, then save and close it.

3. Change the file permission to executable:
apogee@apogee-persiasys:~ chmod +x ncounter

4. Then you can test it... here's my output example:
apogee@apogee-persiasys:~$ ./ncounter 
current n value is 0
new n value is 1
apogee@apogee-persiasys:~$ ./ncounter
current n value is 1
new n value is 2
apogee@apogee-persiasys:~$ ./ncounter
current n value is 2
new n value is 3
apogee@apogee-persiasys:~$ ./ncounter
current n value is 3
new n value is 4

Feel free to experiment and change the ./nvalue file location to somewhere else where you have read and write permission. This is just another linux shell script that I haven't used yet... heheh


Damitr Mazanov said…
Thanks for the script, it has helped me a lot. Will send detailed report of how it was used.



Popular posts from this blog

How to Create Hyperlink on Blogger Post

Well, this time I make a basic simple tutorial. For all advanced geek who read this blog, please ignore this post... I just wanna help myself not to teach the same basic thing on every newbies friends that really need help. OK guys, back to the howto... There are two ways to create a hyperlink on your blog post. One is the simple GUI way and the other is coders way. I prefer the coder way as I can have more control on it. I like to teach by example. So, let say you wanna create a hyperlink on this line of words... You can go here to see my art blog where you wanna link the word "here" to the other page on your blog or on the internet. In this example, I wanna link it to my Art Of Apogee Blog post with the address: And here is how to do it using both ways... The simple GUI way If you rollover every button on your Compose toolbar, you can see one button called "Link" where the word Link will pop

How to Show and Hide Text in Blog Post

I would like to share on how to make a Show/Hide text in your blog post. I'm sorry if I didn't explain detail enough because I want to keep my post short as I'm in a hurry. However, you can read the detail explanation on Blogger Help . In this example, I assume you have sub topics that you want to make it hidden until reader clicks on the headline. Without the show/hide trick, it would look like this... Title One This is the example text for title one. Title Two This is the example text for title two. Title Three This is the example text for title three. Here's my step to do this... Firstly, edit your template and add the following code in the CSS area. .posthidden {display:none} .postshown {display:inline} Then add the following javascript code on your blog template between the <head></head> tag. <script type="text/Javascript"> function expandcollapse (postid) { whichpost = document.getElementById(postid); if (w

How to Show and Hide Text in Blog Post with teaser

As my reply to Andrea's question in the comment section of my previous post on How to Show and Hide Text in Blog Post , Here is how to show and hide text in blog post with something like 300 words teaser before the link to read the rest of the story... Click here to read more... This post is already the example to show that it is possible to use the same technique in my previous show/hide blog post article to have a teaser before the link to show the rest of the story. What I've done here is just placing the "span" tags at the right position where i wanted it to be. To make it short and clear, here is the sample template to this kind of blog post... --- Place your 300 words teaser here --- <span class="postshown" id="your-topic-link-id-0001"> <a href="javascript:expandcollapse('your-topic-link-id-0001'); expandcollapse('your-topic-id-0001');"> Click here for more...</a></span> <span class="