什么做的tr -d在bash中做什么?

时间:2014-03-13 20:28:04

标签: 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?

2 个答案:

答案 0 :(得分:3)

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

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

结果存储在$in

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

  

NAME          tr - 翻译或删除字符

     

概要          tr [OPTION] ... SET1 [SET2]

     

说明          翻译,挤压和/或删除标准输入中的字符,写入标准输出。

   -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

答案 1 :(得分:0)

第一部分测试saved.txt是否存在,然后再尝试删除它。

第二部分将query(我假设输入错误,应该是${query},而不是{query})的内容复制到in,减去任何反斜杠。

第三部分,你是对的;它将in的值写入文件saved.txt