-3

I am a beginner to the unix scripts, can anyone please explain what does the below lines mean:

BTEQTEMPDELLOGS=$LOGS/${tablename}.DELlog

rm $BTEQTEMPDELLOGS 2>/dev/null 1>/dev/null
Ned64
  • 8,726

1 Answers1

1

The rm ("remove") command removes a file. The name of the file to be removed is given in a variable BTEQTEMPDELLOGS, instead of directly. Any error messages (2>) by rm are sent to /dev/null (thrown away), the same for normal output (1>).

The variable BTEQTEMPDELLOGS itself is constructed in your first line by concatinating the variable LOGS, a literal '/' and the variable tablename with the string ".DELlog" at the end.

Here is documentation on (input and) output redirection: GNU bash manual: Redirections.

In your example, only output is redirected, not input. 1> redirects the normal messages issued by rm (there usually are none), 2> redirects only the error messages (file not found etc.).

Here is more detailed information on these numbers ("file handles" from the programmer's point of view): stdin (0), stdout (1) and stderr (2) in-/output of programs.

Here is documentation on /dev/null: Wikipedia: NULL device

Ned64
  • 8,726
  • 1
    Additionally, since the $BTEQTEMPDELLOGS value is used unquoted, it will be split on whitespaces (spaces, tabs and newline by default) and each generated word will undergo filename globbing if they contain filename globbing characters (such as * or [ or ?, or backslash (in recent versions of bash)) before rm is called to delete the file corresponding to each word. – Kusalananda Aug 08 '19 at 17:39
  • Yes, an improvement of the code would be to use rm "$BTEQTEMPDELLOGS" 2>/dev/null 1>/dev/null. – Ned64 Aug 08 '19 at 17:40
  • 1
    Or just rm -- "$BTEQTEMPDELLOGS" 2>/dev/null, as rm never generates output, and you'd want to protect the value from being interpreted as a set of options, just in case it ($LOGS really) starts with a dash. – Kusalananda Aug 08 '19 at 17:41
  • @Kusalananda The variable seems to created by ourselves (lines above the snippet), not user input, so this might well be only a theoretical risk and may complicate discussion. – Ned64 Aug 08 '19 at 17:44
  • Possibly, but we don't actually know where $LOGS comes from. – Kusalananda Aug 08 '19 at 17:45
  • thanks for the reply .its helps in understanding ..i got to know the snippet which i sent .but $LOGS is the variable which holds the the path where logs to be stored.. rm filename 2>/dev/null and 1>/dev/null , if errors are there then it removes but what about inputs.(1>/dev/null) – chetan gowda Aug 09 '19 at 07:25
  • @chetangowda The input is not redirected. The rm command usually has no input (except when used interactively, e.g. rm -i .......). I am adding more information on the distinction between 1> and 2>). If it helped please accept the answer. – Ned64 Aug 09 '19 at 07:59