LinuxQuestions.org
Help answer threads with 0 replies.
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 01-12-2016, 09:19 PM   #1
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Rep: Reputation: 169Reputation: 169
if else difficulty


How do I get this to send time to file every 10 seconds ?

Quote:
#!/bin/bash
#
# Send date/time to file every 10 seconds
#
count=5
if [ $count == 5 ]
then
sleep 10
date "+ %m/%d/%y %r" >> date.txt
else
echo "Has not been 10 seconds"
fi
 
Old 01-12-2016, 09:40 PM   #2
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
This works.

Quote:
#!/bin/bash
#
# Linux Puppy 6.3.0
# SiegeWorks 2016 A.P.K.
#
# Send date/time ten times to file every 5 seconds
#
count=1
done=0

while [ $count -le 10 ]
do
sleep 1
(( count++ ))
if [ $count == 5 ]
then
continue
fi
sleep 5
date "+ %m/%d/%y %r" >> date.txt
done
echo Finished >> date.txt

Last edited by Fixit7; 01-12-2016 at 09:41 PM.
 
Old 01-13-2016, 06:39 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
If I may comment, although you seem to be happy....

A matter of style: If you use (( count++ )), why not if ((count<=10)) and if ((count==5))?

Also, it doesn't work. You write into date.txt roughly every 6 seconds, since
  • you first sleep a second,
  • then decide if you want to skip the rest of the loop (which you do only once, when the count reaches 5),
  • then sleep 5 seconds,
  • then record date and time.
Your code is somewhat complicated; is it supposed to so something else in addition to recording date/time?
 
Old 01-13-2016, 07:13 AM   #4
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
This is a latter edition that does something useful.

I will study your suggestions.

Quote:
#!/bin/bash
#
# Linux Puppy 6.3.0 SiegeWorks 2016 A.P.K.
#
# Send CPU temps to file every 15 minutes
# NOTE: This messes with icon for CPUtemp
count=1
done=0

while [ $count -le 10 ]
do
(( count++ ))
if [ $count == 5 ]
then
continue
fi
date "+ %m/%d/%y %r" >> ~/Documents/CPU_TEMPS.txt
sensors -f >> ~/Documents/CPU_TEMPS.txt
sleep 900
done
 
Old 01-13-2016, 10:10 AM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Maybe I'm missing something, but why is this script so complicated? The "done" variable is never used, and what's the point of "count" causing the script to randomly skip the date/sensors part of the code on the 4th iteration of the while loop?

What was wrong with the original one? Why can't you just do:
Code:
#!/bin/bash

date "+ m/d/y %r" >> ~/Documents/CPU_TEMPS.txt
sensors -f >> ~/Documents/CPU_TEMPS.txt
And stick it in a cron job that runs every 15 minutes?

If you want any useful advice, you need to clarify what it is you're trying to do here.
 
Old 01-13-2016, 10:14 AM   #6
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
So, what do I need to do delete to eliminate the 4th iteration part ?

I am always in a state of learning and mistakes come with the territory.

I have used cron. Feel it is a little complicated.
 
Old 01-13-2016, 10:15 AM   #7
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
This needs work too.

Quote:
#!/bin/bash
#
# Linux Puppy 6.3.0 SiegeWorks 2016 A.P.K.
#
# Send battery state to file every 15 minutes
# Useful in determining how long your battery will actually last
#
count=1
done=0

while [ $count -le 10 ]
do
sleep 1
(( count++ ))
if [ $count == 5 ]
then
continue
fi
date "+ m/d/%y %r" >> Battery_State.txt
grep remaining /proc/acpi/battery/*/state >> Battery_State.txt
#
grep full /proc/acpi/battery/*/info >> Battery_State.txt
#
grep charging /proc/acpi/battery/*/state >> Battery_State.txt
sleep 900
done
 
Old 01-13-2016, 10:45 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Again, you need to tell us what it is you're trying to do. Posting random code samples with NO explanation of what it's doing, what you expect it to be doing, what's good/bad about it, etc. is pointless.

Cron is one of the least complicated parts of Linux. You tell it when to run and what to run, what's complicated about that?

Last edited by suicidaleggroll; 01-13-2016 at 10:47 AM.
 
1 members found this post helpful.
Old 01-13-2016, 10:47 AM   #9
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
I put notes in my scripts.
Is it not clear what I am trying to do ?

# Send battery state to file every 15 minutes
# Useful in determining how long your battery will actually last
 
Old 01-13-2016, 10:54 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
And how are you running it? Why do you have a counter in there at all?
 
Old 01-13-2016, 10:58 AM   #11
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
Originally Posted by suicidaleggroll View Post
And how are you running it? Why do you have a counter in there at all?
I double click the bash script. Or I put it in startup dir.

Counter is so it does not run forever.

I would prefer that it only run until a certain time has been reached.

Like run for 3 hrs.
 
Old 01-13-2016, 11:13 AM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sounds like a pain to me, having to manually run it every time...
Either way, it sounds like you just need to get rid of that if statement that's checking count, it doesn't seem to serve any purpose.

Code:
#!/bin/bash

count=1

while [ $count -le 10 ]; do
   (( count++ ))
   date "+ m/d/y r" >> Battery_State.txt
   grep remaining /proc/acpi/battery/*/state >> Battery_State.txt
   grep full /proc/acpi/battery/*/info >> Battery_State.txt
   grep charging /proc/acpi/battery/*/state >> Battery_State.txt
   sleep 900
done
It would be simpler to just let it run all the time, automatically. Is there any particular reason you don't want it running all the time?

Code:
#!/bin/bash

date "+ m/d/y r" >> Battery_State.txt
grep remaining /proc/acpi/battery/*/state >> Battery_State.txt
grep full /proc/acpi/battery/*/info >> Battery_State.txt
grep charging /proc/acpi/battery/*/state >> Battery_State.txt
Then in cron:
Code:
*/15 * * * * /path/to/script

Last edited by suicidaleggroll; 01-13-2016 at 11:15 AM.
 
Old 01-13-2016, 11:35 AM   #13
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks.

In Puppy versions, any scripts in the root/Startup dir always start whenever computer is booted.

Date part of script is not working.

m/d/y r
remaining capacity: 2680 mAh
last full capacity: 3556 mAh
charging state: charging

Last edited by Fixit7; 01-13-2016 at 11:42 AM.
 
Old 01-13-2016, 11:37 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sorry, that's a forum problem. For some reason "%" symbols get stripped out of posts. Just use what you already had:
Code:
date "+ %m/%d/%y %r" >> date.txt
 
Old 01-13-2016, 11:42 AM   #15
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks a lot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Difficulty getting association Haines Linux - Networking 9 03-26-2015 01:57 PM
difficulty enios LinuxQuestions.org Member Intro 5 08-30-2011 11:50 PM
Difficulty in understanding Gins Programming 6 07-12-2006 04:22 PM
Difficulty in Cinelerra BobNutfield Linux - Software 2 02-15-2006 07:33 AM
difficulty with wireless - sometimes 105659 Linux - Wireless Networking 0 01-22-2006 08:36 PM

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

All times are GMT -5. The time now is 04:40 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