0% found this document useful (0 votes)
31 views83 pages

8.) Bash Shell-2jdudhdbg

The document provides an overview of Bash shell scripting, covering file commands, arithmetic and logical operators, pipes, command-line arguments, redirection, and cron job scheduling. It details various operators and their usage, including file test operators, arithmetic methods, and how to handle command-line arguments. Additionally, it explains the syntax and components of cron jobs for automating tasks in Linux.

Uploaded by

czaetvwp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views83 pages

8.) Bash Shell-2jdudhdbg

The document provides an overview of Bash shell scripting, covering file commands, arithmetic and logical operators, pipes, command-line arguments, redirection, and cron job scheduling. It details various operators and their usage, including file test operators, arithmetic methods, and how to handle command-line arguments. Additionally, it explains the syntax and components of cron jobs for automating tasks in Linux.

Uploaded by

czaetvwp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Bash Shell Script

Course Instructor: Debjani Ghosh


File Command

• In the Bash shell, the file command and


various file test operators are used to
examine and manipulate files in shell
scripts
• Here's a list of the most commonly used
file test options, which can help you in
writing shell scripts:
• File Test Operators: These are
used to test various properties of
files (e.g., whether they exist, are
directories, are readable, etc.).
File Command
Arithmetic Operators
• Mathematical, relational, and logical operators are frequently used for performing calculations,
comparing values, and controlling flow.
• Arithmetic operations are typically performed using the (( )) syntax or the let command.

Arithmetic Operators
Arithmetic Operators
Relational • These operators compare two values. The comparison is
done inside [ ] or [[ ]] for strings and (( )) for
(Comparison) integers.
Operators
Relational (Comparison) Operators
Logical
Operators
• Logical operators are used
to combine multiple
conditions in conditional
expressions. These are
typically used with if,
while, until, or inside [
], [[ ]], or (( )).
• Bitwise operators perform bitwise calculations on
Bitwise integers. They are typically used in (( )) arithmetic
contexts.
Operators
Bitwise Operator
• These operators are used to assign values to variables.
Assignment Operators
Conditional (Ternary) Operator
• Although Bash doesn't have a traditional ternary operator, you can simulate it with expr or
within (( ))
Double [[ ]] and Single Brackets [ ]
• Key Differences and When to Use Each
1. Compatibility:
▪ Use [ ] if you need POSIX compatibility or are working in a script that needs to run on a
variety of Unix-like systems.
▪ Use [[ ]] if you are writing a script specifically for Bash and want to take advantage of
its extended features.
2.Logical Operators:
▪ Use [ ] for simple logical conditions with -a and -o.
▪ Use [[ ]] for more complex logical conditions with && and ||.
3.String Comparisons:
▪ Use [ ] for basic string comparisons.
▪ Use [[ ]] for advanced string comparisons and pattern matching
Double [[ ]] and Single Brackets [ ]

4. Pattern Matching:
o Use [ ] for simple comparisons.
o Use [[ ]] if you need pattern matching with =~ (regular expressions).
5. Error Handling:
o Use [ ] if you prefer traditional, more portable scripting.
o Use [[ ]] for enhanced functionality and easier syntax, but remember it's
not POSIX compliant.
• Note:
o Single Brackets [ ]: Use for basic, POSIX-compliant scripting where
portability is important.
o Double Brackets [[ ]]: Use for more advanced Bash scripting where
additional features and flexibility are beneficial.
For Writing Mathematical Expression in Bash
Shell
• Bash doesn't directly support floating-point arithmetic,
• It provides various methods to handle integer arithmetic.
• For more advanced arithmetic operations, external tools like
o bc and awk can be used to handle both integer and floating-point numbers.
• To write and evaluate mathematical expressions in Bash:

1. Using let Command 4. Using bc for Floating-Point Arithmetic


