1

I have created a small script to move video files from a helmet mounted camera to my mythbuntu machine and there is a variable that for some reason will just not be passed onto the script. The variable in question is

d=`echo $file | awk '{ print $6 }'`

which is captured OK, however in the statement

scp -l 5000 $name $SavePath$d_$hour.AVI

will just not insert a value.

#!/bin/bash
# This script does copy files from /media/disk/DCIM/100DSCIM to 
# 192.20.0.200:/media/Elements/mythtv/videos/BikeRiding and renames the files using the time stamp
set -x
SavePath=root@198.20.0.200:/media/Elements/mythtv/videos/BikeRiding/
SourcePath=/media/disk/DCIM/100DSCIM/
ls $SourcePath*.AVI --full-time > file_list
cat file_list | while read file
do
    d=`echo $file | awk '{ print $6 }'`
    hour=`echo $file | awk '{ print $7 }'`
    name=`echo $file | awk '{ print $9 }'`
    scp -l 5000 $name $SavePath$d_$hour.AVI
done
13

This is because bash interprets variable name as d_. Use curly braces to border variable name in that case:

scp -l 5000 $name $SavePath${d}_$hour.AVI

or even

scp -l 5000 ${name} ${SavePath}${d}_${hour}.AVI

and it's much better to double quote variable to prevent error with name with strange symbols like space:

scp -l 5000 "${name}" "${SavePath}${d}_${hour}.AVI"
  • This answer is exactly right. But I wish I had a nickel for every time I've seen scripts using ${...} where it wasn't needed, most often in the apparent belief that ${foo} worked like "$foo". – dubiousjim Oct 19 '12 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.