LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2016, 09:22 PM   #1
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Rep: Reputation: Disabled
Search file and then add one to its total


Hi all,

I am trying to work thru a problem.

I have a file, that only contains a number (starting at 09). I am trying to run a script that reads that file and then adds 1 to it up to a certain value (42). Once that output in the file gets to 42, it resets to 9.

Code:
#!/bin/bash
##############################
Counter=/home/Counter
##############################

	if [ cat "$Counter" -eq 43 ] ; then
		echo " Resetting the Counter"
		9 > $Counter
	else
	$Counter="`cat $Counter`+1"
	fi
exit 0
I am hoping that the output to "Counter" will be a number that increases by 1 each time the script it run (once per week). Once the script gets to 43, it will reset the value in the "Counter" file to 9.

What am I missing? I hope I am getting close, but I am puzzled as to how to make this work

Any thoughts/comments/suggestions would be appreciated.
 
Old 08-07-2016, 09:48 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
First thing I would say is to be consistent with your information. Initially you have advised the value in the file will be '09', but when you reset the value it will be '9' ... why not '09'?

As for your current code, you have a few issues:

1. It is recommended in bash to use [[]] over [] (see here for more details)

2. cat "$Counter" - This will output data to the screen and is not for being tested. You would need to use process substitution and prefer $() over `` (see here for more details)

3. 9 is not a command and hence cannot be redirected anywhere. Think of what command you would use to show a 9 on screen

4. $ is not used on the left of the equals sign in general. If it were, you would be saying the following:
Code:
/home/Counter="..."
5. "`cat $Counter`+1" :- see previous information on using $() instead, but this is still wrong as it will return a string as follows (assuming '09' is in file)
Code:
Counter="09 + 1"
echo $Counter
09 + 1
As you can see the above is not the desired output or affect

6. Further to the above, you need to look up the use of either the 'let' command or (()), I prefer the latter as easy to spot in code and a little clearer

Let us know how you get on
 
Old 08-07-2016, 10:03 PM   #3
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thanks for your reply.

Yes, I did mean just 9

Thank you for your guidance. I have the script up and running now.

Appreciate your help!
 
Old 08-07-2016, 10:53 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Don't forget to show the working script so others may learn from your experience
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] search a directory and add file contents when find string j-me Linux - General 3 01-11-2013 08:54 AM
[SOLVED] Vi help (search, replace,add) haiders Linux - Software 12 03-18-2011 03:55 PM
how to add a url to search engines karan101 General 2 06-01-2006 02:34 AM
How to add google search on my site tooparam Programming 5 01-05-2006 01:40 PM
How can I add search to the K menu? SolidSnakeX28 Linux - Software 2 09-17-2005 09:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

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