在 shell 脚本中重定向 [英] redirecting in a shell script

查看:57
本文介绍了在 shell 脚本中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来交换文件中的文本:

I'm trying to write a script to swap out text in a file:

sed s/foo/bar/g myFile.txt > myFile.txt.updated
mv myFile.txt.updated myFile.txt

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 中有效.

I evoke the sed program, which swaps out text in myFile.txt and redirects the changed lines of text to a second file. mv then moves .updated txt file to myFile.txt, overwriting it. That command works in the 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' 不是目录

mv: target `myFile.txt' is not a directory

我知道它认为我的新文件的名称是 sed 输出的字符串的第一个单词.我不知道如何纠正.从早上 7 点开始,我一直在尝试每个报价,创建一个临时文件来存储输出(灾难性的结果),IFS...到目前为止的一切都给了我越来越多的无益错误.我需要清醒一下,我需要你的帮助.我该如何解决这个问题?

I understand that it thinks my new file's name is the first word of the string that was sed's output. I don't know how to correct that. I've been trying since 7am and every quotation, creating a temporary file to store the output in (disastrous results), IFS...everything so far gives me more and more unhelpful errors. I need to clear my head and I need your help. How can I fix this?

推荐答案

也许试试

echo $updatedDocument > $document

这篇关于在 shell 脚本中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