shell script – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Wed, 21 Dec 2022 07:32:24 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Bash – Sending Email via Amazon SES https://tecadmin.net/bash-sending-email-via-amazon-ses-smtp/ https://tecadmin.net/bash-sending-email-via-amazon-ses-smtp/#respond Tue, 11 Oct 2022 04:13:55 +0000 https://tecadmin.net/?p=32093 Amazon SES (Simple Email Service) is a popular SMTP service provider similar to Sendgrid, Mailchimp, etc. In order to use SES, you need to signup for an Amazon Web Services account. Which is the leading Cloud-based service provider. Post signup you need to add your credit card for the billing. The default SES allows sending [...]

The post Bash – Sending Email via Amazon SES appeared first on TecAdmin.

]]>
Amazon SES (Simple Email Service) is a popular SMTP service provider similar to Sendgrid, Mailchimp, etc. In order to use SES, you need to signup for an Amazon Web Services account. Which is the leading Cloud-based service provider. Post signup you need to add your credit card for the billing. The default SES allows sending 2000 emails/day freely. After the default limit, you will be charged as pay-per-use.

In this blog post, you will learn to send emails via Amazon SES or any other SMTP provider from a bash shell or script.

Pre-Requisiteis

  • In this tutorial, we used the SendEmail command line SMTP client for sending emails. So you must have installed SendMail on your system.
  • You must have verified the email address or the domain name under Verified Identities in Amazon SES. When the domain is verified, you can use any email address while sending emails
  • All the new accounts in the Amazon SES are in sandbox mode for security purposes. You need to submit a request to support converting the SES account to production mode.

Shell Script for Sending Emails via SMTP

I have written a small shell script that sends emails via the remote SMTP servers. It uses the SendEmail SMTP client. Use any of the popular SMTP providers (like Sendgrid, Amazon SES, and Mailchimp) with this shell script. You can also integrate this shell script code into your existing shell scripts for sending emails properly.

#!/usr/bin/env bash

## SMTP configuration details

SMTP_HOST="email-smtp.us-east-1.amazonaws.com"
SMTP_PORT="587"
SMTP_USER="XXXXXXXXXXXXXXX"
SMTP_PASS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
EMAIL_FROM="notification@example.com"
EMAIL_TO="your-email@example.com"

SUBJECT="WARNING: Github Public Repo Found"

## Sending email

cat Mailcontent.txt | sendemail -l /tmp/email.log \
    -f ${EMAIL_FROM} \
    -u ${SUBJECT} \
    -t ${EMAIL_TO}  \
    -s ${SMTP_HOST}:${SMTP_PORT}  \
    -o tls=yes  \
    -xu ${SMTP_USER}  \
    -xp ${SMTP_PASS}

In the above script, the Mailcontent.txt file contains the mail body content.

Conclusion

Shell scripts are an important part of system administration. It helps us to automate tasks quickly like scheduling backups, archiving logs and collecting data, etc. Sometimes we are also required to send emails from shell scripts. In this tutorial, you have learned to send emails via the Amazon SES server. Even you can also use this script with any other SMTP providers.

The post Bash – Sending Email via Amazon SES appeared first on TecAdmin.

]]>
https://tecadmin.net/bash-sending-email-via-amazon-ses-smtp/feed/ 0
How to run a command on bash script exits https://tecadmin.net/how-to-run-a-command-on-bash-script-exits/ https://tecadmin.net/how-to-run-a-command-on-bash-script-exits/#respond Tue, 02 Aug 2022 03:03:48 +0000 https://tecadmin.net/?p=30843 Shell scripts are handy for automating tasks like backup databases, clearing log files, etc. You need to perform some tasks when the script finishes the execution. No matter at which stage the script is finished. For example, a script that clears the log files from the system. The script first checks the size of the [...]

The post How to run a command on bash script exits appeared first on TecAdmin.

]]>
Shell scripts are handy for automating tasks like backup databases, clearing log files, etc. You need to perform some tasks when the script finishes the execution. No matter at which stage the script is finished.

For example, a script that clears the log files from the system. The script first checks the size of the log file, if the log file size is lower than the specified size, the script exits. In that case, you still want to run a block of code.

To do this, we can use the trap command to catch the EXIT signal and execute a command or function. Below is the sample shell script to run a function or command on shell script exit.

#!/usr/bin/env bash

on_exit(){
  echo "Your script ended now"
}

trap 'on_exit' EXIT

echo "Hello world"

Run the above script and see the results

Output
Hello world Your script ended now

Hope, this quick how-to guide helps you to improve your shell script writing skills.

The post How to run a command on bash script exits appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-run-a-command-on-bash-script-exits/feed/ 0
How to Detect and Handle Errors in Your Bash Scripts https://tecadmin.net/execute-specific-command-on-error-in-bash-script/ https://tecadmin.net/execute-specific-command-on-error-in-bash-script/#respond Sat, 30 Jul 2022 21:53:56 +0000 https://tecadmin.net/?p=30831 We can use the trap command to catch the error signal system by the system during script execution. Then you can execute a shell command or call a function. In this way, you can execute your custom script code on an error that occurred in a bash script. This can be helpful to revert any [...]

The post How to Detect and Handle Errors in Your Bash Scripts appeared first on TecAdmin.

]]>
We can use the trap command to catch the error signal system by the system during script execution. Then you can execute a shell command or call a function. In this way, you can execute your custom script code on an error that occurred in a bash script.

This can be helpful to revert any partial changes, close database connections, or email status to the concerned persons, etc. You can use trap commands with `ERR` signals like:

trap 'on_error_function' ERR

When an error is generated in a shell script, it will execute a function named ‘on_error_function’ of your shell script. Instead of calling a function, you can simply run a command as well.

Example: Execute a function on Error in Bash

Let’s understand with an example. Create a sample shell script, and create a function with any name. Then add the trap command with the function for ERR signal. Next, add a simple command that generates an error.

#!/usr/bin/env bash

on_error(){
  echo "Some error occurred"
}

trap 'on_error' ERR

ls ~/dir_not_exists

Execute the above script and you should see the results below:

Output:
ls: cannot access '/home/tecadmin/dir_not_exists': No such file or directory Some error occurred

You can see that the error is trapped and the function on_error() is executed by the bash script.

Example: Execute a command on Error in Bash

Let’s see one more example. Here we will execute a command when any error will occur in the shell script.

#!/usr/bin/env bash

trap 'echo Ohhh no!' ERR

ls ~/dir_not_exists

In the above script, we do not define any separate function. Simply run an echo command on error. Execute the above script and see the results.

Output:
ls: cannot access '/home/tecadmin/dir_not_exists': No such file or directory Ohhh no!

Example: Get the line number of error occurred

You can also find out the line number, where the error occurred in the bash script along with the script name. To do this use the bash inbuilt ‘caller’.

#!/usr/bin/env bash

on_error(){
        echo "Error found in: $(caller)" >&2
}

trap 'on_error' ERR

ls ~/dir_not_exists

Execute the above script and see the results. You will see the script name and the line number, where the error occurred.

Output:
ls: cannot access '/home/tecadmin/dir_not_exists': No such file or directory Error found in: 9 ./script.sh

Conclusion

Thanks for reading this article. Hopefully, this tutorial helps you with better writing of shell scripts by catching the error and taking some action.

Also, remember that the ERR trap catches the runtime errors only. Like if any command returns the non-zero status code. It doesn’t catch the syntax errors, because in the case of syntax error the script fails without running any command.

The post How to Detect and Handle Errors in Your Bash Scripts appeared first on TecAdmin.

]]>
https://tecadmin.net/execute-specific-command-on-error-in-bash-script/feed/ 0
Check if a script is running as root user in Linux https://tecadmin.net/check-if-a-script-is-running-as-root-user-in-linux/ https://tecadmin.net/check-if-a-script-is-running-as-root-user-in-linux/#respond Wed, 27 Jul 2022 06:09:05 +0000 https://tecadmin.net/?p=30720 Sometimes the shell scripts are required to run as the root user to perform some tasks with administrator privileges. Generally, that tasks can be achieved with the Sudo commands. If in some cases you still need to force the user to run the script as the root user, you can add code to your script [...]

The post Check if a script is running as root user in Linux appeared first on TecAdmin.

]]>
Sometimes the shell scripts are required to run as the root user to perform some tasks with administrator privileges. Generally, that tasks can be achieved with the Sudo commands. If in some cases you still need to force the user to run the script as the root user, you can add code to your script to check if the script is running with the root user or not.

Check if a shell script running as root user

Add the following code at beginning of the shell script to check if the script is running as the root user. If the script is executed as a non-root account, it will exit with status code 1.

#!/usr/bin/env bash

if [ "$EUID" -ne 0 ];then
    echo "Please run this script as root user"
    exit 1
fi

Here the EUID is the system variable that stored the user id (UID) of the currently logged-in user. The “root” user’s UID is always 0 on the Linux systems.

Instead of using the UID, you can also match the logged-in user name. The whoami command provides the currently logged user name. The below script will check if the script is running as a root user or not.

#!/usr/bin/env bash

if [ `whoami` != 'root' ];then
    echo "Please run this script as root user"
    exit 1
fi

Check if a shell script running as non-root user

Sometimes the scripts are required to run as a non-root account. In that case, you can add the following snippet to check for a user account and continue only if the script is running as a normal user.

#!/usr/bin/env bash

if [ "$EUID" -eq 0 ];then
    echo "Please run this script as a non-root user"
    exit 1
fi

Conclusion

In this quick how-to guide, you have learned about adding restrictions in a shell script to run as a root user or non-root user. If it is running a different user, the script will exit immediately.

The post Check if a script is running as root user in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/check-if-a-script-is-running-as-root-user-in-linux/feed/ 0
How to declare boolean variable in shell script https://tecadmin.net/boolean-variable-in-shell-script/ https://tecadmin.net/boolean-variable-in-shell-script/#respond Mon, 25 Jul 2022 15:11:14 +0000 https://tecadmin.net/?p=30695 The shell (bash) script doesn’t offer any data type with the variables. So we can’t specifically declare a variable of type boolean in shell scripts. But, you can still use the variable like a boolean in shell scripts. Store a sample string “true” to a variable and match it with the if condition. This works [...]

The post How to declare boolean variable in shell script appeared first on TecAdmin.

]]>
The shell (bash) script doesn’t offer any data type with the variables. So we can’t specifically declare a variable of type boolean in shell scripts.

But, you can still use the variable like a boolean in shell scripts. Store a sample string “true” to a variable and match it with the if condition. This works similarly to boolean.

#!/usr/bin/env bash

# Assign a string to a variable
var=true

# Test the variable value
if [ "$var" = 'true' ]; then
   echo "It's true"
else
   echo "It's false"
fi

So, even if the shell script doesn’t offer the data types, but we can still use the normal variable with similar working.

The post How to declare boolean variable in shell script appeared first on TecAdmin.

]]>
https://tecadmin.net/boolean-variable-in-shell-script/feed/ 0
Backup MySQL Databases to Amazon S3 (Shell Script) https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/ https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/#respond Sat, 28 May 2022 06:02:54 +0000 https://tecadmin.net/?p=29616 A shell script is a collection of commands to perform a specific job. MySQL is a relational database management system widely used on Linux systems. Amazon S3 is a cloud storage device provided by Amazon Web Services. It’s a good practice for the system administrator to back up databases at regular intervals and store them [...]

The post Backup MySQL Databases to Amazon S3 (Shell Script) appeared first on TecAdmin.

]]>
A shell script is a collection of commands to perform a specific job. MySQL is a relational database management system widely used on Linux systems. Amazon S3 is a cloud storage device provided by Amazon Web Services. It’s a good practice for the system administrator to back up databases at regular intervals and store them in a remote location like Amazon S3.

This tutorial contains a shell script that creates MySQL databases backup and uploads them to Amazon S3 buckets. You can also use this shell script to back up MariaDB or Amazon Aurora (MySQL compatible) databases.

Backup MySQL Databases to S3

Use the below step-by-step tutorial to back up the MySQL databases and upload them to the Amazon S3 bucket.

1. Install AWS CLI

In order to use this script, the system must have AWS CLI installed.

https://tecadmin.net/installing-aws-cli-in-linux/

2. Create S3 Bucket

Login to AWS Management Console and create a new s3 bucket.

Alternatively, you can also create s3 bucket via AWS CLI. The command will be like:

aws s3api create-bucket --bucket s3-bucket-name --region us-east-1 

Just replace the bucket name and region.

3. Shell Script to Backup MySQL database to S3

Copy the below shell script to a file like db-backup.sh. This script uses mysqldump command to create databases backups. Then use gzip command to archive backup files and finally use aws command to upload backup files to Amazon S3 bucket.

Create a file like /backup/scripts/s3-backup-mysql.sh in edit your favorite text editor. Then add the below content:

#!/usr/bin/env bash

#########################################################################
#########################################################################
###
####       Author: Rahul Kumar
#####      Website: https://tecadmin.net
####
#########################################################################
#########################################################################

# Set the folder name formate with date (2022-05-28)
DATE_FORMAT=$(date +"%Y-%m-%d")

# MySQL server credentials
MYSQL_HOST="localhost"
MYSQL_PORT="3306"
MYSQL_USER="user"
MYSQL_PASSWORD="password"

# Path to local backup directory
LOCAL_BACKUP_DIR="/backup/dbbackup"

# Set s3 bucket name and directory path
S3_BUCKET_NAME="s3-bucket-name"
S3_BUCKET_PATH="backups/db-backup"

# Number of days to store local backup files
BACKUP_RETAIN_DAYS=30 

# Use a single database or space separated database's names
DATABASES="DB1 DB2 DB3"

##### Do not change below this line

mkdir -p ${LOCAL_BACKUP_DIR}/${DATE_FORMAT}

LOCAL_DIR=${LOCAL_BACKUP_DIR}/${DATE_FORMAT}
REMOTE_DIR=s3://${S3_BUCKET_NAME}/${S3_BUCKET_PATH}

for db in $DATABASES; do
   mysqldump \
        -h ${MYSQL_HOST} \
        -P ${MYSQL_PORT} \
        -u ${MYSQL_USER} \
        -p${MYSQL_PASSWORD} \
        --single-transaction ${db} | gzip -9 > ${LOCAL_DIR}/${db}-${DATE_FORMAT}.sql.gz

        aws s3 cp ${LOCAL_DIR}/${db}-${DATE_FORMAT}.sql.gz ${REMOTE_DIR}/${DATE_FORMAT}/
done

DBDELDATE=`date +"${DATE_FORMAT}" --date="${BACKUP_RETAIN_DAYS} days ago"`

if [ ! -z ${LOCAL_BACKUP_DIR} ]; then
	cd ${LOCAL_BACKUP_DIR}
	if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
		rm -rf ${DBDELDATE}

	fi
fi

## Script ends here

Update all the necessary variables as per your system environment.

4. How to run backup script

Set the execute (x) permission on script:

chmod +x s3-backup-mysql.sh 

Then run the backup script.

./s3-backup-mysql.sh 

5. Schedule backup script to run daily

Schedule the shell script using crontab to run on a daily basis.

crontab -e 

Add the below settings to end of the file:

# Run daily @ 2am
0 2 * * * /backup/scripts/s3-backup-mysql.sh > /dev/null 2>&1

Save the file and close it.

Conclusion

This tutorial provides you with a shell script to back up MySQL databases and upload them to the Amazon S3 bucket. That could be helpful for you to automate database backups and save a copy on cloud storage.

The post Backup MySQL Databases to Amazon S3 (Shell Script) appeared first on TecAdmin.

]]>
https://tecadmin.net/backup-mysql-database-to-amazon-s3-shell-script/feed/ 0
Bash Concatenate Strings https://tecadmin.net/bash-concatenate-strings/ https://tecadmin.net/bash-concatenate-strings/#respond Mon, 01 Nov 2021 11:10:22 +0000 https://tecadmin.net/?p=28295 Concatenating strings in Bash is as simple as combining them with the double-quote (“ ”) character. However, if your strings consist of more than a couple of words or include special characters, you may need to use a different method. Fortunately, the Bash programming language provides several methods for concatenating strings. This article explores five [...]

The post Bash Concatenate Strings appeared first on TecAdmin.

]]>
Concatenating strings in Bash is as simple as combining them with the double-quote (“ ”) character. However, if your strings consist of more than a couple of words or include special characters, you may need to use a different method. Fortunately, the Bash programming language provides several methods for concatenating strings.

This article explores five common ways to combine strings in your Bash scripts and programs. Keep reading to learn more about concatenating strings in Bash and which method is best for your specific situation.

Concatenate Strings

The simplest way to combine strings in Bash is to use the double-quote (“ ”) character. You can enclose your strings within double quotes and combine them to form a single string. This is useful for combining short strings that don’t require any special formatting. The example below demonstrates how to combine two short strings to form a single, long string using double quotes.

#/usr/bin/env bash
# A Sample shell script to concatenate strings 

# Declare variables
STR1="Welcome"
STR2="Tecadmin"

# Concatenate both strings 
STR3="$STR1$STR2"
echo "$STR3"

# You can even add a space between strings
STR3="$STR1 $STR2"  
echo "$STR3"

The echo command will print the result string.

Output
WelcomeTecadmin Welcome Tecadmin

Concatenate String Variable with Literal

A literal represents a fixed value. Instead of joining two string variables, we can also join a literal string with a variable value. For example, take an input of a user’s first name and prefix it with a “Welcome” literal string.

#/usr/bin/env bash

# A shell script to concatenate variable 
# with a literal string

# Take a user input and store to variable
read -p "What is your first name: " STR1

# Concatenate the string
STR2="Welcome ${STR1}"
echo "$STR2"

Execute the above script, It will prompt you to enter your Name. Then concatenate “Welcome” as a prefix to the input string and print the results.

Output
Enter your first name: Rahul Welcome Rahul

Concatenate Strings with += Operator

In general programming language the += adds the RHS value to LHS. You can also use this method to Concatenate the RHS string variable to the LHS string.

#/usr/bin/env bash
# A Sample shell script to concatenate strings 

# Delcare variable
STR="Welcome to"

# Concatenate another string to this variable.
STR+=" TecAdmin"

# Display the result string
echo $STR

This will print: Welcome to TecAdmin

Using the Printf Command

In bash, print is a command that is used to format and print data to standard output. The -v option initialize a variable with the output rather than print on output.

#/usr/bin/env bash

# A Sample shell script to concatenate strings 
# with the print command.

# Delcare variable
PREFIX="Hello Mr. "

# Concatenate another string to this variable.
printf -v STR "$PREFIX Rahul"

# Display the result string
echo $STR

This will print: Hello Mr. Rahul

Using loop

When there is an undefined number of the input strings, you need to concatenate them into a single string. The while loop will help you with the join (+=) operator.

For example, you need to read all lines of a file and concatenate them in a single string. To do this, we will read the file content line by line and concatenate them.

#/usr/bin/env bash

# A Sample shell script to concatenate strings 
# with a while loop

# Delcare variable
while read LINE; do
  STR+="$LINE "
done < data.txt

# Display the result string
echo $STR

Conclusion

This article explores five common ways to combine strings in your Bash scripts and programs. The simplest way to combine strings is to use the double-quote character. You can also use the for loop command to iterate through a series of words and combine them into a single string. The join (+) command is a Bash built-in that can be used to combine a series of items into a single string. The BASH scripting language allows you to perform more complex string operations including combining variables, calculations, and more.

The post Bash Concatenate Strings appeared first on TecAdmin.

]]>
https://tecadmin.net/bash-concatenate-strings/feed/ 0
Shell Script to Add Two Numbers https://tecadmin.net/bash-add-two-integers/ https://tecadmin.net/bash-add-two-integers/#comments Sat, 05 Jun 2021 02:34:57 +0000 https://tecadmin.net/?p=25948 Calculating the sum of two integers (Numbers) in a shell script is pretty simple as in other programming languages. Bash shell provides a command-line utility called expr to evaluate expressions. The latest version of the Bash shell also includes the functionality to evaluate expressions directly with the shell. In this tutorial, we will discuss a [...]

The post Shell Script to Add Two Numbers appeared first on TecAdmin.

]]>
Calculating the sum of two integers (Numbers) in a shell script is pretty simple as in other programming languages. Bash shell provides a command-line utility called expr to evaluate expressions. The latest version of the Bash shell also includes the functionality to evaluate expressions directly with the shell.

In this tutorial, we will discuss a few methods to calculate the sum of the two numbers in a bash script.

Bash – Adding Two Numbers

The expr is the command-line utility used for evaluating mathematical expressions. Bash shell also supports evaluating the mathematical expressions directly.

Use the following syntax to calculate the sum of two integers in a shell script:

  • Using expr command with quotes
    sum=`expr $num1 + $num2`
    
  • Use expr command inclosed with brackets and start with dollar symbol.
    sum=$(expr $num1 + $num2)
    
  • This is my preferred way to directly with the shell.
    sum=$(($num1 + $num2))
    

In the next few examples, we will discuss calculating the sum of numbers directly with a shell. You can also choose expr command to give the syntax above.

Calculate Sum in Shell Script

Bash shell also evaluates the mathematical expressions directly. You just need to write the expressions enclosed in double brackets with a dollar like $((...)).

Write an example shell script to initialize two numeric variables. Then perform an addition operation on both values and store results in the third variable.

#!/bin/bash
# Calculate the sum of two integers with pre initialize values
# in a shell script

a=10
b=20

sum=$(( $a + $b ))

echo "Sum is: $sum"

Output:

Sum is: 30

Calculate Sum with Command Line Arguments

In this second example, the shell script reads two numbers as command line parameters and performs the addition operation.

#!/bin/bash
# Calculate the sum via command-line arguments
# $1 and $2 refers to the first and second argument passed as command-line arguments

sum=$(( $1 + $2 ))

echo "Sum is: $sum"

Let’s execute this script is a shell

./sum.sh 12 14        # Executing script 

Sum is: 26

Calculate Sum with Run Time Input

Here is another example of a shell script, which takes input from the user at run time. Then calculate the sum of given numbers and store to a variable and show the results.

#!/bin/bash
# Take input from user and calculate sum.
 
read -p "Enter first number: " num1
read -p "Enter second number: " num2
 
sum=$(( $num1 + $num2 ))
 
echo "Sum is: $sum"

Output:

Enter first number: 12
Enter second number: 15
Sum is: 27

Conclusion

In this tutorial, you have learned few methods to add two numbers in a shell script.

The post Shell Script to Add Two Numbers appeared first on TecAdmin.

]]>
https://tecadmin.net/bash-add-two-integers/feed/ 4
How to Run Shell Script as Systemd in Linux https://tecadmin.net/run-shell-script-as-systemd-service/ https://tecadmin.net/run-shell-script-as-systemd-service/#comments Mon, 12 Apr 2021 12:46:00 +0000 https://tecadmin.net/?p=25188 Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with PID 1. This also helps us to manage system and application services on our Linux operating system. We can also run any custom script as [...]

The post How to Run Shell Script as Systemd in Linux appeared first on TecAdmin.

]]>
Systemd is a software application that provides an array of system components for Linux operating systems. It is the first service to initialize the boot sequence. This always runs with PID 1. This also helps us to manage system and application services on our Linux operating system.

We can also run any custom script as systemd service. It helps the script to start on system boot. This can be helpful for you to run any script which required to run at boot time only or to run always.

In our previous tutorial we have provides you instructions to run a Python script using Systemd. This tutorial covers running a shell script as a Systemd service.

Step 1 – Create a Shell Script

First of all, create a sample shell script, which needs to run at system startup. You can also create a shell script to run always using an infinite while loop. The script will keep running until the system goes down.

Create a shell script as per requirements. For testing purposes, I am creating a sample shell script as /usr/bin/script.sh.

sudo nano /usr/bin/script.sh 

Added the following sample shell script code.

#!/bin/bash

while true
do
 // Your statements go here
 sleep 10
done

Press CTRL+O and hit enter to save changes. Then press CTRL + x to quit from nano editor.

Now, make the script executable:

sudo chmod +x /usr/bin/script.sh 

To run a script once during system boot time doesn’t require any infinite loop. Instead of the above script, you can use your shell script to run as a Systemd service.

Step 2 – Create a Systemd Unit File

Next, create a service unit file for the systemd on your system. This file must have .service extension and saved under the under /lib/systemd/system/ directory

sudo nano /lib/systemd/system/shellscript.service 

Now, add the following content and update the script filename and location. You can also change the description of the service.

[Unit]
Description=My Shell Script

[Service]
ExecStart=/usr/bin/script.sh

[Install]
WantedBy=multi-user.target

Save the file and close it.

Step 3 – Enable the New Service

Your system service has been added to your service. Let’s reload the systemctl daemon to read the new file. You need to reload this daemon each time after making any changes in .service file.

sudo systemctl daemon-reload 

Now enable the service to start on system boot, also start the service using the following commands.

sudo systemctl enable shellscript.service 
sudo systemctl start shellscript.service 

Finally verify the script is up and running as a systemd service.

sudo systemctl status shellscript.service 

The output looks like the below:

Running shell script as systemd service

All done!

Conclusion

Congratulation, You have successfully configured a shell script to run at system startup. This can be helpful to run your custom scripts that are required to start at system boot. This tutorial helped you to configure a shell script as systemd service.

The post How to Run Shell Script as Systemd in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/run-shell-script-as-systemd-service/feed/ 6
Using Increment (++) and Decrement (–) Operators in Bash https://tecadmin.net/bash-increment-decrement-operators/ https://tecadmin.net/bash-increment-decrement-operators/#respond Sun, 28 Feb 2021 12:08:20 +0000 https://tecadmin.net/?p=24803 Similar to other programming language bash also supports increment and decrement operators. The increment operator ++ increases the value of a variable by one. Similarly, the decrement operator -- decreases the value of a variable by one. Pre and Post Increment: When using ++ operator as prefix like: ++var. Then first the value of variable [...]

The post Using Increment (++) and Decrement (–) Operators in Bash appeared first on TecAdmin.

]]>
Similar to other programming language bash also supports increment and decrement operators. The increment operator ++ increases the value of a variable by one. Similarly, the decrement operator -- decreases the value of a variable by one.

Pre and Post Increment:

  • When using ++ operator as prefix like: ++var. Then first the value of variable is incremented by 1 then, it returns the value.
  • When using the ++ operator as postfix like: var++. Then first original value will returned and after that the value incremented by 1.

Pre and Post Decrement:

  • When using -- operator as prefix like: --var. Then first the value of variable is decremented by 1 then, it returns the value.
  • When using the -- operator as postfix like: var--. Then first original value will returned and after that value is decremented by 1.

Using ++ and -- Operators in Bash

In bash scripting, increment and decrement operators can be write in various ways. You can choose any of the below expression defined below to perform post increment or decrement value in bash.

Increment operator expression in bash –

  1. var=$((var++))
    
  2. ((var++))
    
  3. let "i++"
    

Decrement operator expression in bash –

  1. var=$((var--))
    
  2. ((var--))
    
  3. let "i--"
    

Post-Increment Example in Bash

Below is the sample example of increment operator, where we assigning a numeric value to a variable (i). Then Perform the post increment (i++) operation on variable and with storing value to another variable.

i=10
j=$((i++))
echo $j
echo $i

Output:

10
11

See the above results and understand what happened.

  • In first row, we assigned number value 10 to variable i.
  • Then perform post increment (i++) and assign value to variable j.
  • As it is post increment operation, then first original value will be assigned to variable j, then value of i will be increases by one.

Using while Loop:

i=1
while(($i<10))
do
   echo $i
   ((i++))
done

Using for Loop:

for((i=1; i<10; i++))
do
   echo $i
done

Pre-Increment Example in Bash

The below example will use the pre increment operator.

i=10
 j=$((++i))
 echo $j
 echo $i

Output:

11
11

See the above results and understand what happened.

  • In first row, we assigned number value 10 to variable i.
  • Then perform pre increment (++i) and assign value to variable j.
  • As it is pre increment operation, then first the value of the variable will be increases by 1 then the assignment will be performed.

The post Using Increment (++) and Decrement (–) Operators in Bash appeared first on TecAdmin.

]]>
https://tecadmin.net/bash-increment-decrement-operators/feed/ 0