Shell Scripting Questions, Writing alarm clock script
I left the linux world for a few years and now i am trying to re-learn my shell scripting basics. I am writing a script for an alarm clock, but i seem to be running into a few issues. My script is posted below. If anyone could help, that would be great. Thanks.
------------------------------------------------------------ #!/bin/bash declare -i H declare -i M declare -i currentH declare -i currentM declare -i minutesLeft declare -i hoursLeft echo -e "What Hour (24 Format) do you want to Wake Up?" read H echo -e "What Minute?" read M echo `expr $H + 0` currentH=`date +%H` currentM=`date +%M` echo "You Selected " echo "$H:$M" echo -e "\nIt is Currently $currentH:$currentM" if [ "$currentH" < "echo $H" ]; then hoursLeft= `expr $H - $currentH` echo "$H - $currentH means You Have: $hoursLeft hours still" fi if [ "$currentH" > "$H" ]; then hoursLeft= `expr $currentH - $H` echo -e "\n$currentH - $H means you have $hoursLeft hours left \n" fi if [ "$currentH" == "$H" ]; then hoursLeft= 0 echo -e "Taking a nap?\n" fi if [ "$currentM" < "$M" ]; then minutesLeft= `expr $M - $currentM` echo -e "$M -$currentM menas you have: $minutesLeft minutes still" fi ---------------------------------- My script, instead of doing what it should, produces the following errors: ./alarm: line 19: echo 11: No such file or directory ./alarm: line 26: 6: command not found 17 - 11 means you have hours left ./alarm: line 37: -41: command not found 11 -52 menas you have: minutes still expr: non-integer argument 52 - 11 means you have 0 minutes left Sleeping... for hours and 0 minutes sleep: invalid time interval `h' Try `sleep --help' for more information. and if i choose the time 11:06, files called 11 and 6 are creating in the working directory. I am sure it is something really simple, but i haven't used linux or scripting on my computers in around 6 years. Thanks. |
Code:
if [ "$currentH" < "echo $H" ]; If you're comparing whether the current hour value is less than the alarm hour, then the "echo " makes no sense. Could you repost your code at http://pastebin.com/ (select Bash) so we can have line numbers, monospacing, and syntax highlighting? |
Yes the echo does not make sense. Sorry about that. That actually should have been cleaned up, but I deleted it from my script, but forgot to delete it from here.
Thanks for the quick response. |
Here's a few tips to get you further along:
1) When comparing integers, do it like this: Code:
if [ $H -gt 3 ]; then 2) When assigning values to vaiables, make sure there's no space after the "=" sign: Code:
hoursLeft= `expr $H - $currentH` 3) What does this do: Code:
echo `expr $H + 0` Code:
echo $((VAR + 5)) Cheers. |
Quote:
I am trying to set an alarm. I am going to add a command to invoke mplayer at the given time after the sleep commands, but this is more than anything just me trying to get back into scripting. Kind of frustrating, because I used to be able to do kind of task fairly easily. Somehow I completely forgot almost everything. Posted to http://pastebin.com/TMH7qtXk Thanks |
Thanks AlucardZero and GrapefruiTgirl. Fixed it now. Runs well now. Here is the new code:
Code:
#!/bin/bash |
Is it necessary for me to declare the variables as integers?
|
Quote:
Code:
A shell variable need not have its integer attribute turned on to be used in an expression. |
However! I should add that, something to keep in mind is that: if you're doing integer comparisons with variables, and one or another variable that has not been given a value (or zero), but you expect it to have an integer (not a string) then you can get errors. Like so:
Code:
root@reactor: [ $c -eq 3 ] && echo "equal" |
So i should set my integer variables equal to a non-zero number first? Thanks. The last code i posted doesn't work under all circumstances, but now that I have the very basics worked out, it shouldn't be too difficult. Thanks so much.
|
Well, take for example the following simple script:
Code:
#!/bin/bash To prevent the error, make sure $c will ALWAYS contain some integer, including zero, before comparing it. Tthere should either be an ELSE case to go with the IF test of the name, which sets c to some value other than 5; or, you should initialise c=0 at beginning of the script. So: Code:
#!/bin/bash |
Finished Script.
Okay Thanks for all your help. After reading your replies I started over from scratch, and it works well now with no errors. Also, I added a countdown timer as well. Here is my new code. Try it out, and tell me what you think.
Code:
#!/bin/bash |
All times are GMT -5. The time now is 07:03 PM. |