You are not logged in.

#1 2012-08-31 00:04:51

frostyfrog
Member
From: Utah, USA
Registered: 2011-03-27
Posts: 42

Playing mplayer with spaces in filepath through script.

So I'm having this issue where I'm constructing a string, then executing said string. The problem? mplayer is thinking (even after I've escaped in EVERY way that I can think of) that I'm passing in multiple files.

The code in particular is at the bottom of the main function. The last line in the image below is the completed string. (took the image because synergy wasn't being nice}

#!/bin/bash
# Declare variables here
TITLE="Frostyfrog's Countdown Timer"
TIME=30
MFILE="/home/frostyfrog/Music/Hitoshi\ Sakimoto/Best\ of\ the\ Valkyria\ Chronicles/01\ Valkyria\ Chronicles\ Series\ Main\ Theme.mp3"
MCMD='mplayer'

# Ask for how long to count down for
asktime() {
	t=$(dialog --stdout --title 'Timer' --backtitle "$TITLE" --inputbox 'How long do you want the countdown timer to last? (in minutes) (def. 30)' 8 50)
	# Make sure it's an int
	if ! [[ "$t" =~ ^[0-9]+$ ]]; then
		dialog --backtitle "$TITLE" --infobox "No time or invalid number specified, using the default of 30 minutes." 4 40
		sleep 3
	else
		TIME="$t"
	fi
}

# Main function
main() {
	tput smcup
	asktime
	counter=0
	# Make sure time is in minutes
	time=$(echo "$TIME*60"|bc)
	(while :
	do
		# Get percentage of time left
		t=$(echo "scale=2;$counter/$time*100" |bc|awk '{sub(/.[0-9][0-9]$/,""); print $1 }')
cat <<EOF
XXX
$t
$(date -d"0+$counter sec" +%H:%M:%S)
XXX
EOF
		(( counter += 1 ))
		[ $counter -gt $time ] && break
		sleep 1; 
	# Print out an ncurses progress bar
	done)|dialog --title "Counting Down From $TIME minute(s)" --backtitle "$TITLE" --guage 'Please wait' 7 70 0
	# Say we are done
	dialog --backtitle "$TITLE" --infobox 'Times up!' 4 40

	# Switch to music dir
	cd $(dirname $MFILE)
	# Construct music string
	music="$MCMD \"$(basename "$MFILE")\""
	# Run mplayer and file
	$music #&>/dev/null
	echo $music
	cd
	scrot
sleep 500
}

# Cleanup
btrap() {
	tput rmcup
}
trap btrap EXIT

# Start the program :D
main

dcjWI.png

Edit: I though I should note that running the command by itself works (outside of the script).

Last edited by frostyfrog (2012-08-31 02:17:16)


{arch32} {subtlewm}{Acer Aspire One AO532h}
{arch64} {Headless Server}
Grrr! 400 char limit sad

Offline

#2 2012-08-31 00:15:45

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Playing mplayer with spaces in filepath through script.

You either have quotes around it and no delimiter, or no quotes and delimiters.  But if you have both, bash reads the delimiters as true backslashes.  So pick one and go with it.

Offline

#3 2012-08-31 00:30:37

frostyfrog
Member
From: Utah, USA
Registered: 2011-03-27
Posts: 42

Re: Playing mplayer with spaces in filepath through script.

And when I remove the quotes, the same exact thing happens. When I remove the delimiters, again, same thing happens D:.


{arch32} {subtlewm}{Acer Aspire One AO532h}
{arch64} {Headless Server}
Grrr! 400 char limit sad

Offline

#4 2012-08-31 03:31:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Playing mplayer with spaces in filepath through script.

$ mplayer "/home/frostyfrog/Music/Hitoshi\ Sakimoto/Best\ of\ the\ Valkyria\ Chronicles/01\ Valkyria\ Chronicles\ Series\ Main\ Theme.mp3"
MPlayer2 UNKNOWN (C) 2000-2012 MPlayer Team
Can't open joystick device /dev/input/js0: No such file or directory
Can't init input joystick
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing /home/frostyfrog/Music/Hitoshi\ Sakimoto/Best\ of\ the\ Valkyria\ Chronicles/01\ Valkyria\ Chronicles\ Series\ Main\ Theme.mp3.
File not found: '/home/frostyfrog/Music/Hitoshi\ Sakimoto/Best\ of\ the\ Valkyria\ Chronicles/01\ Valkyria\ Chronicles\ Series\ Main\ Theme.mp3'
Failed to open /home/frostyfrog/Music/Hitoshi\ Sakimoto/Best\ of\ the\ Valkyria\ Chronicles/01\ Valkyria\ Chronicles\ Series\ Main\ Theme.mp3.


Exiting... (End of file)

The problem seems to be with how you call mplayer to play the song.

Alternatively you can rename the file and use underscores instead of spaces.

Offline

Board footer

Powered by FluxBB