Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix管道和重定向_Unix_Redirect_Tr_Piping - Fatal编程技术网

Unix管道和重定向

Unix管道和重定向,unix,redirect,tr,piping,Unix,Redirect,Tr,Piping,假设您想读取输入文件“passwords.txt”,则更改 每个大写字母对应一个小写字母,每个“$”对应一个“#”, 然后将结果保存回同一个文件(覆盖它)。怎么 您是否可以使用tr命令执行此操作?提示:使用管道和文件 重定向 我试过这两个命令,但都不起作用。我认为该文件被系统截断了,因此passwords.txt在删除后变为空 cat passwords.txt | tr'[A-Z'!']''[A-Z'#'] tr'[A-Z”!“]'[A-Z”#“]'passwords.txt 我知道我们可以将

假设您想读取输入文件“passwords.txt”,则更改 每个大写字母对应一个小写字母,每个“$”对应一个“#”, 然后将结果保存回同一个文件(覆盖它)。怎么 您是否可以使用tr命令执行此操作?提示:使用管道和文件 重定向

我试过这两个命令,但都不起作用。我认为该文件被系统截断了,因此passwords.txt在删除后变为空

cat passwords.txt | tr'[A-Z'!']''[A-Z'#']

tr'[A-Z”!“]'[A-Z”#“]'passwords.txt

我知道我们可以将其写入临时文件,然后使用“&&”将临时文件名更改为passwords.txt。然而,这是一个家庭作业问题,我们还没有了解到“&&”,所以我相信一定有另一种方法可以做到这一点,只需要管道和重定向

我认为文件被系统截断了,所以 passwords.txt在之后变为空

你说得对。您需要将
tr
输出重定向到临时文件,并将临时文件移动到原始文件

此外,在
周围使用引号
(我假设它是
$
)而
#
是多余的。你可以说:

cat /etc/passwd | tr 'A-Z$' 'a-z#' > temp && mv temp /etc/passwd
或者通过使用间接方式消除对
cat
的无用使用:

tr 'A-Z$' 'a-z#' < /etc/passwd > temp && mv temp /etc/passwd

然后,当且仅当command1返回零的退出状态,即command1成功时,才执行command2。

这里是另一种不使用
&&
的方法

echo "$(tr '[A-Z$]' '[a-z#]' < sample)" > sample
echo“$(tr'[A-Z$]'[A-Z#]'sample
测试

sat:~# cat > sample
sample TEXT
$test
sat:~# 
sat:~# echo "$(tr '[A-Z$]' '[a-z#]' < sample)" > sample
sat:~# cat sample
sample text
#test
sat:~# 
sat:~#cat>示例
示例文本
$test
星期六:~#
sat:~#echo“$(tr'[A-Z$]'[A-Z#]'sample
sat:~#cat样本
示例文本
#试验
星期六:~#
sat:~# cat > sample
sample TEXT
$test
sat:~# 
sat:~# echo "$(tr '[A-Z$]' '[a-z#]' < sample)" > sample
sat:~# cat sample
sample text
#test
sat:~#