cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 600 Views

Red Hat Linux Interview Series 8

Level - L1 & L2

Q.) Explain the below special shell variables :  

 

$#   $?    $$   $@

 

 

Q.) What is the function of these redirections :

 

>/dev/null 
1>/dev/null 
2>/dev/null 
1>&2
2>&1

bonus item :
3>&1

 

 

Q.) Explain the logic in this shell script :

Chetan_Tiwary__0-1726834938222.png

 

 

I'll be posting a series of Linux-related questions covering various skill levels. Feel free to share your insights and expertise. Your contributions will benefit learners at all stages, from those in current roles to those preparing for Linux interviews.

5 Replies
Ad_astra
Flight Engineer Flight Engineer
Flight Engineer
  • 581 Views

Question 3:

The logic in the shell script is as follows:

The "$?" contains the return code from the previously executed bash command; the change directory command to "my_dir". 

If the directory exists and can be found by the command, assuming no permission or other errors, then the return code will be "0" (zero) and indicates the command executed successfully.

The $? contains the return code and the logic comparision of the if statement will match; effectively making the statement 'true'. The shell variable $? is wrapped in double quotes, as is the zero value. This means the comparision is treating each value as a string and not a numeric value. 

If the cd command fails, then the return code would be non zero; most likely a 1 if not found. The comparision would then fail.

Hope this makes sense! 

Please correct me if anything is incorrect or misleading.

Chetan_Tiwary_
Community Manager
Community Manager
  • 448 Views

Thanks @Ad_astra you did great!

Ad_astra
Flight Engineer Flight Engineer
Flight Engineer
  • 398 Views

Thank you!

Trevor
Starfighter Starfighter
Starfighter
  • 469 Views

Question:  What is the function of these redirections:

>/dev/null
- redirects standard output (file descriptor 1) to the device file named "null"

1>/dev/null
- same as ">/dev/null"
- file descriptor 1 refers to the stdout stream (file)

2>/dev/null
- redirects standard error (file descriptor 2) to the device file named "null"
- file descriptor 2 refers to the stderr stream (file)


1>&2
# Redirects stdout to stderr
# Merges output from stream 1 with stream 2
# messages intended for stdout get sent to same place as stderr
# redirect stdout (file descriptor 1 ) to stderr (file descriptor 2 ).
# temporarily connects stdout to the same "resource" as the shell's stderr.

2>&1
# Redirects stderr to stdout.
# Merges output from stream 2 with stream 1.
# Error messages get sent to same place as stdout.
# redirect stderr (file descriptor 2 ) to stdout (file descriptor 1 ).
# temporarily connects stderr to the same "resource" as the shell's stdout.

bonus item :
3>&1 
Creates a file descriptor number 3 that is a copy of file descriptor number 1 (stdout);
       that is, file descriptor 3 is a duplicate of file descriptor 1

 

Additional information:
* Whenever a program is executed at the terminal, 3 files are generated:
- standard input(0)
- standard output(1)
- standard error(2)
These files are always created whenever a program is run. By default, an error stream
is displayed on the screen.

* There is a stdin (0), stdout (1), and a stderr (2) associated with each command.

* A command expects the first three file descriptors to be available.
- The first, fd 0 (standard input, stdin), is for reading.
- The other two (fd 1, stdout and fd 2, stderr) are for writing.

/dev/null
- a special device file
- a character device file
- will discard anything written to it
- a pseudo-device file that is created during the system's boot process.
- It has a size of 0 bytes, and occupies no blocks on the storage device

Generic syntax:       i>&j
# Redirects file descriptor i to j.
# All output of the file pointed to by i gets sent to the file pointed to by j.

Trevor "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 446 Views

@Trevor wonderful explanation!

Join the discussion
You must log in to join this conversation.