You are not logged in.

#1 2014-11-25 03:39:47

Xaero252
Member
Registered: 2011-11-28
Posts: 107

[BASH] rm, cp not working with variables in script

So, I'm completely defeated right now, I went to town on a script of mine overhauling it to be more flexible and have more user configuration available and to provide more organized results.
The script itself grabs the current window coordinates and starts ffmpeg recording (the first time it is executed, the second time it stops the ffmpeg in the original script and that allows the original to proceed past that point)
Anyways, the variable "filename" gets added to "/tmp/" to come up with the variable "tmp_name" on line 79. This variable is used several times to reference the file which ffmpeg should record to, and which file the rest of the script needs to work with.
Everything handles this variable fine except three things:
The "cp" command on line 98 (I threw it into a variable and eval'd it to try and work around this)
The "puush" command on line 104 (same story)
The "rm" command on line 132
The file does exist, and by replacing "eval" with "echo" and copying the resulting command it executes fine:

http://puu.sh/d4vpA/f372a6c3b6.png

Here is the script - I'm at a loss for ideas on what to try:

#!/bin/bash
##USER TUNABLES##

##Generic Settings
#Enable or disable notifications (both enabled by default)
audible_notification=true
visual_notification=true
#Timestamp to mark recordings with - to keep things organized
timestamp=$(date "+%m-%d-%y@%H%M")
#sizes can be anything find acepts. Maximum size puush accepts is 20M (default)
upload_cutoff="+20M"
#Below icons are able to be defined as absolute paths or icon names from the currently selected icon set (/usr/share/icons)
record_icon=gtk-media-record
stop_icon=gtk-media-stop
puush_icon=media-playlist-shuffle
#This sound should be an mp3 format sound blip
puush_sound="$HOME/.sounds/puush.mp3"
#Move all videos (true), or just those that don't get uploaded (false)
archive_all_videos=true

##Recording Settings (mostly self explanitory)
framerate=60
video_storage_dir="$HOME/Videos"
video_container="mp4"
video_codec="libx264"
video_codec_preset="ultrafast"
audio_codec="libmp3lame"
audio_channels="2"

#Alsa users: whatever recording device you wish, microphone or otherwise, you can also use snd-aloop to create a monitor
#Pulse users: you should be able to set this to pulse and then use pavucontrol (or whatever) to select a recording device
audio_device="hw:4,1" #Alsa users: whatever recording device you wish, microphone or otherwise, you can also use snd-aloop to create a monitor
audio_samplerate="44100"
audio_bitrate="128k"

#Limit video resolution to no more than current monitor size? (useful for strange fullscreen window behaviors) Default: true
limit_res=true


#Don't Touch these variables
friendlytitle=$(xprop -id `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | awk '{print $5}'` | grep "WM_CLASS(STRING)" | sed -e 's/.*= "\([^"]*\)".*/\1/')
filename="${friendlytitle}_on_${timestamp}.mp4"
maxresx=$(xdotool getdisplaygeometry | awk '{print $1}')
maxresy=$(xdotool getdisplaygeometry | awk '{print $2}')

if [ ! -f /tmp/.record ];then
	touch /tmp/.record

	#Below Snippet taken from:
	#http://ur1.ca/iu5rm
	# Get the coordinates of the active window's
	#    top-left corner, and the window's size.
	#    This excludes the window decoration.
	  unset x y w h
	  eval $(xwininfo -id $(xdotool getactivewindow) |
	    sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
	           -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
	           -e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
	           -e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
	#End Snippet

	if $limitres; then

		if [ "$w" -gt "$maxresx" ];then
			w="$maxresx"
		fi

		if [ "$h" -gt "$maxresy" ];then
			h="$maxresy"
		fi
	fi

	#Now that we know where the current window is, and how big it is
	#we can use this to record the window - but only if it doesn't move
	##FIXME - DOES NOT FOLLOW ACTIVE WINDOW##

	size="${w}x${h}"
	
	tmp_name="\"/tmp/${filename}\""

	ffmpeg_command=$(echo "ffmpeg -y -video_size $size -framerate $framerate -f x11grab -i $DISPLAY+$x,$y -f alsa -ac $audio_channels -i $audio_device -vcodec $video_codec -preset $video_codec_preset -acodec $audio_codec -ar $audio_samplerate -ab $audio_bitrate $tmp_name&")
	eval "$ffmpeg_command"

	FFMPEG_PID=$!

	if $visual_notification;then
		notify-send -i $record_icon "Started Recording"&
	fi

	while [ -f /tmp/.record ];do
		sleep 0.05
	done

	kill $FFMPEG_PID && wait  #kill ffmpeg, the .record file has been removed. wait for things to clean up
	puushed=false

	if [[ $(find $tmp_name -type f -size $upload_cutoff 2>/dev/null) ]] || $archive_all_videos; then
		cp_command=$(echo "cp '$tmp_name' '$video_storage_dir'")
		eval $cp_command&
	fi

	if [[ ! $(find $tmp_name -type f -size $upload_cutoff 2>/dev/null) ]]; then
		
		puush_command=$(echo "puush '$tmp_name' | grep http | tr -d '\n' | xclip -selection c.")
		eval $puush_command
		puushed=true
	fi

	if $visual_notification;then

		if $puushed; then
			notif_string='"Stopped Recording" "Uploading to Puush"'
		else
			notif_string='"Stopped Recording" "Archiving Video"'
		fi
		
		notif_command=$(echo "notify-send -i $stop_icon $notif_string")
		eval $notif_command&
	fi
	
	wait

	if $puushed;then
		if $visual_notification;then
			notify-send -i $puush_icon "Puush Complete!"&
		fi
		
		if $audible_notification;then
			mpg123 $puush_sound
		fi
	fi
	rm "$tmp_name"
else
		rm /tmp/.record
fi

Interestingly, the only way FFMPEG would work after adding all of the variables was to cast it to a variable and then eval the variable - not entirely certain why.

Edit: I have also diff'd the resulting string against one that has been copied and there is no difference. It just doesn't make any sense.



-- mod edit: read the Forum Etiquette and only post thumbnails http://wiki.archlinux.org/index.php/For … s_and_Code [jwr] --

Last edited by Xaero252 (2014-11-25 03:40:48)

Offline

#2 2014-11-25 07:51:09

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [BASH] rm, cp not working with variables in script

Hi Xaero252,

the problem is here

        tmp_name="\"/tmp/${filename}\""

the escaped quotes are treated as part of the file name, as you can see from the error messages in your screenshot.
You should change it to

        tmp_name="/tmp/${filename}"

About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#3 2014-11-25 10:04:44

Xaero252
Member
Registered: 2011-11-28
Posts: 107

Re: [BASH] rm, cp not working with variables in script

*FACEPALM*

I swear I tried it and needed to do that to prevent another error from firing, but apparently that was fixed along the way and the workaround was causing a problem.
Thank you for pointing out that I missed something simple lol.

EDIT:
Not quite; I was right, now the script appears to *not* work with spaces in $tmp_name
EDIT2: fixed it again, just had to put escaped quotes around $tmp_name in the ffmpeg command line.

Last edited by Xaero252 (2014-11-25 10:20:17)

Offline

Board footer

Powered by FluxBB