Domanda

Can someone walk through what this code is doing?

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?
È stato utile?

Soluzione

The initial if statement will test if the file is a regular file. More on file test operators here.

This script will echo the characters {query} and pipe it to the command tr, which with the -d will delete characters that are specified. tr stands for translate. In this case it takes a SET and per the man page, it will delete backslashes if you use \\.

The result is stored in $in.

Finally, the result stored in in will be outputted to 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

Altri suggerimenti

The first part tests if saved.txt exists before trying to remove it.

The second part copies the contents of query (I'm assuming a typo and that should be ${query}, not {query}) into in, minus any backslashes.

The third part, you are correct; it writes the value of in to the file saved.txt.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top