bash - 什么是 | tr -d 在 bash 中做什么?

标签 bash pipe

有人可以解释一下这段代码在做什么吗?

if [ -f "saved.txt" ]; then                // What does -f do?
    rm saved.txt
fi

in=$(echo "{query}" | tr -d "\\")          // How does this work? 
                                           // What does | tr -d "\\" mean?

echo "$in" > saved.txt                     // Is this simply putting the 
                                           // value of $in into saved.txt?

最佳答案

最初的 if 语句将测试该文件是否是常规文件。更多关于file test operators here .

此脚本将回显字符 {query} 并将其传送到命令 tr,该命令使用 -d 将删除那些字符被指定。 tr 代表翻译。在这种情况下,它需要一个 SET,并且根据手册页,如果您使用 \\,它将删除反斜杠。

结果存储在$in中。

最后将存储在in中的结果输出到saved.text中。

NAME tr - translate or delete characters

SYNOPSIS tr [OPTION]... SET1 [SET2]

DESCRIPTION Translate, squeeze, and/or delete characters from standard input, writing to standard output.

   -c, -C, --complement
          first complement SET1

   -d, --delete
          delete characters in SET1, do not translate

   -s, --squeeze-repeats
          replace  each  input  sequence  of  a repeated character that is listed in SET1 with a single occurrence of that
          character

   -t, --truncate-set1
          first truncate SET1 to length of SET2

   --help display this help and exit

   --version
          output version information and exit

   SETs are specified as strings of characters.  Most represent themselves.  Interpreted sequences are:

   \NNN   character with octal value NNN (1 to 3 octal digits)

   \\     backslash

关于bash - 什么是 | tr -d 在 bash 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22390167/

相关文章:

linux - 查找并删除所有不包含某些文件类型的目录

r - 管道结果到多个参数(使用 R 4.1+)

c - 如何从标准输入写入 C 中的管道

batch-file - 在 CALL 语句中传递来自 DOS 批处理脚本的管道字符

powershell - 从Write-Host到其他命令的管道输出

python - 为什么使用 sudo 运行脚本或使用 crontab 计划会导致错误?

bash - 用于阶乘计算的变量乘法

macos - 寻找 。并替换为 . & 新队 & ;在 OSX 中(对于新手来说)

arrays - 如何将数组与 `bash` 中的附加元素连接起来?

C++管道用多个程序修改输入文件