2. Using (( )) for Arithmetic
Expansion 5. Using awk for Arithmetic
and Floating-Point Operations
3. Using expr Command
Using let
Command
• Used to perform
arithmetic operations on
integer variables.
• It allows you to evaluate
arithmetic expressions
directly and update
variables.
• The (( )) construct is commonly used for integer arithmetic in Bash.
Using (( )) for • It evaluates the arithmetic expression inside it and can also be used in
Arithmetic conditions.
• You can perform complex arithmetic within it.
Expansion
• The (( )) construct is commonly used for integer arithmetic in Bash.
Using (( )) for • It evaluates the arithmetic expression inside it and can also be used in
Arithmetic conditions.
• You can perform complex arithmetic within it.
Expansion
Using expr • Evaluate basic arithmetic expressions involving integers.
• It requires spaces between operators and operands.
Command
Using bc for • Bash doesn’t handle floating-point arithmetic natively
• You need an external tool like bc (Basic Calculator).
Floating-Point o bc is a powerful tool that supports floating-point
calculations and more advanced math.
Arithmetic o bc is invoked by piping expressions to it via echo
Using bc for • You can specify the number of decimal places using the
Floating-Point scale parameter.
Arithmetic
• awk is another external tool that can be used for both integer and
Using awk for floating-point calculations.
Arithmetic and • It provides a broader set of features, including pattern matching and
Floating-Point text processing
Operations • awk uses BEGIN {} to evaluate expressions before processing input.
Combining Mathematical Expressions in Conditional Statements
Combining Mathematical Expressions in Conditional Statements
Using Variables Inside Arithmetic Expressions
Summary of Arithmetic Methods in Bash:
Pipes in Bash Shell Scripting
• The pipe (|) in Bash is a powerful operator used to redirect the
output of one command as the input to another command.
• This allows for the chaining of multiple commands, where the output
of the first command can be processed by subsequent commands.
• Pipes are essential for building complex command sequences in Bash
scripts and the command line.
Pipes in Bash Shell Scripting
• List files and count lines:
Basic Example of Pipe • The below command lists all files in the current
directory using ls and counts the number of
Usage lines (files) using wc -l.
• Search and filter content:
Basic Example of Pipe • Using cat to display the contents of a file and
grep to search for specific patterns.
Usage
• Filter processes:
Basic Example of Pipe • Listing all running processes and filtering them
with grep to find a specific one.
Usage
• The below command shows all processes that
include "bash" in their name or description.
• Extract specific columns of data from file
Basic Example of Pipe
Usage
To Do
• Sort the input.
• Remove duplicate lines.
• Count words, lines, characters for a file
Advanced Pipe Usage Examples
• Finding large files in a directory:
o This command lists all files in the current directory using ls -l, sorts them by size,
and shows the largest files.

• Breakdown:
o ls -l: List files with details.
o sort -k5 -n: Sort by the fifth column (file size) in numerical order.
o tail -n 5: Display the last 5 entries (largest files).
Advanced Pipe Usage Examples
• Display network connections:
o Using netstat to display network connections and grep to filter
connections using the HTTP port (80).

• This shows all active network connections using port 80.


Advanced Pipe Usage Examples
• Monitor system memory:
o Using free to check memory usage and filtering only the relevant line for
available memory.

• The -h option provides human-readable memory sizes, and grep


"Mem" filters out only the line containing memory usage.
Advanced Pipe Usage Examples
• Log Analysis:
o Extract and analyze error messages from a log file.

• Breakdown:
o cat /var/log/syslog: Display the system log.
o grep "error": Filter lines containing "error".
o sort | uniq -c: Count and display unique error messages.
o sort -nr: Sort the errors by frequency, in descending order.
Error Handling in Pipes
• In Bash, pipes only capture the exit status of the last command in the
chain.
• However, sometimes you may want to know if any command in the pipe
failed.
• Use set -o pipefail to catch errors in any command within a pipe:

