You are not logged in.

#1 2006-01-04 19:18:00

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Bash script - backgrounding a function?

*I'll be very grateful to whoever correctly answers this question, since I need to use this script tonight*

Here's a quick script I've written to help me adjust to polyphasic sleep (please don't start asking me about that here - if you've got questions, make a post in off-topic and I'll answer them wink).

I want the script to stay running permanently, continuously listening for my input, and adjusting $DELAY accordingly, then restarting the function 'sleeping'.

The problem is, once I call 'sleeping', there's simply nothing on the screen, as the whole script waits for the 'sleep' bit in 'sleeping' to finish.

Is there some way I can background the 'sleeping' function, so that my 'while' loop will still execute, even while 'sleeping' is running?

Also when this backgrounding is achieved, I only want one instance of 'sleeping' running at any time - if I call 'sleeping' again, will it stop the instance of 'sleeping' that was running beforehand?

#!/bin/bash

if [ $UID != "0" ]; then
    echo "You must run this script as root." 1>&2
    exit
fi

sleeping() {
    sleep $1m
    alsactl restore
    play /home/.sounds/alarm.mp3
    DELAY=60
    sleeping $DELAY
}

DELAY=60
sleeping $DELAY

while [ "1" -lt "2" ]
do
    clear
    echo "1. I'm awake"
    echo "2. I'm going for a nap"
    echo "3. I'm going out"
    echo "4. I'm back"
    echo "n"
    echo -n Current Time:
    date +%k:%M:%S
    echo "n"
    echo Time Remaining: $DELAY minutes
    read -e $input
done

if [ $input = "1" ]
then
    DELAY=60
    sleeping $DELAY
elif [ $input = "2" ]
then
    DELAY=35
    sleeping $DELAY
elif [ $input = "3" ]
then
    DELAY=999999
    sleeping $DELAY
elif [ $input = "4" ]
then
    $DELAY=60
    sleeping $DELAY
fi

.oO Komodo Dave Oo.

Offline

#2 2006-01-04 19:35:34

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Bash script - backgrounding a function?

no. bash does not have "threading".
you need to use a more robust language (python.ruby,etc)..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2006-01-04 19:40:21

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Bash script - backgrounding a function?

cactus wrote:

no. bash does not have "threading"

I guess I knew that I was asking for threading, I just didn't want to admit it.

Balls... I'll do it in python then. Time for Dive Into Python again :?


.oO Komodo Dave Oo.

Offline

#4 2006-01-04 21:50:09

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Bash script - backgrounding a function?

I decided to do it by just using a pair of scripts instead, where one of the scripts is the function in my original script.

However, I can't for the life of me understand why this doesn't change the variable DELAY when a number is put in. (Note - the script has many more parts than this, but this is the structure that isn't functioning correctly). Any clues?

#!/bin/bash

DELAY=60

while [ "1" -lt "2" ]
do
    echo Time Remaining: $DELAY minutes
    read -t 5 -e $input

if [ "$input" = "1" ]
then
    DELAY=60
elif [ "$input" = "2" ]
then
    DELAY=35
elif [ "$input" = "3" ]
then
    DELAY=999999
elif [ "$input" = "4" ]
then
    DELAY=60
fi
done

.oO Komodo Dave Oo.

Offline

#5 2006-01-04 22:36:36

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Bash script - backgrounding a function?

"export DELAY" at the bottom? /shrug

Offline

#6 2006-01-04 22:42:33

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Bash script - backgrounding a function?

Change

read -t 5 -e $input

to

read -t 5 -e input

Offline

#7 2006-01-04 22:47:14

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Bash script - backgrounding a function?

alterkacker wrote:

Change

read -t 5 -e $input

to

read -t 5 -e input

Hehe, what a pillock, of course.. it's always something small.

Many thanks alterkacker wink


.oO Komodo Dave Oo.

Offline

Board footer

Powered by FluxBB