Basic Bash scripting¶
Lab Highlights:¶
- Lab Highlights:
- 01. echo
- 02. Receive Input from User
- 03. The If Statement
- 04. Using the AND Operator
- 05. Using the OR Operator
- 06. Switch statement {switch,case,esac}
- 07. Command Line Arguments
- 08. #!/bin/bash
- 09. Slicing Strings
- 10. Extracting Substrings Using Cut
- 11. Test File Existence
01. echo¶
- This script demonstrates the basic use of the
echocommand in Bash scripting. - The
echocommand is used to output text to the standard output (usually the terminal). -
Here, we’re simply printing the string “Hello World” to the screen.
What we’re doing:
- Displaying a simple message to the user.
How the script does it:
- The
echocommand takes the string argument and outputs it followed by a newline. - The shebang
#!/bin/bashtells the system to interpret this script with Bash.
02. Receive Input from User¶
- This script shows how to receive input from the user using the
readcommand. -
It prompts the user to enter something and then echoes back what they entered.
What we’re doing:- Interacting with the user by taking input and displaying it back.
How the script does it:
- The
echo -nprints the prompt without a newline, so the cursor stays on the same line. - The
readcommand waits for user input and stores it in the variablesomething. - Then,
echodisplays the variable’s value using$something.
03. The If Statement¶
- This script introduces conditional logic using the
ifstatement. -
It checks if a number entered by the user is greater than 10 and prints a message accordingly.
#!/bin/bash echo -n "Enter a number: " read num if [[ $num -gt 10 ]] then echo "Number is greater than 10." fiWhat we’re doing:
- Making a decision based on user input.
How the script does it:
- The
ifstatement evaluates the condition[[ $num -gt 10 ]], which uses double brackets for arithmetic comparison. -gtmeans “greater than”.- If true, it executes the
thenblock. - The
ficloses the if statement.
04. Using the AND Operator¶
- This script demonstrates the use of the logical AND operator (
&&) in an if statement. -
It checks if a number is less than 10 AND even, then classifies it as even or odd.
#!/bin/bash echo -n "Enter Number:" read num if [[ ( $num -lt 10 ) && ( $num%2 -eq 0 ) ]]; then echo "Even Number" else echo "Odd Number" fiWhat we’re doing:
- Checking multiple conditions simultaneously.
How the script does it:
- The condition uses
&&to combine two tests:$num -lt 10(less than 10) and$num%2 -eq 0(even). - The
%operator is used to check for evenness (remainder 0 when divided by 2). - The
-ltoperator checks if the number is less than 10. - The
-eqoperator checks for equality. - If both are true, it prints “Even Number”; otherwise, “Odd Number”.
- Note: The logic seems incorrect as it only checks evenness if less than 10, but the else covers all other cases.
05. Using the OR Operator¶
- This script is similar to the previous one but demonstrates the OR operator (
||). -
It checks if a number is less than 10 OR even.
#!/bin/bash echo -n "Enter Number:" read num if [[ ( $num -lt 10 ) || ( $num%2 -eq 0 ) ]]; then echo "Number is less than 10 OR Even" else echo "Number is greater than 10 AND Odd" fiWhat we’re doing:
- Checking if at least one of the conditions is true.
How the script does it:
- The condition uses
||to combine two tests. - If either
$num -lt 10is true OR$num%2 -eq 0is true, it executes thethenblock.
06. Switch statement¶
-
This script uses a
casestatement (switch-case equivalent in Bash) to match the input number against specific values and print corresponding messages.#!/bin/bash echo -n "Enter a number: " read num case $num in 100) echo "Hundred!!" ;; 200) echo "Double Hundred!!" ;; *) echo "Neither 100 nor 200" ;; esacWhat we’re doing:
- Handling multiple possible values for a variable with specific actions.
How the script does it:
- The
casestatement matches$numagainst patterns:100,200, or*(default). - Each match ends with
;;. - The
esaccloses the case block. - The
100and200cases print specific messages, while the*case handles all other inputs. - The user is prompted to enter a number, which is then evaluated by the case statement.
07. Command Line Arguments¶
- This script demonstrates how to access command-line arguments passed to the script.
- Run the script like
./script.sh arg1 arg2. - It prints the total number of arguments and the first two arguments.
-
The special variable
$#holds the number of arguments, while$1,$2, etc., access individual arguments.
What we’re doing:
- Displaying information about arguments provided when running the script.
How the script does it:
$#gives the number of arguments.$1,$2, etc., access the first, second, etc., arguments.- When run as
./script.sh arg1 arg2, it outputs:
08. #!/bin/bash¶
-
This script shows how to parse named command-line arguments (like X=5 Y=10) and perform operations on them.
#!/bin/bash for arg in "$@" do # This splits each argument on '=' and assigns values accordingly index=$(echo $arg | cut -f1 -d=) # Then we extract the value part val=$(echo $arg | cut -f2 -d=) # Look at the index and assign to variables case $index in X) x=$val;; Y) y=$val;; *) esac done # Sum the values of X and Y ((result=x+y)) # Print the result echo "X+Y=$result"What we’re doing:
- Parsing named arguments and calculating their sum.
How the script does it:
- Loops through all arguments (
$@),- splits each on
=usingcut, - assigns values to variables
xandybased on the index, - then performs arithmetic addition with
((result=x+y)).
- splits each on
- Run as
./script.sh X=5 Y=10.
09. Slicing Strings¶
-
This script demonstrates string slicing in Bash, extracting a substring from a given string.
#!/bin/bash Str="Learn Bash Commands from UbuntuPit" # Substring from position 0 to 20 (starting at 0 not 1) subStr=${Str:0:20} echo $subStrWhat we’re doing:
- Extracting a portion of a string.
How the script does it:
- Uses parameter expansion
${Str:0:20}to get 20 characters starting from position 0. - The result is “Learn Bash Commands”.
Str:indicates the starting index, and20is the length of the substring.- The output is printed using
echo. - Note: String indexing starts at 0 in Bash (Not from 1 !!!).
10. Extracting Substrings Using Cut¶
-
This script shows how to extract substrings using the
cutcommand, which is useful for parsing delimited text.#!/bin/bash Str="Learn Bash Commands from UbuntuPit" # Previous example was: subStr=${Str:0:20} # Now using cut to get first three words (words are space delimited) # The -d ' ' specifies space as the delimiter # The -f 1-3 specifies fields 1 to 3 subStr=$(echo $Str| cut -d ' ' -f 1-3) echo $subStrWhat we’re doing:
- Extracting specific fields from a string based on delimiters.
How the script does it:
- Pipes the string to
cut, which uses space (-d ' ') as delimiter and selects fields 1 through 3 (-f 1-3). - The output is “Learn Bash Commands”.
- Run as
./script.sh.
11. Test File Existence¶
- This script checks if a file exists using conditional statements and file test operators.
- It takes a filename as a command-line argument and verifies its existence.
-
The
-foperator checks if the file exists and is a regular file.#!/bin/bash filename=$1 if [ -f "$filename" ]; then echo "File exists" else echo "File does not exist" fiWhat we’re doing:
- Verifying the existence of a file provided as an argument.
How the script does it:
- Uses the
-ftest operator in anifstatement to check if the file exists and is a regular file. $1is the first command-line argument.- Run as
./script.sh myfile.txt.
12. Working with Variables¶
- This script introduces the concept of variables and demonstrates input from the user.
- It captures the user’s name and the current date, then displays a personalized greeting.
- It uses command substitution to get the current date.
- Variables are accessed by prefixing their names with a dollar sign (
$). -
$(...)is used for command substitution to assign the output of a command to a variable.#!/bin/bash # Define a variable named 'TODAY' and assign it the output of the 'date' command. # $(...) is a command substitution, which executes the command inside and # uses its output as the value for the variable. TODAY=$(date +"%A, %B %d, %Y") # Prompt the user for their name. echo "Hello! What is your name?" # 'read' is used to capture user input and store it in the variable 'USER_NAME'. read USER_NAME # Use the variables in the output. # To access a variable's value, you prefix its name with a dollar sign ($). echo "Welcome, $USER_NAME!" echo "Today is $TODAY." echo "Have a great day!"What we’re doing:
-
Introducing variables, user input, and command substitution.
Format Specifier Description Example Output (for the current date) %A Full weekday name. Saturday ”,” A literal comma and a space. ”, “ %B Full month name. December %d Day of the month as a two-digit number (01-31). 13 ”,” A literal comma and a space. ”, “ %Y Full year (four digits). 2025
How the script does it:
- Variables: Storing data (e.g.,
USER_NAME,TODAY). - Variable Access: Retrieving the value using the dollar sign (
$VARIABLE_NAME). - Command Substitution (
$(...)): Running a command and using its result as data. - read: Capturing input typed by the user.
- echo: Displaying messages that include variable values.
-
13. File ReaRename (Loop)¶
- This script introduces the
forloop, which is used to iterate over a list of items and perform an action on each one. - The script renames multiple files by appending “_processed” to their names.
- It checks if each file exists before renaming it to avoid errors.
- Positional parameters (
$@) are used to access all arguments passed to the script. - String manipulation techniques are used to construct the new filenames.
- The
mvcommand is used to rename the files. - Run the script like
./script.sh file1.txt file2.txt. -
The script processes each file provided as an argument.
#!/bin/bash # Check if any arguments were provided. # This ensures the script is used correctly. # Its assumes the user will provide filenames as arguments. # Its a check to prevent errors if no files are given. if [ "$#" -eq 0 ]; then echo "Usage: $0 <file1> <file2> ..." echo "Provide one or more filenames to add '_processed' to their name." exit 1 fi # The 'for' loop iterates over all positional arguments ($@ means all arguments). for FILENAME in "$@"; do # Check if the file actually exists before trying to rename it. if [ -f "$FILENAME" ]; then NEW_FILENAME="${FILENAME%.*}_processed.${FILENAME##*.}" # 'mv' command is used to move/rename files. mv "$FILENAME" "$NEW_FILENAME" echo "Renamed: '$FILENAME' -> '$NEW_FILENAME'" else echo "Skipping: File '$FILENAME' not found." fi done echo "Batch renaming complete."What we’re doing:
- Iterating over a list of files and renaming them.
How the script does it:
- for loop: Repeating a set of commands for every item in a list.
- Positional Parameters (
$@): Represents all arguments passed to the script. - File Test Operator (
-f): Checks if a path exists and is a regular file. - String Manipulation (Parameter Expansion):
${FILENAME%.*}: Removes the shortest match of.*(the extension) from the end.${FILENAME##*.}: Removes the longest match of*(the filename/path) from the beginning, leaving the extension.
- mv: The command used for moving/renaming files.