• If any of the commands in the pipeline fail, the pipeline as a whole will fail,
and you can handle the error accordingly.
1. Used to pass information to a script when it is executed.
Command line 2. Bash provides a set of special symbols to handle these command-
line arguments.
arguments in Bash 1. $0 Represents the name of the script itself (the command
used to invoke the script).
Script • When the below script executed as ./myscript.sh, the output
will be:
• The script name is: ./myscript.sh
2. $1, $2, $3, …
• Description: These represent the positional parameters.
$1 is the first argument, $2 is the second argument, and
Command line so on.
arguments in Bash • If the below script is executed as ./myscript.sh arg1
arg2, the output will be:
Script First argument: arg1
Second argument: arg2
3. $#
Command line • Description: Represents the total number of
command-line arguments passed to the script.
arguments in Bash
• If the below script is executed as ./myscript.sh arg1
Script arg2 arg3, the output will be:
• Total number of arguments: 3
5. $*
Command line • Description: Expands to all the arguments passed
arguments in Bash to the script as a single word. Unlike $@, $* treats all
arguments as one string.
Script • If the below script is executed as ./myscript.sh arg1
arg2 arg3, the output will be:
All arguments as a single string: arg1 arg2 arg3
$@ vs $*
6. $?
Command line • Description: Represents the exit status of the last
arguments in Bash executed command. If the previous command was
successful, $? will be 0. If the command failed, $? will
Script be a non-zero value.
• If the below script is executed and the ls command fails, the
output will be something like:
Exit status: 2 (or another non-zero value).
7. $$
Command line • Description: Represents the process ID (PID) of
arguments in Bash the script itself.
Script • If the below script is executed
• The output will be the PID of the running script.
8. $!
Command line • Description: Represents the process ID of the
arguments in Bash last background command.
Script • The output of the below script will show the PID
of the sleep 10 command that was sent to the
background.
9. $-
Command line
• Description: Displays the current options set for
arguments in Bash the shell.
Script • The output of the below script will display the
current shell options like himBH.
9. $-
Command line
• Description: Displays the current options set for
arguments in Bash the shell.
Script • The output of the below script will display the
current shell options like himBH.
10. shift
Command line • Description: Shifts the positional parameters to
arguments in Bash the left, so $2 becomes $1, $3 becomes $2, and
so on.
Script • After using shift, the first argument $1 is lost,
and $2 takes its place.
Summary of Command-Line Argument Symbols
Redirection
1. Redirecting Standard Output (stdout)
• Redirect standard output to a file (overwrite):

• Saves the list of files in the current directory to files_list.txt,


overwriting the file if it exists.
Redirection
1. Redirecting Standard Output (stdout)
• Redirect standard output to a file (append):

• Appends the string "New entry" to log.txt.


Redirection
2. Redirecting Standard Error (stderr)
• Redirect standard error to a file (overwrite):

• Attempts to list a non-existent file, and writes the error message to


error_log.txt
Redirection
2. Redirect standard error to a file (append):
• Redirect standard error to a file (overwrite):

• Appends the error message of trying to list a non-existent file to


error_log.txt.
Redirection
3. Redirecting Both Standard Output and Error
• Redirect both stdout and stderr to a file (overwrite):

• Searches for all .txt files in the root directory. Both the results and
any error messages (e.g., permission denied) are written to
output_log.txt.
Redirection
3. Redirecting Both Standard Output and Error
• Redirect both stdout and stderr to a file (append):

• Appends both the search results and any errors to


output_log.txt.
Redirection
3. Redirecting Both Standard Output and Error
• Shorter syntax for redirecting both:

• Append both:
Redirection
4. Redirecting Input
• Redirect input from a file:

• Counts the number of lines in input.txt


Redirection
4.Redirect input and output:
• Redirect input from a file:

• Sorts the content of input.txt and writes the sorted output to


sorted_output.txt.
Redirection
6. Here Strings (<<<)
• Send a string as input:

• Searches for the word "hello" in the string "hello world".


Redirection
7. Discarding Output
• Redirect standard output to /dev/null (i.e., discard it):

• Suppresses the output of ls, discarding it


Redirection
7. Discarding Output
• Redirect both output and error to /dev/null:

