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:
#!/bin/sh

nfilename="./nvalue"

n=0
touch $nfilename
. $nfilename

echo "current n value is $n"

n=$(expr $n + 1)

if [ $n -gt 10 ]; then
n=1
fi

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
apogee@apogee-persiasys:~$

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

Comments

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

regard

Damitr

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:
http://artofapogee.blogspot.com/2008/03/gimp-best-free-photoshop.html

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 up when yo…

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 OneThis is the example text for title one.
Title TwoThis is the example text for title two.
Title ThreeThis 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 (whichpost.className==&q…

VBScript Get File Extension

Before anything else, you may love to read this book too!...


Actually, I got this question from the reader of my previous blog post about VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder, and I decided to answer it here.

Question from Justin:
I tried something simular..but modified your VBS..
'======================================================== ' VBScript to replace underscore in file name with space ' for each files in a folder ' Written by ApOgEE of http://coderstalk.blogspot.com '======================================================== Dim sName Dim fso Dim fol ' create the filesystem object Set fso = WScript.CreateObject("Scripting.FileSystemObject") ' get current folder Set fol = fso.GetFolder(".") ' go thru each files in the folder For Each fil In fol.Files ' check if the file name contains underscore If InStr(1, fil.Name, "The ") <> 0 Then ' replace underscore with…