In: Computer Science
a) In no less than 100 words, describe several ways that an administrator could use the alias command to make their job easier.
b )In no less than 100 words, describe how you could use the "if" construct in administrating a Linux system.
c) Describe what stdout is? What is the default stdin? Describe stderr.
d) What file descriptor number represents stdout?
e) Describe redirection in BASH. Provide a command that uses redirection.
Please find the below answers, Please provide your feedback
Thanks and Happy Learning!
a) alias command in Unix/Linux allows a user to create simple alternative names or abbreviations for commands(A command is an instruction given to the computer to do something). These alternative names(alias names) can be of even a single character in length.
Below are some of the scenarios where alias command helps the system administartor in his daily work:
1. To setup simple,meaningful and short/easy to type names for the original commands or a group of commands. This saves a lot of time for the administrator in his daily jobs.
Example: Instead of typing the command 'ls -la' every time to list all the files(including the hidden files), administartor can setup an alias command like below so that when 'l' is typed it does the same functionality of the command 'ls -la'
alias l="ls -la"
2. Having aliases helps the administrator in avoiding common spelling mistakes while typing the commands.
3. Administartor can help to increasing the safety of the system by making some important commands (such as rm) interactive.
For example, the rm command deletes a given file, so to avoid user from removing a file by mistake, the administartor can set an alias for 'rm' command as follows so that the rm command prompts the user for confirmation before performing a delete.
alias rm="rm -i"
b) The if construct in Linux helps the user to take decision based on a particular condition. The system administartor can use the if construct in the scripts that is used by the administartor to automate some of thedaily activities. For example the administrator can setup a script to monitor a particular system process and make the script to send if it finds some error in the log file of that process. The pseudo code using 'if' construct for this scenario is give below
if [ there are any error in the log file ]
then
sendmail
else
doNothing and continue monitoring the process.
Another common scenario of using if construct is in the scripts which allow or deny some users/group of users from accessing some resources such as files or directories.
c) stdout is the standard stream used by the programs to write their output. The default stdout for a program in Linux system is the computer screen. Like everything else in a Linux system the stdout stream is also considered as a file to which the programs write their output. The below command in Linux system outputs the string to the stdout
echo "Hello World"
The file descriptor for the stdout is 1 in a Linux machine.
stdin is the standard stream used by the programs to read in the user input. The default stdin for a program in Linux is the keyboard. The below 'read' command reads the user input from the stdin(which is set as the keyboard by default) to a variable called as 'inputString'
read inputString
The stdin also considered as a file in a Linux machine, and the file descriptor for the stdin is set to zero (0).
stderr is the stream used by the programs to output the error messages. By default the stderr is set to the computer screen like stdout. The file descriptor for the stderr is 2 in a Linux machine.
d) File descriptor number for representing stdout is 1
e) Redirection in bash is used to redirect the output of a command from its default output stream to some other destination. For example the command 'ls -la' lists all the contsents of a directory and displays the result in the stdout(which is computer screen by default) . So if the user wants to redirect the output of the above command to a file then the user can use the redirection in bash. The symbol used for redirection in bash is '>'.
For example: To redirect the output of 'ls -la' command to a file called 'test.txt' the below command can be used
ls -la 1>test.txt
In the above command the one(1) is the file descriptor for thee stdout. So in short the above command redirects the output of the command form stdout (sith file descriptor 1) to a file named test.txt
similarly to redirect the output of a command from stderr to a file called test.txt below command can be used
ls -la 2>test.txt
In the above command the two(2) is the file descriptor for thee stderr.
Also one can use the symbol '>>' instead of '>' if the user wants to append the output to the end of a file. If the user uses the symbol '>' it overwrites the file. So in the case where the output file (test.txt) already has some content then using the symbol '>' redirection will make the old contents to be lost. But if the symbol '>>' is used for redirection then the new content will be appended to the end of the already existing content thus preserving the old content.
Example:
ls -la 1 >>test.txt