• Suppresses both the output and error messages from the find
command.
Cron Job Scheduling in Linux
Cron Job Scheduling
• A way to automatically run tasks at specified times or intervals.
• It’s widely used for repetitive tasks like backups, updates, and system
maintenance.
• In Linux, cron jobs are managed using the cron daemon
o Which checks the /etc/crontab file,
o The /etc/cron.d/ directory, and
o Users' crontab files to execute scheduled commands.
Key Components of a Cron Job
1. Minute (0 - 59)
2. Hour (0 - 23)
3. Day of the Month (1 - 31)
4. Month (1 - 12)
5. Day of the Week (0 - 7) where both 0 and 7 represent Sunday.
6. Command: The script or command you want to run.
Key Components of a Cron Job
Each field can be set to:
• A specific value (e.g., 5 for 5 minutes).
• A wildcard (*) meaning "every possible value" (e.g., every minute,
every hour).
• Intervals using / (e.g., */15 means "every 15 minutes").
• Ranges using - (e.g., 1-5 for Monday to Friday).
• Lists using commas (e.g., 1,15 means on the 1st and 15th).
Crontab Syntax
• The basic syntax for a cron job entry is:
Example Cron Jobs
• Run a backup script every day at midnight:

• This will execute the script located at /path/to/backup.sh every


day at midnight (00:00).
Example Cron Jobs
• Run a script every Monday at 5:30 AM:

• Here, the script runs every Monday (1 is Monday) at 5:30 AM.


Example Cron Jobs
• Run a command every 15 minutes:

• The command runs every 15 minutes, regardless of the hour or day.


Example Cron Jobs
• Run a task at 10 PM on the first day of every month:

• This cron job will execute on the first day of every month at 10 PM.
Example Cron Jobs
• Run a cleanup script every 5 minutes between 9 AM and 5 PM,
Monday to Friday:

• The script runs every 5 minutes during work hours on weekdays.


Example Cron Jobs
• Run a script on January 1st at midnight:

• This job runs the script exactly at midnight on New Year's Day.
Special Strings for Scheduling
• There are shorthand strings for common cron schedules:
1. @reboot: Run the command once, at startup.
2. @yearly or @annually: Run the command once a year (0 0 1
1 *).
3. @monthly: Run the command once a month (0 0 1 * *).
4. @weekly: Run the command once a week (0 0 * * 0).
5. @daily or @midnight: Run the command once a day (0 0 *
* *).
6. @hourly: Run the command once an hour (0 * * * *).
Example using • To run a script every time the system starts:
shorthand:
User-Specific Crontab
• Each user can manage their own cron jobs using the crontab
command. To edit a user-specific crontab file, use:

• This opens a file where you can add cron jobs without specifying the
user (unlike in /etc/crontab).
Commands for Cron
• To list all your cron jobs:

• To remove all cron jobs for the current user:


Cron Environment
• By default, cron jobs execute with a limited environment. Important
considerations include:
o PATH: The environment may not include all directories in the path you expect.
It’s safer to use full paths for executables.
o MAILTO: You can specify an email to receive the output of cron jobs (if any).
Example:

o If MAILTO is empty (MAILTO=""), no email will be sent.


Redirecting Output
• By default, cron sends any output (stdout and stderr) to the user's
mailbox.
• You can redirect output to a file or suppress it entirely.
• Redirect to a log file:
Redirecting Output
• By default, cron sends any output (stdout and stderr) to the user's
mailbox.
• You can redirect output to a file or suppress it entirely.
• Suppress output:
Managing System-Wide Cron Jobs
• System-wide cron jobs are usually located in the following files or
directories:
o /etc/crontab: For system-wide cron jobs. It has an additional field to specify the
user who will run the command.
o /etc/cron.d/: Directory for additional system-wide cron jobs.
o /etc/cron.daily/, /etc/cron.weekly/, /etc/cron.monthly/: Directories
for scripts to run at daily, weekly, or monthly intervals.

o This will run the system update script as the root user at 2:30 AM daily.
Troubleshooting Cron Jobs
• Check Logs:
o Output from cron jobs is typically logged in /var/log/syslog or
/var/log/cron. Check these logs to troubleshoot failed jobs.

o Permissions: Make sure the scripts have execute permissions. Run chmod +x
/path/to/script.sh if necessary.
o Full Paths: Always use full paths for executables and files in your scripts, as
cron may not have the same environment as your shell.

You might also like