LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 09-28-2013, 04:19 PM   #1
Djyou
LQ Newbie
 
Registered: Jun 2013
Distribution: Whatever works for the job.
Posts: 23

Rep: Reputation: Disabled
Linux timer script variable errors


So I have a script I am build to act as a timer whenever I run it with an argument, and count done that many seconds. However I need it run a while loop so long as the number as seconds is greater than zero
and an if then when it is equal to zero, however I keep getting a cannot find 20 error for line five whenver I run it as ./timer.sh 20

Here is the code as it stands
Code:
#! /bin/sh

seconds=$1

while $seconds > 0
do
echo $seconds
sleep 1
seconds=$seconds-1
if $seconds==0
then
espeak 2>/dev/null 'TIMER COMPLETED'
fi
done
 
Old 09-28-2013, 04:55 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by Djyou View Post
So I have a script I am build to act as a timer whenever I run it with an argument, and count done that many seconds. However I need it run a while loop so long as the number as seconds is greater than zero
and an if then when it is equal to zero, however I keep getting a cannot find 20 error for line five whenver I run it as ./timer.sh 20

Here is the code as it stands
Code:
#! /bin/sh

seconds=$1

while $seconds > 0
do
    echo $seconds
    sleep 1
    seconds=$(( $seconds -1 ))
    if $seconds==0
    then
        espeak 2>/dev/null 'TIMER COMPLETED'
    fi
done
exit
Edit
change echo $seconds
to
printf "%5d" "$seconds\r"

for a cleaner output

also add
printf "\n"
just above the exit

Last edited by Firerat; 09-28-2013 at 04:59 PM.
 
Old 09-28-2013, 05:53 PM   #3
Djyou
LQ Newbie
 
Registered: Jun 2013
Distribution: Whatever works for the job.
Posts: 23

Original Poster
Rep: Reputation: Disabled
Still not working, here is the EXACT error message I get

Code:
./timer.sh: 5: ./timer.sh: 20: not found
Neither of our scripts work and both return this error/
 
Old 09-28-2013, 06:10 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
hmm, didn't actually test it, or even pay much attention to what it was doing

change the first line to

#!/bin/bash

or

Code:
#!/bin/bash
seconds=$1
until (( $seconds == 0 ));do
    printf "%5d\r" "$seconds"
    sleep 1
    seconds=$(( $seconds - 1 ))
done
espeak 2>/dev/null 'TIMER COMPLETED'
printf "\n"
exit

Last edited by Firerat; 09-28-2013 at 06:13 PM. Reason: don't know why I did a for loop?
 
Old 09-28-2013, 06:23 PM   #5
Djyou
LQ Newbie
 
Registered: Jun 2013
Distribution: Whatever works for the job.
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks, that worked beautifully, I'll keep working on the fine tuning but thanks again for the help.

--Djyou
 
Old 09-29-2013, 05:59 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
A for loop alternative:
Code:
for ((seconds=$1;seconds>0;seconds--))
do
    ....
done
A briefer while loop:
Code:
seconds=$1
while ((--seconds>-1))
do
    echo $seconds
    sleep 1
done
espeak 2>/dev/null 'TIMER COMPLETED'
 
Old 09-29-2013, 07:31 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,390
Blog Entries: 3

Rep: Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479Reputation: 1479
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-29-2013, 09:22 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961Reputation: 1961
Just in case it is not clear about original error, the commands while and if themselves test the return code of a function / command.
So when you wrote:
Code:
while $seconds > 0
You were telling bash to execute '$seconds' with parameters / arguments '>' and '0', hence the message that '20' command was not found.

The correct format has been demonstrated by Firerat and colucix, but for clarity, the above should be written as:
Code:
while (( seconds > 0 ))
The alternative would be to use square brackets (although round are specifically designed for arithmetic) and would be written as:
Code:
while [[ $seconds -gt 0 ]]
Hope that helps

Note: 'if' requires the same as above
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 8 03-18-2013 09:13 PM
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 01:26 AM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 08:59 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
Red HAt install First timer...getting unusual errors??? soundengineer Red Hat 3 06-04-2004 09:40 PM


All times are GMT -5. The time now is 02:02 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration