Podcast
Questions and Answers
What will be the output if the value of val is 50 in the nested if statement?
What will be the output if the value of val is 50 in the nested if statement?
Which boolean operation is used to check multiple conditions simultaneously in the provided examples?
Which boolean operation is used to check multiple conditions simultaneously in the provided examples?
In the if elif else structure, what will the output be if val equals 17?
In the if elif else structure, what will the output be if val equals 17?
What will the following for loop output: for i in {1..3}; do echo -n "$i "; done?
What will the following for loop output: for i in {1..3}; do echo -n "$i "; done?
Signup and view all the answers
What will be the output of the for loop that iterates from 0 to 10 with increments of 2?
What will be the output of the for loop that iterates from 0 to 10 with increments of 2?
Signup and view all the answers
What does the variable $HOSTNAME represent?
What does the variable $HOSTNAME represent?
Signup and view all the answers
Which variable would you check to find the shell's version number?
Which variable would you check to find the shell's version number?
Signup and view all the answers
What does the variable $CDPATH do?
What does the variable $CDPATH do?
Signup and view all the answers
Which variable shows the default timeout value for input during the read command?
Which variable shows the default timeout value for input during the read command?
Signup and view all the answers
What does the $PATH variable indicate?
What does the $PATH variable indicate?
Signup and view all the answers
Which variable indicates the current user's home directory?
Which variable indicates the current user's home directory?
Signup and view all the answers
What is the purpose of the $TERM variable?
What is the purpose of the $TERM variable?
Signup and view all the answers
Which command would you use to view the current value of $HISTSIZE?
Which command would you use to view the current value of $HISTSIZE?
Signup and view all the answers
What command is used to set the default text editor in the shell?
What command is used to set the default text editor in the shell?
Signup and view all the answers
Which operator checks if a string is empty in a shell script?
Which operator checks if a string is empty in a shell script?
Signup and view all the answers
How would you obtain the result of adding two integer variables in a shell script?
How would you obtain the result of adding two integer variables in a shell script?
Signup and view all the answers
In an if statement, which condition checks if a value is greater than 100?
In an if statement, which condition checks if a value is greater than 100?
Signup and view all the answers
What does the operator '-e FILE' check in a shell script?
What does the operator '-e FILE' check in a shell script?
Signup and view all the answers
Which command is used to display the current working directory in a shell script?
Which command is used to display the current working directory in a shell script?
Signup and view all the answers
How can you read multiple inputs into separate variables in a shell script?
How can you read multiple inputs into separate variables in a shell script?
Signup and view all the answers
What is the purpose of the command 'echo $DISPLAY' in the shell?
What is the purpose of the command 'echo $DISPLAY' in the shell?
Signup and view all the answers
Signup and view all the answers
Flashcards
BASH_VERSION
BASH_VERSION
Holds the version of the current bash instance.
HOSTNAME
HOSTNAME
The name of your computer.
CDPATH
CDPATH
The search path for the cd command.
HISTFILE
HISTFILE
Signup and view all the flashcards
HISTSIZE
HISTSIZE
Signup and view all the flashcards
HOME
HOME
Signup and view all the flashcards
IFS
IFS
Signup and view all the flashcards
PATH
PATH
Signup and view all the flashcards
export
export
Signup and view all the flashcards
SHELL
SHELL
Signup and view all the flashcards
DISPLAY
DISPLAY
Signup and view all the flashcards
expr command
expr command
Signup and view all the flashcards
if statements
if statements
Signup and view all the flashcards
-gt operator
-gt operator
Signup and view all the flashcards
-e FILE
-e FILE
Signup and view all the flashcards
-d FILE
-d FILE
Signup and view all the flashcards
Nested If Statements
Nested If Statements
Signup and view all the flashcards
If Else Statements
If Else Statements
Signup and view all the flashcards
If Elif Else Statements
If Elif Else Statements
Signup and view all the flashcards
Boolean Operations
Boolean Operations
Signup and view all the flashcards
For Loop
For Loop
Signup and view all the flashcards
Study Notes
Shell Programming
-
This presentation covers shell programming concepts.
-
It outlines user-defined and system-defined variables.
-
System variables are predefined by the shell and hold information about the system environment
-
Examples of system variables include:
$BASH_VERSION
: Holds the version of the bash shell.$HOSTNAME
: Displays the computer's name.$CDPATH
: Shows the search path forcd
command.$HISTFILE
: The file name for saving command history.$HISTFILESIZE
: The maximum number of lines in the command history file.$HISTSIZE
: The maximum number of commands remembered by shell history; default is 500.$HOME
: The home directory of the current user.$IFS
: The internal field separator. Default is , used for splitting strings.$LANG
: Locale category variable.$PATH
: Directory search path for shell commands.$PS1
: User's prompt setting.$TMOUT
: The shell timeout setting for input. A shell with a timeout setting will log out if no command is entered within the specified time limit.$TERM
: User's login terminal type.$SHELL
: Path to the shell.$DISPLAY
: X display name.$EDITOR
: Default text editor.
-
User-defined variables are created by the user for specific tasks.
-
Example of user defined variables:
a=50
assigns a value to the variable aread abc
: Takes user input for variableabc
.read -a name
: Takes array input for variablename
.- `echo "name[0]{name[0]} name[0]{name[1]} ${name[2]}": Displays array elements.
Expressions
- Using command substitution for expressions
- Example for adding numbers:
val=
expr a+a + a+b`
- Example for multiplying numbers:
val=
expr a∗a * a∗b`
- Example for adding integer/floating-point numbers:
val=
echo a+a + a+b | bc -l`
bc
: Basic calculator.
If Statements
- The
if
statement is used for conditional execution of commands. - Structure:
if [ <condition> ]
then
followed by the commands to execute if the condition is truefi
marks the end of theif
block
- Example: verifying if a value is greater than 100
if [ $val -gt 100 ]
then
- echo "Large number"
fi
Nested If Statements
- Using
if
statements within otherif
statements. - Example checking if a value is both less than 100 and even:
if [ $val -lt 100 ]
then
if [
expr $val % 2-eq 0 ]
then
- echo "Even and small number"
If-Else
- Executes different blocks of commands based on condition.
- Structure:
if [ <condition> ]
then
followed by commands if trueelse
followed by commands if falsefi
marks end
- Example: check if value is less than 100, display one message, otherwise display another
if [ $val -lt 100 ]
then
- echo "Small number"
else
- echo "Large number"
fi
If-Elif-Else
- Allows for multiple conditions.
- Structure: -
if [ <condition1> ]
then
followed by commands for condition1elif [ <condition2> ]
then
followed by commands for condition2
else
executed if no condition matchesfi
marks end
- Example: age-based eligibility check
if [ $val -gt 18 ]
then
echo "Voter"
elif [ $val -eq 17 ]
then
echo "Enroll for voter"
else
echo "Not eligible"fi
Boolean Operators
- Combining conditions using
&&
(and) and||
(or). if [ <condition1> && <condition2> ]
- Executions if both condition1 and condition2 are true.
if [ $val -lt 25 ] && [ $val -gt 20 ]
Looping Constructs
while loop
: Executes a block of code repeatedly as long as a condition is true.for loop
: Iterates over a list of items.until loop
: Repeats a block until a condition becomes true.
Case Statements
case <expression> in
pattern 1) <commands>;;
pattern 2) <commands>;;
esac
- Example: checks various states of a variable
case $x instart) <commands>;;stop) <commands>;;*) <commands>;;
Functions
- Reusable blocks of code.
function_name() { code; }
f1()
andf2()
: nested functions call example- Passing parameters using
$1
,$2
, etc. for arguments.
Assignment Questions
- Various programming tasks are given.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz explores key concepts in shell programming, focusing on user-defined and system-defined variables. Participants will learn about essential system variables, such as $BASH_VERSION
, $HOSTNAME
, and more, which hold crucial information about the shell environment. Develop your understanding of how these variables influence shell operations and commands.