'cpmv' instead of 'cp' Re: [asterisk-users] [tip]semicolon trouble: System($(sleep 4; cp 1.call out)&) not working, but System($( sleep 4 && cp 1.call out)&) ; )

Tzafrir Cohen tzafrir.cohen at xorcom.com
Sat Jul 8 10:14:53 MST 2006


On Sat, Jul 08, 2006 at 05:20:31PM +0200, Robert Michel wrote:
> Salve *!
> 
> Small improvement, I think it would be a good
> idea to use $RANDOM for the temporary file
> name - you could never known what it is
> running at the same time, especialy in case
> that someone would use this script not
> for asterisk callfiles  -
> remember my script is just a try ;)
> 
> #!/bin/bash

To simplify error handling:

set -e

> # cpmv
> # cp is not atomic and not reliable
> # for fast reading process e.g. inside
> # asterisk to store a copy to 
> # /var/spool/asterisk/outgoing
> # so cpmv will
> # copy a file to /tmp/ first and
> # move it then to your target.
> #
> # This solution does not work with
> # wildcards like '?' or '*'
> # ../name is also not working
> # s : source
> # t : target
> # f : file
> # p : path
> # $1 : source path/file
> # $2 : target path/file
> #
> # store it e.g. /usr/bin/cpmv
> # like BSD-licence, do what you like.
> # cpmv by news at robertmichel.de 2006-08-07
> 
> # for testing if a path is given
> sslash=$(echo $1 | sed 's/[^/]//g')
> tslash=$(echo $2 | sed 's/[^/]//g')
> 
> # extract filename from $1 $2
> sf=${1##*/}
> tf=${2##*/}
> sp=${1%/*}
> tp=${2%/*}
> 
> # echo sf $sf
> //# echo tf $tf
> # echo sp $sp
> # echo tp $tp
> 
> # exit when no sourcefile is given
> if [[ $sf = "" ]]; then
> # echo no given sourcefile
>   exit
> fi
> 
> # or exit when source=target
>   if [[ $sp$sf = $tp$tf ]]; then
> # echo source=target
>   exit
> fi
> 
> # extract pathnames from $1 $2
> # and add "/" at the end
> if [[ $sslash != "" ]]; then
>    sp=$(echo $sp/ )
> else
> # in case no source pathname is given use 'pwd'
>    sp=$(echo $(pwd)/)
> fi
> 
> if [[ $tslash != "" ]]; then
>    tp=$(echo $tp/ )
> else
>    tp=$sp
> fi
> 
> # echo 2sp $sp
> # echo 2tp $tp
> # echo 2sf $sf
> # echo 2tf $tf
> 
> # let us create a RANDOM Number for a uniq /tmp/ filename
> rnd=$RANDOM$RANDOM$RANDOM

use mktemp. Don't reimemment it...

tmpfile=`mktemp`

Note that the temp file has to be in the sme filesystem as the asterisk
spool

> 
> # now lets cp -p sourcefile to /tmp/
> # and move it then to targetpath 
> 
> if [[ $tf != "" ]]; then
>    cp -p $sp$sf /tmp/$sf.cpmv.$rnd; mv /tmp/$sf.cpmv.$rnd $tp$tf
>       else
>    cp -p $sp$sf /tmp/$sf.cpmv.$rnd; mv /tmp/$sf.cpmv.$rnd $tp$sf
> fi

-- 
Tzafrir Cohen      sip:tzafrir at local.xorcom.com
icq#16849755       iax:tzafrir at local.xorcom.com
+972-50-7952406           
tzafrir.cohen at xorcom.com  http://www.xorcom.com



More information about the asterisk-users mailing list