Unit - 4 LSP
Unit - 4 LSP
Unit - 4 LSP
2. Execute with explicit interpreter: You can explicitly specify which interpreter
to use to execute the script. This is useful if the script is not executable or if
you want to use a different interpreter than the default one. For example:
$ bash my_script.sh
$ sh my_script.sh
$ python my_script.py
3. Add execute permission and run as command: You can add the execute
permission to the script and then call it like a regular command. For example:
$ chmod +x my_script.sh
$ my_script.sh
4. Source the script: You can source the script to run its commands in the current
shell environment. This is useful when you want to modify environment
variables or define functions that will be used by other commands. For
example:
$ source my_script.sh
$ . my_script.sh # same as above
5. Run in the background: You can run the script in the background by appending
"&" to the command. For example:
$ ./my_script.sh &
Each of these ways of executing a shell script has its own advantages and
disadvantages depending on the situation.
For example, if we have a shell script named "myscript.sh" that accepts two
arguments, we can access them using the $1 and $2 variables within the script.
The following is an example of how to use positional parameters in a shell
script:
#!/bin/bash
echo "The first argument is $1"
echo "The second argument is $2"
Then, when we run the script with the arguments "hello" and "world", we will
get the following output:
$ ./myscript.sh hello world
The first argument is hello
The second argument is world
In this example, the myfunc function defines a local variable myvar and
assigns it the value "local value". The echo statement within the function
displays the value of myvar as "local value".
Outside the function, a global variable myvar is defined and assigned the
value "global value". When the script is executed, the value of myvar is
displayed before and after the function call. The output will be:
Before function call: myvar is global value
Inside function: myvar is local value
After function call: myvar is global value
As you can see, the value of myvar inside the function is different from the
value of myvar outside the function. This is because the local keyword was
used to define a local variable within the function. Any changes made to the
value of the local variable within the function do not affect the value of the
global variable outside the function.
2. Environment variables: Environment variables are variables that are set and
maintained by the operating system or user session and can be used by
processes running within that session. These variables contain information
that the operating system and other programs can use to customize behavior
and execution.
Here are some commonly used environment variables in Linux:
• PATH: Contains a list of directories that the shell searches when a
command is entered. If the command is not found in the current directory,
the directories listed in the PATH variable are searched in order.
• HOME: Points to the current user's home directory. This is where a user's
files and directories are stored by default.
• USER: Contains the name of the current user.
• SHELL: Specifies the default shell for the current user.
• PWD: Contains the current working directory.
• TERM: Specifies the terminal type.
• DISPLAY: Specifies the X server display to use.
• EDITOR: Specifies the default editor to use.
• LANG: Specifies the language and locale settings for the current user.
These environment variables can be accessed and modified using various shell
commands and programming constructs.
Environment variables are typically defined in the .bashrc or .bash_profile
files.
3. Shell variables: Shell variables are variables that are created and maintained
by the shell itself. These variables are used to customize the behavior of the
shell and to store temporary data.
Some common shell variables are:
$HOME: This variable contains the path to the current user's home directory.
$PATH: This variable contains a list of directories that the shell will search
when you enter a command.
$USER: This variable contains the name of the current user.
$SHELL: This variable contains the path to the shell that is currently running.
$PWD: This variable contains the path to the current working directory.
You can also create your own shell variables by assigning a value to a variable
name. For example, MYVAR="Hello World" creates a variable named
MYVAR with the value "Hello World". You can then access the value of the
variable by prefixing the variable name with a $ symbol, such as $MYVAR.
Shell variables are local to the current shell process by default, meaning they
are not accessible by other processes. However, you can also export a variable
to make it available to child processes by using the export command. For
example, export MYVAR exports the MYVAR variable to child processes.
4. Readonly variables:
Readonly variables in shell refer to variables whose values cannot be changed
or modified once they are assigned. They are used to create constant values
that remain the same throughout the execution of the shell script. Readonly
variables are useful in preventing unintentional modification of critical
variables in a script.
The syntax to declare a readonly variable is as follows:
readonly variable=value
Once declared, the value of the variable cannot be changed using the =
operator or any other means. Attempts to modify the value of a readonly
variable will result in an error.
For example, consider the following script that uses a readonly variable:
#!/bin/bash
readonly PI=3.14159
echo "The value of PI is: $PI"
# Trying to modify the value of PI
PI=3
echo "The new value of PI is: $PI"
When executed, the above script will produce the following output:
The value of PI is: 3.14159
./test.sh: line 7: PI: readonly variable
As can be seen from the output, the value of PI is displayed correctly in the
first echo statement, but the attempt to modify its value results in an error
because it is a readonly variable.
Keyword Variables :
In shell programming, a keyword is a reserved word that has a specific meaning
and purpose within the context of the shell script. These keywords are part of the
shell's syntax and cannot be used as variable names or other identifiers in the
script.
Some of the commonly used keywords in shell programming are:
1. if: used to test a condition and execute a set of commands if the condition
is true
Syntax:
if [ condition ]
then
commands
fi
# or
function function_name {
commands
}
syntax:
unset variable
Command Line Arguments :
Command line arguments refer to the values passed to a shell script or program
when it is executed in the command line. They are used to specify options,
parameters, or input data for the script or program to process.
Command line arguments are stored in special variables called positional
parameters. The first argument is stored in $1, the second argument in $2, and so
on. $0 stores the name of the script or program itself.
For example, consider the following script named my_script.sh:
#!/bin/bash
echo "The name of the script is: $0"
echo "The first argument is: $1"
echo "The second argument is: $2"
If we run the script with the command ./my_script.sh foo bar, it will produce the
following output:
The name of the script is: ./my_script.sh
The first argument is: foo
The second argument is: bar
We can also use special parameters like $# to get the number of arguments passed
to the script, $@ to get all arguments as a list, and $* to get all arguments as a
single string.
For example:
#!/bin/bash
echo "Number of arguments: $#"
echo "All arguments as a list: $@"
echo "All arguments as a single string: $*"
If we run the script with the command ./my_script.sh foo bar baz, it will produce
the following output:
Number of arguments: 3
All arguments as a list: foo bar baz
All arguments as a single string: foo bar baz
You can also use command line arguments to control how your script is executed.
For example, the following code uses the -v option to print the value of the
$VERBOSE variable:
fi
In the above example, the script checks if the user running the script is the root
user using the $EUID environment variable. If the user is not the root user, an
error message is displayed and the script exits with an exit status of 1. If the user
is the root user, some operation is performed and the script exits with an exit
status of 0 (success).
Logical Operators for Conditional Execution:
In shell programming, logical operators are used for conditional execution of
commands. There are three logical operators commonly used:
1. AND (&&): The && operator is used to execute a command if the
previous command succeeds or returns a 0 exit status.
Syntax:
command1 && command2
Example:
ls file.txt && echo "File exists"
In the above example, the echo command will only be executed if the ls
command succeeds in finding the file.txt.
2. OR (||): The || operator is used to execute a command if the previous
command fails or returns a non-zero exit status.
Syntax:
command1 || command2
Example:
ls file.txt || echo "File not found"
In the above example, the echo command will only be executed if the ls
command fails in finding the file.txt.
3. NOT (!): The ! operator is used to negate the exit status of a command.
Syntax:
! command
Example:
! ls file.txt
In the above example, the ! operator will negate the exit status of the ls
command, i.e., if the file is not found, the exit status will be 0, and if the
file is found, the exit status will be non-zero.
The Set and Shift Command:
set:
The set command is a built-in command in the shell that allows you to set or
change the values of the positional parameters. It can also be used to set values
of shell options and positional parameters.
The syntax of the set command is as follows:
set [options] [arguments]
Some common options for the set command include:
-e: Exit immediately if a command exits with a non-zero status.
-u: Treat unset variables as an error.
-x: Print commands and their arguments as they are executed.
shift:
The shift command is another built-in command in the shell that allows you to
shift the positional parameters to the left. This means that the value of $2 becomes
the value of $1, the value of $3 becomes the value of $2, and so on. The syntax
of the shift command is as follows:
shift [n]
The optional argument n specifies the number of positions to shift. If n is not
specified, it defaults to 1.
Expanding NULL and UNSET Variables:
In shell scripting, it is common to encounter null or unset variables. In such cases,
the shell will try to expand the variable, and the behavior will depend on whether
the variable is null or unset.
A null variable is one that is defined, but has no value. In other words, it is an
empty variable. When a null variable is expanded, it is replaced with an empty
string. For example:
var=""
echo $var # Output: (nothing)
An unset variable, on the other hand, is one that has not been defined at all. When
an unset variable is expanded, it is replaced with an empty string as well, but the
shell also generates a warning. For example:
echo $var # Output: (nothing, plus a warning)
You can use the following syntax to expand a null or unset variable:
${parameter:-word}
Example:
//if color is not set then blue is used
echo "the sky is ${color:-"blue"}"
//Only print not assign to color
echo "the sky is ${color:="blue"}"
/print as well as assign it
color="blue"
$ echo "the sky is ${color:?"color not set"}"
the sky is blue
In shell scripting, we can also test whether a variable is null or unset using the -z
and -n operators. The -z operator returns true if the variable is null or empty,
while the -n operator returns true if the variable is not null or empty. For example:
var=""
if [ -z "$var" ]; then
echo "Variable is null or empty"
fi
if [ -n "$var" ]; then
echo "Variable is not null or empty"
fi
Here, the first if statement will execute because the variable var is null. The
second if statement will not execute because the variable is empty.
Functions:
In shell programming, a function is a block of code that can be defined once and
called multiple times throughout the script. Functions are useful for code reuse
and for keeping the main body of the script organized and readable.
To define a function in shell, the syntax is as follows:
function_name() {
# function code here
}
Here, function_name is the name of the function, and the code to be executed is
enclosed in curly braces.
You can then call the function by simply typing its name in your script:
function_name
Functions can take parameters, just like scripts. The parameters are passed to the
function in the same way as they are passed to scripts:
function_name() {
parameter1=$1
parameter2=$2
# function code goes here
}
function_name
echo $?
The above code will print 5 to the console, which is the value returned by the function.
Aliases:
In Linux, an alias is a way to create a short name for a command or a set of commands.
It helps to save time and typing effort by allowing users to execute long or complex
commands with a simple keyword.
Aliases are created using the alias command, followed by the short name (alias) and the
corresponding command(s) enclosed in quotes. The general syntax is as follows:
alias alias_name='command(s)'
For example, to create an alias for the ls -al command, we can use the following
command:
alias ll='ls -al'
Now, whenever we type ll in the terminal, it will be expanded to ls -al.
Aliases can be stored in the shell configuration files such as .bashrc or .bash_aliases.
These files are executed each time a new terminal is opened, and the aliases become
available.
Aliases can be listed using the alias command with no arguments, and they can be
removed using the unalias command followed by the alias name:
unalias alias_name
THE END
In case you want to know about me -