'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)&) ; )

Robert Michel news at robertmichel.de
Sat Jul 8 08:20:31 MST 2006


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
# 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

# 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

# EOF


 





More information about the asterisk-users mailing list