Podcast
Questions and Answers
What is the purpose of the first line in a shell script that starts with #!?
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?
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?
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?
In the shell script example using arrays, which command retrieves all elements of the array?
How is today's date obtained in a shell script?
How is today's date obtained in a shell script?
What happens when the command 'if rm test 2>/dev/null' is executed?
What happens when the command 'if rm test 2>/dev/null' is executed?
What is the output of 'echo "22=$(( 2$PERM ))"' if PERM is set to 2?
What is the output of 'echo "22=$(( 2$PERM ))"' if PERM is set to 2?
What does the command 'clear' do in a shell script?
What does the command 'clear' do in a shell script?
Flashcards
Shell Script
Shell Script
A file containing commands to be executed by a shell interpreter, typically bash. It allows for automating tasks.
Shebang Line
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
chmod +x
A command that grants the script permission to be executed.
Variable Assignment
Variable Assignment
Signup and view all the flashcards
Array
Array
Signup and view all the flashcards
User Input
User Input
Signup and view all the flashcards
Command Substitution
Command Substitution
Signup and view all the flashcards
Arithmetic Expansion
Arithmetic Expansion
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 usingecho "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 ))"
andecho "((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.
Related Documents
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.