壳牌 - 写变量内容到一个文件 [英] Shell - Write variable contents to a file

查看:115
本文介绍了壳牌 - 写变量内容到一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的内容复制一个变量(这里称为 VAR )到一个文件中。

I would like to copy the contents of a variable (here called var) into a file.

该文件的名称都存储在另一个变量 destfile

The name of the file is stored in another variable destfile.

我有这样的问题。以下是我已经试过:

I'm having problems doing this. Here's what I've tried:

cp $var $destfile

我也试过同样的事情dd命令......显然外壳认为 $ VAR 指的是一个目录,因此对我说,目录无法找到。

I've also tried the same thing with the dd command... Obviously the shell thought that $var was referring to a directory and so told me that the directory could not be found.

我如何解决此得到什么?

How do I get around this?

推荐答案

使用回声命令:

var="text to append";
destdir=/some/directory/path/filename

if [ -f "$destdir" ]
then 
    echo "$var" > "$destdir"
fi

这是 $ DESTDIR 重新presents一个文件中的如果测试。

The if tests that $destdir represents a file.

> 追加截断文件之后的文本。如果你只想在 $ VAR 来的文件中的文本追加现有内容,然后用>> 而不是

The > appends the text after truncating the file. If you only want to append the text in $var to the file existing contents, then use >> instead:

echo "$var" >> "$destdir"

CP 命令用于复制文件(文件)的,不写文本文件。

The cp command is used for copying files (to files), not for writing text to a file.

这篇关于壳牌 - 写变量内容到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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