Shell Programming Concepts

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be the output if the value of val is 50 in the nested if statement?

  • That's a small number (correct)
  • That's a large number
  • Even number
  • Not eligible

Which boolean operation is used to check multiple conditions simultaneously in the provided examples?

  • xor (^)
  • and (&&) (correct)
  • or (||)
  • not (!)

In the if elif else structure, what will the output be if val equals 17?

  • You are a minor
  • You may enroll for a voter (correct)
  • You are not eligible
  • You are a voter

What will the following for loop output: for i in {1..3}; do echo -n "$i "; done?

<p>1 2 3 (B)</p> 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?

<p>Welcome 0 times Welcome 2 times Welcome 4 times Welcome 6 times Welcome 8 times Welcome 10 times (C)</p> Signup and view all the answers

What does the variable $HOSTNAME represent?

<p>The name of the current computer (C)</p> Signup and view all the answers

Which variable would you check to find the shell's version number?

<p>$BASH_VERSION (B)</p> Signup and view all the answers

What does the variable $CDPATH do?

<p>Specifies the search path for the cd command (A)</p> Signup and view all the answers

Which variable shows the default timeout value for input during the read command?

<p>$TMOUT (C)</p> Signup and view all the answers

What does the $PATH variable indicate?

<p>The directories to look for executable commands (A)</p> Signup and view all the answers

Which variable indicates the current user's home directory?

<p>$HOME (D)</p> Signup and view all the answers

What is the purpose of the $TERM variable?

<p>Specifies the current terminal type being used (A)</p> Signup and view all the answers

Which command would you use to view the current value of $HISTSIZE?

<p>echo $HISTSIZE (D)</p> Signup and view all the answers

What command is used to set the default text editor in the shell?

<p>export EDITOR=/usr/bin/vim (D)</p> Signup and view all the answers

Which operator checks if a string is empty in a shell script?

<p>-z STRING (D)</p> Signup and view all the answers

How would you obtain the result of adding two integer variables in a shell script?

<p>Val=<code>expr $a + $b</code> (A)</p> Signup and view all the answers

In an if statement, which condition checks if a value is greater than 100?

<p>if [ $val -gt 100 ] (D)</p> Signup and view all the answers

What does the operator '-e FILE' check in a shell script?

<p>FILE exists (B)</p> Signup and view all the answers

Which command is used to display the current working directory in a shell script?

<p>pwd (B)</p> Signup and view all the answers

How can you read multiple inputs into separate variables in a shell script?

<p>read a b c (D)</p> Signup and view all the answers

What is the purpose of the command 'echo $DISPLAY' in the shell?

<p>Print the name of the X display (C)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

BASH_VERSION

Holds the version of the current bash instance.

HOSTNAME

The name of your computer.

CDPATH

The search path for the cd command.

HISTFILE

The name of the file where command history is saved.

Signup and view all the flashcards

HISTSIZE

Number of commands to remember in command history.

Signup and view all the flashcards

HOME

The home directory of the current user.

Signup and view all the flashcards

IFS

The Internal Field Separator used for word splitting.

Signup and view all the flashcards

PATH

A colon-separated list of directories for command search.

Signup and view all the flashcards

export

A command to set environment variables in the shell.

Signup and view all the flashcards

SHELL

The default login shell for the user, specifies the command interpreter.

Signup and view all the flashcards

DISPLAY

Environment variable that defines the X server display to use.

Signup and view all the flashcards

expr command

Used to evaluate expressions and perform integer arithmetic.

Signup and view all the flashcards

if statements

Used to execute commands based on conditions being true or false.

Signup and view all the flashcards

-gt operator

Tests if one integer is greater than another.

Signup and view all the flashcards

-e FILE

Checks if a given file exists regardless of its type.

Signup and view all the flashcards

-d FILE

Tests if a file is a directory.

Signup and view all the flashcards

Nested If Statements

If statements within another if statement, for multiple conditions.

Signup and view all the flashcards

If Else Statements

A control structure where one action occurs if a condition is true and another if it's false.

Signup and view all the flashcards

If Elif Else Statements

Extends if else with additional conditions using elif for multiple scenarios.

Signup and view all the flashcards

Boolean Operations

Combining conditions using logical operators like AND (&&) and OR (||).

Signup and view all the flashcards

For Loop

A loop that iterates over a sequence or range, repeating actions for each item.

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 for cd 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 a
    • read abc: Takes user input for variable abc.
    • read -a name: Takes array input for variable name.
    • `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 true
    • fi marks the end of the if 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 other if 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 true
    • else followed by commands if false
    • fi 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 condition1
    • elif [ <condition2> ]
      • then followed by commands for condition2
    • else executed if no condition matches
    • fi 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() and f2(): 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.

Quiz Team

Related Documents

Shell Programming Notes PDF

More Like This

Use Quizgecko on...
Browser
Browser