X
Collapse
-
Bash Script Include Another Bash Script
If you are a Linux enthusiast, then you have probably heard of the bash scripting concept. In bash scripting, you can automate the code block of the command sequence. However, sometimes, the code block in bash scripting becomes complex, and it can be tough for a user to execute it further.
That’s why bash offers a feature that includes a script in another. It lets you run the commands efficiently and achieve an enhanced level of automation. In this blog, we will explain the concept and provide examples of how to include a bash script into another script.
The source Command
The source is one of the most common commands to include a bash script into another. For example, if you want to add the “info.sh” script into “main.sh”, so all you have to do is add the following line into the “main.sh” script:
source info.sh
The info.sh script contains the following information:
Once you run the main.sh script, it will execute the info.sh first and then run the other commands:
Dot (.) or Dot-Slash
You can use dot (.) instead of source command as both are similar and used to execute the command from the current shell.
You can also use the dot-slash (./) instead of dot (.) to execute the command from the current directory:
How to Include Multiple Scripts Into One Script
Let’s create a script to check the current CPU load and execute the other script according to the load. Now, we need to create two scripts, one for the high load and the other is for the medium load:
For High Load:
#!/bin/bash
echo "High CPU load detected."
echo "Killing non-essential processes..."
pkill -9 -f firefox
echo "Non-essential processes have been terminated."
For Medium Load:
#!/bin/bash
echo "Moderate CPU load."
echo "Logging current process statuses..."
ps -aux > /tmp/current_processes.txt
echo "Process log created at /tmp/current_processes.txt."
The script above creates a tmp text file if the system has a medium CPU load. Let’s now create a third script, which will have the above two scripts link:
#!/bin/bash
cpu_load=$(uptime | awk -F '[a-z]:' '{ print $2}' | cut -d, -f2 | xargs)
if (( $(echo "$cpu_load > 2.0" | bc -l) )); then
echo "Triggering high load script."
source scripts/high_load.sh
else (( $(echo "$cpu_load > 1.0" | bc -l) ));
echo "Triggering moderate load script."
source scripts/moderate_load.sh
fi
Finally, save and run the script by running the below command in the terminal:
Wrapping Up
So, this was all about the simple ways to include one script to another and enhance the automation with no hassles. We have also included an example showing how you can perfectly include multiple scripts to break the complex processes without catering to the automation. We recommend you explore creativity and create various scripts around linking scripts.
More...Tags: None
Posting comments is disabled.
Categories
Collapse
Article Tags
Collapse
There are no tags yet.
Latest Articles
Collapse
-
by Kasimba
by George Whittaker
Introduction
In the world of operating systems, Windows has long held the lion’s share of the market. Its user-friendly interface and wide compatibility have made it the default choice for many. However, in recent years, Linux has steadily gained traction, challenging the status quo with its unique offerings. What was once considered the domain of tech enthusiasts and developers is now...-
Channel: Articles
Yesterday, 06:52 AM -
-
by KasimbaOverview of NGINX and Apache
NGINX and Apache are leading web server solutions utilized for hosting websites and web applications. Apache, developed by the Apache Software Foundation, offers robust configuration options and extensibility. NGINX, created by Igor Sysoev, is known for its efficiency in handling numerous concurrent connections with low resource utilization. Both servers function not only as HTTP servers but also as reverse proxies, load balancers, and more.
What is
...-
Channel: Articles
Yesterday, 03:54 AM -
-
by KasimbaHi everyone, I hope you are enjoying the BETA so far! This release introduces new features, tools, and artwork, so we anticipate a good number of bug reports. Every single fix helps us refine and improve the final release. Your feedback during the BETA phase is extremely important to us. Linux Mint 22.1 is our […]
More...-
Channel: Articles
12-16-2024, 11:50 AM -
-
by Kasimba
by George Whittaker
Introduction
Remote access is a cornerstone of modern IT infrastructure, enabling administrators and users to manage systems, applications, and data from virtually anywhere. However, with great power comes great responsibility—ensuring that remote access remains secure is paramount. This is where OpenSSH steps in, providing robust, encrypted communication for secure remote management....-
Channel: Articles
12-13-2024, 10:31 PM -
-
by Kasimba
by George Whittaker
Introduction
The grep command, short for "global regular expression print," is one of the most powerful and frequently used tools in Unix and Linux environments. From sifting through log files to finding patterns in text, grep is a Swiss Army knife for system administrators, developers, and data analysts alike. However, many users limit themselves to its basic functionality, unaware...-
Channel: Articles
12-13-2024, 09:24 PM -
-
by KasimbaThis is the BETA release for Linux Mint 22.1 “Xia”. Linux Mint 22.1 is a long term support release which will be supported until 2029. It comes with updated software and brings refinements and many new features to make your desktop even more comfortable to use. New features: This new version of Linux Mint contains […]
More...-
Channel: Articles
12-12-2024, 09:31 AM -