【问题标题】:add double quotes to a variable为变量添加双引号
【发布时间】:2020-10-06 16:44:53
【问题描述】:

我有以下脚本,我需要将变量 $f 用双引号括起来,因为某些文件中有空格:

# create a reftime (for the last 24 hours)
((reftime=$(date '+%s')-(24*60*60)))
# create a reffile for that time in /var/tmp (or any other place)
reffile=/var/tmp/reffile.$$.tmp
touch --date=@${reftime} ${reffile}

# find all files (in srcbase), that was changed in the last 24h - that means
# now all files that are newer then our reffile, and copy to destbase...
srcbase=/myorigin
dest=gpadmin@10.0.1.8:/mydest/dest

# for all of these files we need the directory names (because we need the structure for copy)
chgfiles=/var/tmp/chgfile.$$.tmp
find ${srcbase} -type f -newer $reffile 2>/dev/null | sort -u >${chgfiles}

cat ${chgfiles} | while read f ; do
 # echo "copy file $f to destination $dest ..."
  # create directory structures if not exist
  #mkdir -p "${dest}"
  # and finaly cp the file
scp "$f" $dest
done

# clear all tmp files
rm ${reffile} ${chgfiles}

我尝试了scp "$f" $dest,但没有成功..

有什么想法吗?

谢谢!

【问题讨论】:

  • 谢谢,IFS 是什么?抱歉,我不熟悉这个术语,对脚本编写有点陌生
  • read -d '' f 应该与此处的IFS= 相同
  • 参见BashFAQ #20: How can I find and safely handle file names containing newlines, spaces or both? 此外,还有很多其他变量引用应该用双引号括起来。最好养成双引号 all 变量引用的习惯,除非有特定的理由不使用双引号。
  • 错误信息是什么?你能澄清“没用”吗?
  • 这不起作用:read -d '' f 它没有进入while

标签: linux bash variables double-quotes


【解决方案1】:

这行得通:

scp "${f}" $dest

它在我需要的文件周围添加了双引号。

我想我错过了 {}

【讨论】:

  • 你领先我一秒 :)
  • 大括号在这里完全没有区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多