Bash Scripting Basics
8 Questions
1 Views

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 is the purpose of the first line in a shell script that starts with #!?

  • It indicates the version of the shell interpreter.
  • It initializes variables for the script.
  • It provides documentation for the script.
  • It specifies the path to the shell interpreter. (correct)

Which command is used to give execute permissions to a shell script file?

  • chmod +x shell_script_file (correct)
  • exec shell_script_file
  • setperm shell_script_file
  • run +x shell_script_file

What does the command 'echo $REPLY' do in a shell script?

  • It clears the terminal screen.
  • It displays the last input provided by the user. (correct)
  • It announces an error in the script.
  • It prints a predefined message.

In the shell script example using arrays, which command retrieves all elements of the array?

<p>echo ${CAR[*]} (C)</p> Signup and view all the answers

How is today's date obtained in a shell script?

<p>DATE=$(date) (D)</p> Signup and view all the answers

What happens when the command 'if rm test 2>/dev/null' is executed?

<p>The variable 'test' will be deleted if it exists. (D)</p> Signup and view all the answers

What is the output of 'echo "22=$(( 2$PERM ))"' if PERM is set to 2?

<p>2*2=4 (B)</p> Signup and view all the answers

What does the command 'clear' do in a shell script?

<p>It resets the terminal screen to a blank state. (D)</p> Signup and view all the answers

Flashcards

Shell Script

A file containing commands to be executed by a shell interpreter, typically bash. It allows for automating tasks.

Shebang Line

The first line of a shell script indicating the interpreter to use. It starts with "#!" followed by the interpreter's path.

chmod +x

A command that grants the script permission to be executed.

Variable Assignment

Assigning a string value to a variable. The variable name is followed by an equal sign and the value.

Signup and view all the flashcards

Array

A data structure that holds an ordered collection of values. Values can be accessed using their index.

Signup and view all the flashcards

User Input

Reading user input from the standard input (stdin) using the 'read' command and storing it in the special variable 'REPLY'.

Signup and view all the flashcards

Command Substitution

Using command substitution to capture the output of another command. The command is enclosed in backticks ("`").

Signup and view all the flashcards

Arithmetic Expansion

Expressions enclosed in double parentheses (( )) are evaluated arithmetically using bash's internal arithmetic engine.

Signup and view all the flashcards

Study Notes

Shell (bash) Scripts

  • Shell scripts are files containing shell commands.
  • The first line starts with #! followed by the interpreter (e.g., #!/bin/sh).
  • To run a script, grant execution permissions using chmod +x script_name.

Shell Script Examples (1)

  • Create a file s1.sh.
  • Add the shebang line (#!/bin/bash).
  • Add commands like clear, CAR="Golf is the best", echo "CAR:", echo CAR, echo '$CAR:', echo $CAR.

Shell Script Examples (2)

  • Create a file s2.sh.
  • Add the shebang line (#!/bin/bash).
  • Define an array CAR=(bmw audi toyota).
  • Use array indexing like echo "CAR[0]=${CAR[0]}".
  • Print array elements using echo "ALL - ${CAR[*]}", echo "Alternative ALL - ${CAR[@]}".

Shell Script Examples (3)

  • Create a file s3.sh.
  • Add the shebang line (#!/bin/bash).
  • Use echo -n to display a prompt for user input.
  • Use the read command to get user input.
  • Store the user input in the REPLY variable.
  • Display the input with echo "Your selected car - $REPLY".

Shell Script Examples (4)

  • Create a file s4.sh.
  • Add the shebang line (#!/bin/bash).
  • Clear the terminal using clear.
  • Use date to get the current date and format it using backticks (date).
  • Store the date in the variable DATE. Print the date using echo "Today's date = $DATE".
  • Get the number of users using who | wc -l
  • Store the number of users in USERS variable.
  • Display the number of users in the system. echo "Users in the system = $USERS".

Shell Script Examples (5)

  • Create a file s5.sh.
  • Add the shebang line (#!/bin/bash).
  • Use arithmetic expansion with shell variables for calculations, like echo "2*2=$((2*$PERM ))" and echo "((2*3+5)-4)/2=$((((2*3+5)-4)/2 ))".

Shell Script Examples (6)

  • Create a file s6.sh.
  • Add the shebang line (#!/bin/bash).
  • Use conditional statements (if, then, else, fi) to check whether a file exists.
  • Use rm test 2>/dev/null to remove "test" file without error messages.
  • Print echo "Deleted" if file deletion is successful.
  • Otherwise print echo "Not deleted".

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Bash Scripting PDF

Description

Explore the foundational concepts of bash scripting with this quiz. Learn about shebang lines, variables, and arrays, and how to execute scripts effectively. Test your knowledge with practical examples and commands used in creating shell scripts.

More Like This

Scripting and Shell Commands Quiz
20 questions
at & Localization Pop Quiz
9 questions

at & Localization Pop Quiz

GuiltlessAshcanSchool avatar
GuiltlessAshcanSchool
Shell Scripting Grundlagen
40 questions

Shell Scripting Grundlagen

RevolutionaryFern2779 avatar
RevolutionaryFern2779
Use Quizgecko on...
Browser
Browser