Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
See $VAR vs ${VAR} and to quote or not to quote –  Gilles Oct 19 '12 at 20:45

1 Answer 1

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"
share|improve this answer
    
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
    
Thanks very much, that has worked perfectly OK –  Inspector Gadget Oct 20 '12 at 21:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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