Bash写入文件没有回声?

39 bash command-line io-redirection

作为练习,是否存在将字符串重定向到没有回声的文件的方法?目前我正在使用

echo "Hello world" > test.txt
Run Code Online (Sandbox Code Playgroud)

我知道catprintf.我在想类似的东西

> test.txt <<<"Hello world"
Run Code Online (Sandbox Code Playgroud)

当然这不起作用,但也许是一个类似的命令?

Eri*_*ric 56

您可以使用"cat"和here-document执行此操作.

cat <<EOF > test.txt
some text
EOF
Run Code Online (Sandbox Code Playgroud)

这样做的一个原因是为了避免在ps的输出中出现密码的可能性.但是,在bash和大多数现代shell中,"echo"是一个内置命令,它不会显示在ps输出中,所以使用这样的东西是安全的(当然,忽略了将密​​码存储在文件中的任何问题):

echo "$password" > test.txt
Run Code Online (Sandbox Code Playgroud)


小智 16

我遇到的问题是无法发送">"并最终收到回音!

echo "Hello world" | dd of=test.txt
Run Code Online (Sandbox Code Playgroud)

  • 如果您需要“sudo”写入某些受保护的文件,这会很有用。echo 上的 `sudo` 不起作用,我通常更喜欢 `echo some stuff | sudo tee /etc/some/protectedFile` 。我认为在这种情况下`| sudo dd of=/etc/protectedFile` 也可以工作。当心“dd”,当与“sudo”一起使用时,它可以写入块设备并擦除磁盘:) (2认同)

小智 8

有多种方法可以做到这一点,让我们运行这个名为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
Run Code Online (Sandbox Code Playgroud)

这是您应该看到的输出:

$ bash exercise.sh
Here is another here-string I can see and write to file
Run Code Online (Sandbox Code Playgroud)

文件应包含以下内容:

$ 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
Run Code Online (Sandbox Code Playgroud)


orm*_*aaj 6

有太多的方法可以讨论,但您可能并不关心。当然,您可以破解 - strace bash,或者在 gdb 中运行 Bash 进行各种黑魔法。

实际上你有两个完全不同的例子。<<<'string'已经将字符串写入文件。如果除了printfecho和之外的任何内容都可以接受cat,您可以使用许多其他命令来表现得像 cat (sed、awk、tee 等)。

$ cp /dev/stdin ./tmpfooblah <<<'hello world'; cat tmpfooblah
hello world
Run Code Online (Sandbox Code Playgroud)

或者地狱,取决于你如何编译 Bash。

$ enable -f /usr/lib/bash/print print; print 'hello world' >tmpfile
Run Code Online (Sandbox Code Playgroud)

如果您只想在纯 bash 中使用 bash 字符串和重定向,没有黑客攻击,也没有可加载文件,这是不可能的。然而在 ksh93 中,这是可能的。

 $ rm tmpfooblah; <<<'hello world' >tmpfooblah <##@(&!()); cat tmpfooblah
 hello world
Run Code Online (Sandbox Code Playgroud)


小智 5

The way to do this in bash is

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.

这就是为什么你应该只使用cat.

  • 这不是在 bash 中如何执行,这是在 zsh 中如何执行并让它通过 bash 执行。 (2认同)

Ign*_*ams 2

只有重定向不起作用,因为没有任何东西可以连接现在打开的文件描述符。所以不,没有这样的办法。