You are not logged in.

#1 2011-08-28 08:27:06

SlayingDragons
Member
Registered: 2011-07-12
Posts: 42

[solved] Sh variable problems...

Sorry for vague title, but I'm at a loss at exactly what the problem is.

I'm writing a small alarm program to wake me up in the morning for school. I have it working almost exactly like I want it to, where I could stop working on it now and still have it be functional, but I want to figure this out.

Before I go on, the code:

#!/bin/sh

#alarm_set="01:33"
alarm_set="33"
mfile="/home/lucas/Musics/Aphex Twin/Drukqs/1-01 - jynweythek.mp3"
mode=+%S #%I:%M
x=1
echo $mfile

function alarm(){
while [ $(date $mode) != $alarm_set ]
do 
	sleep 1s
done

if [ $(date $mode) == $alarm_set ]
then
	echo "Alarm went off."
	vlc /home/lucas/Musics/Aphex\ Twin/Drukqs/*.mp3 & sleep 1m
#	vlc $mfile & sleep 1m
	kill $!
else
	echo "Hmm, something went wrong..."
fi
}

while [ $x = 1 ]
do 
	alarm &
	sleep 1m
done

exit 0

#All the values in this are for testing

When I try to run vlc with the $mfile variable set in the code above, it returns the errors:

File reading failed:
VLC could not open the file "/home/lucas/Musics/Aphex".
Your input can't be opened:
VLC is unable to open the MRL 'file:///home/lucas/Musics/Aphex'. Check the log for details.
File reading failed:
VLC could not open the file "/home/lucas/Desktop/programs/Twin/Drukqs/1-01".
Your input can't be opened:
VLC is unable to open the MRL 'file:///home/lucas/Desktop/programs/Twin/Drukqs/1-01'. Check the log for details.
File reading failed:
VLC could not open the file "/home/lucas/Desktop/programs/jynweythek.mp3".
Your input can't be opened:
VLC is unable to open the MRL 'file:///home/lucas/Desktop/programs/jynweythek.mp3'. Check the log for details.

But if I change the variable to the following:

mfile=/home/lucas/Musics/=bansheebeat=/*.mp3

Or anything else without spaces, it'll work. So the spaces have to be the problem, except that I can put what didn't work with the variable as the file to open after vlc and it will work. I've tried backslash escaping, all kinds of quoting, and can't seem to get it to work.

But, being me, hopefully it'll be something simple and stupid I overlooked. Any help? smile

Last edited by SlayingDragons (2011-08-28 16:37:49)

Offline

#2 2011-08-28 10:33:06

krisoijn
Member
Registered: 2011-03-18
Posts: 15
Website

Re: [solved] Sh variable problems...

Will quoting it work?

vlc "/home/lucas/Musics/Aphex Twin/Drukqs/*.mp3"

EDIT:

#!/bin/sh

# Usage: $0 10:10

TUNEDIR="$HOME/Downloads"
WAKEUP=06:30
PLAYCOUNT=5

[ -z $1 ] || WAKEUP=$1

alarm() {
  TIME=$(date +"%H:%M")

  if [[ $TIME == $WAKEUP ]]; then
    for COUNT in {1..$PLAYCOUNT}; do
      smplayer "$(ls -1 "$TUNEDIR"/*.mp3 | shuf | head -n1)" && sleep 30
    done
    exit 0
  fi
}

while true; do
  alarm
  sleep 10
done

A simple cronjob would do the job also.

anyhow, I just wrote this rather quickly. Test it out youself if you are gonna use it. Change smplayer, WAKEUP and TUNEDIR variables smile

If you are going to paste a shell script, please remove any unused variable and format it better. (PS: I am not saying OP's code is badly format, just some that I have to sit down and read it...it make my head hurt)
Maybe codes (scripts) is for machine to run but more importantly it should be readable for human.
Not sure if just me or anyone else, I'd rather give up than trying to understand it.

Ninja EDIT:

if you are new to BASH scripting, you may wanna have a look at http://mywiki.wooledge.org/BashPitfalls

Last edited by krisoijn (2011-08-28 10:51:54)

Offline

#3 2011-08-28 12:00:17

fabertawe
Member
From: Lloegr
Registered: 2009-11-24
Posts: 281

Re: [solved] Sh variable problems...

Have you tried quoting the variable when passed to vlc?

vlc "$mfile" & sleep 1m

Ryzen 9 5950X, X570S Aorus Pro AX, RX 6600, Arch x86_64

Offline

#4 2011-08-28 16:36:42

SlayingDragons
Member
Registered: 2011-07-12
Posts: 42

Re: [solved] Sh variable problems...

krisoijn wrote:

Will quoting it work?

vlc "/home/lucas/Musics/Aphex Twin/Drukqs/*.mp3"

EDIT:

#!/bin/sh

# Usage: $0 10:10

TUNEDIR="$HOME/Downloads"
WAKEUP=06:30
PLAYCOUNT=5

[ -z $1 ] || WAKEUP=$1

alarm() {
  TIME=$(date +"%H:%M")

  if [[ $TIME == $WAKEUP ]]; then
    for COUNT in {1..$PLAYCOUNT}; do
      smplayer "$(ls -1 "$TUNEDIR"/*.mp3 | shuf | head -n1)" && sleep 30
    done
    exit 0
  fi
}

while true; do
  alarm
  sleep 10
done

A simple cronjob would do the job also.

anyhow, I just wrote this rather quickly. Test it out youself if you are gonna use it. Change smplayer, WAKEUP and TUNEDIR variables smile

If you are going to paste a shell script, please remove any unused variable and format it better. (PS: I am not saying OP's code is badly format, just some that I have to sit down and read it...it make my head hurt)
Maybe codes (scripts) is for machine to run but more importantly it should be readable for human.
Not sure if just me or anyone else, I'd rather give up than trying to understand it.

Ninja EDIT:

if you are new to BASH scripting, you may wanna have a look at http://mywiki.wooledge.org/BashPitfalls

Yeah, sorry, I'm still pretty new to computers in general. I should have warned that I'm a noob. tongue

And thanks for the code, that makes infinitely more sense.

Oh, and yeah, quoting it worked. I thought I tried that. As I thought, it was something simple and stupid. lol

edit: I'll go ahead and mark this solved.

Last edited by SlayingDragons (2011-08-28 16:37:12)

Offline

Board footer

Powered by FluxBB