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 #!?
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?
What does the command 'echo $REPLY' do in a shell script?
What does the command 'echo $REPLY' do in a shell script?
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?
Signup and view all the answers
How is today's date obtained in a shell script?
How is today's date obtained in a shell script?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What does the command 'clear' do in a shell script?
What does the command 'clear' do in a shell script?
Signup and view all the answers
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.