【发布时间】:2013-12-21 15:47:37
【问题描述】:
我正在尝试编写一个脚本来交换文件中的文本:
sed s/foo/bar/g myFile.txt > myFile.txt.updated
mv myFile.txt.updated myFile.txt
我调用了 sed 程序,该程序交换了 myFile.txt 中的文本并将更改的文本行重定向到第二个文件。 mv 然后将 .updated txt 文件移动到 myFile.txt,覆盖它。该命令在 shell 中有效。
我写道:
#!/bin/sh
#First, I set up some descriptive variables for the arguments
initialString="$1"
shift
desiredChange="$1"
shift
document="$1"
#Then, I evoke sed on these (more readable) parameters
updatedDocument=`sed s/$initialString/$desiredChange/g $document`
#I want to make sure that was done properly
echo updated document is $updatedDocument
#then I move the output in to the new text document
mv $updatedDocument $document
我得到错误:
mv: 目标 `myFile.txt' 不是目录
我知道它认为我的新文件名是 sed 输出的字符串的第一个单词。我不知道如何纠正。从早上 7 点开始,我一直在尝试每条报价单,创建一个临时文件来将输出存储在(灾难性的结果)、IFS 中……到目前为止,一切都给了我越来越多的无用错误。我需要清醒一下,我需要你的帮助。我该如何解决这个问题?
【问题讨论】:
-
您似乎正在尝试运行您使用某些通配符模式作为输入文件编写的脚本。尝试使用一个文件作为文件名运行,看看会发生什么。
-
我敢打赌,当你双引号 all 你的变量时你会没事的。如果您发布了脚本的所有输入/参数和输出,将会很有用。
-
@user2719058,呃,不,你不好。