Unit 1 Question Bank
Unit 1 Question Bank
Ans: The default prompt symbol for the bash shell is the dollar sign ($). This symbol
indicates that the shell is waiting for you to enter text. You can configure the prompt
to provide basic information about your environment. The first example above
shows three pieces of information in the prompt: ■ The username that started the
shell ■ The current virtual console number ■ The current directory (the tilde sign is
shorthand for the home directory)
5. Explain in detail (any 6) ps command GNU parameters in Linux. Pg.
no 132
ans:
5. Explain the TAR command options in detail. Pg no. 121
Ans:
6. Explain the BSD-style parameters in detail. Pg. no 129
Ans:
ans: The bash shell uses a feature called environment variables to store information about
the shell session and the working environment. There are two types of environment
variables in the bash shell: ■ Global variables ■ Local variables
global variable: Global environment variables are visible from the shell session, and any
child processes that the shell spawns. Local variables are only available in the shell that
creates them. This makes global environment variables useful in applications that spawn
child processes that require information from the parent process. To view the global
environment variables, use the printenv command.
Local environment variables, as their name implies, can be seen only in the local process in
which they are defined. Don’t get confused though about local environment variables, they
are just as important as global environment variables. In fact, the Linux system also defines
standard local environment variables for you by default.
2. Explain in details the Linux Exit Status Codes with example. Pg. no
257
Linux provides the $? special variable that holds the exit status value from the last command
that executed. You must view or use the $? variable immediately after the command you
want to check. It changes values to the exit status of the last command executed by the shell
By convention, the exit status of a command that successfully completes is zero. If a
command completes with an error, then a positive integer value is placed in the exit status.
The invalid command returns an exit status of 127. An exit status value of 126 indicates that
the user didn’t have the proper permissions set to execute the command
Linux Exit Status Codes
0:Successful completion of the command
1: General unknown error
2:Misuse of shell command
126: The command can’t execute
127: Command not found
128: Invalid exit argument
130: Command terminated with Ctl-C
255: Exit status out of range
3. Explain the test Numeric Comparison & describe any one comparison
with example. Pg. no 268
ans: The most common method for using the test command is to perform a comparison of
two numeric values. The numeric test conditions can be used to evaluate both numbers and
variables. Here’s an example of doing that:
$ cat test5
#!/bin/bash
# using numeric test comparisons
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo "The test value $val1 is greater than 5"
fi
if [ $val1 -eq $val2 ]
then
echo "The values are equal"
else
echo "The values are different"
fi
File comparisons: These conditions give you the ability to check files in your filesystem
within your shell scripts The last category of test comparisons is quite possibly the most
powerful and most used comparisons in shell scripting. The test command allows you to test
the status of files and directories on the Linux filesystem. Checking directories:The -d test
checks if a specified filename exists as a directory on the system. Checking if an object exists
The -e comparison allows you to check if a file or directory object exists before you attempt
to use it in your script. Checking for a file: The -e comparison works for both files and
directories. To be sure that the object specified is a file, you must use the -f comparison.
Reading the file: Before trying to read data from a file, it’s usually a good idea to test if you
can read from the file first. This is done using the -r comparison. Checking if you can write
to a file: The -w comparison determines if you have permission to write to a file. Checking if
you can run a file: The -x comparison is a handy way to determine if you have execute
permission for a specific file.
Ans: One of the most useful features of shell scripts is the lowly back quote character,
usually called the backtick (`) in the Linux world. The backtick allows you to assign the
output of a shell command to a variable. While this doesn’t seem like much, it is a major
building block in script programming. The shell runs the command within the backticks,
and assigns the output to the variable testing. Example:
ans: the for loop operates on lists of items. It repeats a set of commands for every item in a
list.
Syntax
1. Create a file using a vi editor(or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh