Bash 在没有回显的情况下写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11279335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash write to file without echo?
提问by Eric
As an exercise, does a method exist to redirect a string to a file without echo? Currently I am using
作为练习,是否存在将字符串重定向到没有回显的文件的方法?目前我正在使用
echo "Hello world" > test.txt
I know about cat
and printf
. I was thinking something like
我知道cat
和printf
。我在想像
> test.txt <<<"Hello world"
Of course this doesnt work, but maybe a similar command?
当然这不起作用,但也许是类似的命令?
回答by Eric
You can do this with "cat" and a here-document.
你可以用“cat”和一个here-document来做到这一点。
cat <<EOF > test.txt
some text
EOF
One reason for doing this would be to avoid any possibility of a password being visible in the output of ps. However, in bash and most modern shells, "echo" is a built-in command and it won't show up in ps output, so using something like this is safe (ignoring any issues with storing passwords in files, of course):
这样做的一个原因是为了避免密码在 ps 的输出中可见的任何可能性。然而,在 bash 和大多数现代 shell 中,“echo”是一个内置命令,它不会出现在 ps 输出中,所以使用这样的东西是安全的(当然,忽略在文件中存储密码的任何问题):
echo "$password" > test.txt
回答by guest
I had the problem not being able to send ">" and ended up with echo!
我遇到了无法发送“>”的问题,结果是回声!
echo "Hello world" | dd of=test.txt
回答by paulerickson
The way to do this in bash is
在 bash 中执行此操作的方法是
zsh <<< '> test <<< "Hello World!"'
zsh <<< '> test <<< "Hello World!"'
This is one of the interesting differences between zsh and bash: given an unchained >
or >>
, zsh has the good sense to hook it up to stdin, while bash does not. It would be downright useful - if it were only standard.
I tried to use this to send & append my ssh key over ssh to a remote authorized_keys
file, but the remote host was bash, of course, and quietly did nothing.
这是 zsh 和 bash 之间有趣的区别之一:给定一个未链接的>
or >>
, zsh 可以很好地将其连接到标准输入,而 bash 则不然。这将是非常有用的——如果它只是标准的话。我尝试使用它通过 ssh 将我的 ssh 密钥发送并附加到远程authorized_keys
文件,但远程主机当然是 bash,并且悄悄地什么也没做。
And that's why you should just use cat
.
这就是为什么你应该只使用cat
.
回答by David Burke
awk ' BEGIN { print "Hello, world" } ' > test.txt
would do it
会做的
回答by ormaaj
There are way too many ways to possibly discuss that you probably don't care about. You can hack of course - strace bash, or do all sorts of black magic running Bash in gdb.
有太多方法可以讨论,而您可能并不关心。你当然可以 hack - strace bash,或者在 gdb 中运行 Bash 做各种黑魔法。
You actually have two completely different examples there. <<<'string'
is already writing a string to a file. If anything is acceptable other than printf
, echo
, and cat
, you can use many other commands to behave like cat (sed, awk, tee, etc).
你实际上有两个完全不同的例子。<<<'string'
已经在将字符串写入文件。如果除了printf
, echo
, 和之外的任何东西都可以接受cat
,您可以使用许多其他命令来表现像 cat (sed、awk、tee 等)。
$ cp /dev/stdin ./tmpfooblah <<<'hello world'; cat tmpfooblah
hello world
Or hell, depending on how you've compiled Bash.
或者见鬼去吧,这取决于你编译 Bash 的方式。
$ enable -f /usr/lib/bash/print print; print 'hello world' >tmpfile
If you want to use only bash strings and redirection, in pure bash, with no hacking, and no loadables, it is not possible. In ksh93 however, it is possible.
如果您只想使用 bash 字符串和重定向,在纯 bash 中,没有黑客攻击,也没有可加载项,这是不可能的。然而,在 ksh93 中,这是可能的。
$ rm tmpfooblah; <<<'hello world' >tmpfooblah <##@(&!()); cat tmpfooblah
hello world
回答by sdkks
There are multiple ways to do it, let's run this script called exercise.sh
有多种方法可以做到,让我们运行这个名为 exercise.sh
#!/usr/bin/env bash
> file1.txt cat <<< "This is a here-string with random value $RANDOM"
# Or if you prefer to see what is happening and write to file as well
tee file2.txt <<< "Here is another here-string I can see and write to file"
# if you want to work multiline easily
cat <<EOF > file3.txt
You don't need to escape any quotes here, $ marks start of variables, unless escaped.
This is random value from variable $RANDOM
This is literal $RANDOM
EOF
# Let's say you have a variable with multiline text and you want to manipulate it
a="
1
2
3
33
"
# Assume I want to have lines containing "3". Instead of grep it can even be another script
a=$(echo "$a" | grep 3)
# Then you want to write this to a file, although here-string is fine,
# if you don't need single-liner command, prefer heredoc
# Herestring. (If it's single liner, variable needs to be quoted to preserve newlines)
> file4.txt cat <<< "$a"
# Heredoc
cat <<EOF > file5.txt
$a
EOF
This is the output you should see:
这是您应该看到的输出:
$ bash exercise.sh
Here is another here-string I can see and write to file
And files should contain these:
文件应该包含这些:
$ ls
exercise.sh file1.txt file2.txt file3.txt file4.txt file5.txt
$ cat file1.txt
This is a here-string with random value 20914
$ cat file2.txt
Here is another here-string I can see and write to file
$ cat file3.txt
You don't need to escape any quotes here, $ marks start of variables, unless escaped.
This is random value from variable 15899
This is literal $RANDOM
$ cat file4.txt
3
33
$ cat file5.txt
3
33
回答by Ivan Gut
I've a solution for bash purists.
我为 bash 纯粹主义者提供了解决方案。
The function 'define' helps us to assign a multiline value to a variable. This one takes one positional parameter: the variable name to assign the value.
函数 'define' 帮助我们为变量分配一个多行值。这需要一个位置参数:要赋值的变量名。
In the heredoc, optionally there're parameter expansions too!
在 heredoc 中,可选地也有参数扩展!
#!/bin/bash
define ()
{
IFS=$'\n' read -r -d ''
}
BUCH="Matth?us 1"
define TEXT<<EOT
Aus dem Buch: ${BUCH}
1 Buch des Geschlechts Jesu Christi, des Sohnes Davids, des Sohnes Abrahams.
2 Abraham zeugte Isaak; Isaak aber zeugte Jakob, Jakob aber zeugte Juda und seine Brüder;
3 Juda aber zeugte Phares und Zara von der Thamar; Phares aber zeugte Esrom, Esrom aber zeugte Aram,
4 Aram aber zeugte Aminadab, Aminadab aber zeugte Nahasson, Nahasson aber zeugte Salmon,
5 Salmon aber zeugte Boas von der Rahab; Boas aber zeugte Obed von der Ruth; Obed aber zeugte Isai,
6 Isai aber zeugte David, den K?nig. David aber zeugte Salomon von der, die Urias Weib gewesen;
EOT
define TEXTNOEXPAND<<"EOT" # or define TEXTNOEXPAND<<'EOT'
Aus dem Buch: ${BUCH}
1 Buch des Geschlechts Jesu Christi, des Sohnes Davids, des Sohnes Abrahams.
2 Abraham zeugte Isaak; Isaak aber zeugte Jakob, Jakob aber zeugte Juda und seine Brüder;
3 Juda aber zeugte Phares und Zara von der Thamar; Phares aber zeugte Esrom, Esrom aber zeugte Aram,
4 Aram aber zeugte Aminadab, Aminadab aber zeugte Nahasson, Nahasson aber zeugte Salmon,
5 Salmon aber zeugte Boas von der Rahab; Boas aber zeugte Obed von der Ruth; Obed aber zeugte Isai,
6 Isai aber zeugte David, den K?nig. David aber zeugte Salomon von der, die Urias Weib gewesen;
EOT
OUTFILE="/tmp/matth?us_eins"
# Create file
>"$OUTFILE"
# Write contents
{
printf "%s\n" "$TEXT"
printf "%s\n" "$TEXTNOEXPAND"
} >>"$OUTFILE"
Be lucky!
祝你好运!
回答by Ignacio Vazquez-Abrams
Only redirection won't work, since there's nothing to connect the now-open file descriptors. So no, there is no way like this.
只有重定向不起作用,因为没有任何东西可以连接现在打开的文件描述符。所以不,没有这样的方法。