LinuxQuestions.org
Review your favorite Linux distribution.
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 12-17-2017, 09:19 AM   #1
Ftret1
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Rep: Reputation: Disabled
Shell script alarm clocks


Hi

First of all, sorry if this is in the wrong section.

I'm trying to make a shell script that has multiple alarm clocks, but am running into a problem.

I try to put the alarm creation in a for loop, but this makes it so that the 2nd alarm will only be created when the 1st alarm has finished. I want it to initialize all alarms in the for loop, and then run them.

The code

Code:
#!/bin/bash
clear

echo "Welcome to your alarm clock.
Enter all time stamps as follows:
14:30, 15:20"
echo ""
read -p "How many alarms do you want to make?: " num

# Decides if previous was a number or not.
aa=1
if ((num<aa)); then
echo "Not a number"
fi

# For loop with the alarms in them.
for ((c=1; c<=num; c++))
do
"
read -p "What time does your alarm need to go off? (HH:MM) 
" target
echo ""
sleep $(( $(date --date="$target" +%s) - $(date +%s) ));
echo Wake up!

done
Thaks in advance
 
Old 12-17-2017, 12:35 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712
You need to detach your alarm processes from the main flow somehow, running task in background?
Just curious, why not just use at command?
 
Old 12-18-2017, 06:53 AM   #3
Ftret1
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
It's a small project I'm maing, learning bash

Do you have any idea how to detatch it?
 
Old 12-18-2017, 08:12 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712Reputation: 712
One simple way is to make function and start it in background, using & at the end of function call

Code:
# args: <time in seconds> <id of alarm>
bla() {
   echo "alarm #$2 started, sleep $1 seconds"
   sleep $1
   echo "Wake up from alarm #$2"
}

for i in {1..3}; do
   bla $((RANDOM % 10 +1)) $i &
done

Last edited by keefaz; 12-18-2017 at 08:15 AM.
 
1 members found this post helpful.
Old 12-20-2017, 04:04 AM   #5
Ftret1
LQ Newbie
 
Registered: Dec 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks a lot! you helped me fix it
 
  


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
Alarm script Fixit7 General 4 06-25-2016 04:40 PM
[SOLVED] Adjustable alarm script Fixit7 Puppy 3 06-26-2015 08:16 AM
LXer: 5 best Android alarm clocks LXer Syndicated Linux News 0 07-14-2013 12:00 PM
Shell Scripting Questions, Writing alarm clock script burgsprinta Linux - General 11 11-30-2010 01:37 AM
Script to raise the alarm akhtar.bhat Programming 4 11-04-2008 11:49 PM

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

All times are GMT -5. The time now is 10:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration