How to Create Simple Shell Scripts in Linux

Creating shell scripts is one of the most important skills Linux users need to master at the touch of a button. Shell scripts play a major role in automating repetitive tasks that would otherwise be tedious to perform line by line.

In this tutorial we will look at some of the basic theses that every Linux user should perform.

1. Creating a simple shell script

The shell script is a file with ASCII text. We start by creating a simple shell script, and for that we use a word processor. There are many word processors available, both on the command line and in the GUI. For this manual we use the vim editor.

Let’s start by creating a simple script that displays Hello World as it runs.

$ vim hello.sh

Paste the following content into the file and save it.

#! /bin/bash
# Print Hello World
echo Hello World!

Let’s go through the shell script line by line.

  • The first line – #! /bin/bash – is known as the shebang headline. This is a special construction that indicates which program is used to interpret the script. In this case it is a bash shell, which is named /bin/bash. There are other scripting languages like Python, which is called #! /usr/bin/python3, and Perl, whose shebang header is called #! /usr/bin/perl.
  • The second line is a comment. A comment is a statement that describes what a shell script does and does not do when it is executed. Comment is always preceded by a pound sign #.
  • The last line is the command that prints the Hello World message on the terminal.

The next step is to execute the script by giving it permission to execute it with the chmod command, as shown in the figure below.

$ chmod +x hello.sh

Finally, execute the shell script with one of the commands :

$ bash hello.sh
OR
$ ./hallo.sh

Scenario Hello, World Clam

2. Use conditional statements to implement code

Like other programming languages, bash-scripting uses conditional operators for decision making, with minor syntax changes. We cover the yew, the als-else and the elif.

Example when the approval is only

whether it can be used to test one or more conditions. We will start using the if statement to check for a disorder in principle. If the declaration is defined in blocks, as … Fi.

if command
, then
approval
fi

Let’s take a look at the shell script below.

#! /bin/bash
echo ‘Enter account’
read x

if [ [ $x == 70 ] ] ; then
the ultrasound is good work !
fi

The shell script above asks the user to specify a note, which is then stored in the x-variable. If the score is 70, the script gives the output Good work! The relational operator == is used to check whether the estimate entered corresponds to the estimate stored in variable x, 100.

if declared in a shell script

Other comparison operators you can use are

  • -eq – equal
  • -is not the same
  • -Less than
  • -less than or equal to
  • -Less than
  • -ge – more or less

For example, the If format block below prints the words Work harder if the input letter is less than 50.

if [ [ [ $x -lt 50 ]] ; then you can hear the echo in
Work louder !
fi

if declared in a shell script

Example of if-else approval

For situations where you have two possible outcomes – one or the other – a self-declaration will be useful.

If command
, then the
1
operator is always the
2
operator.

The script below reads the initial score and checks if it is greater than or equal to 70.

If the score is greater than or equal to 70, you get a good job, you pass! However, if the score is less than 70, you cannot print the picture.

#! /bin/bash

echo Enter your account

read x

if [ $x -ge 70 ] ; then
is an echo. Well done. You made it!
else
Echo.
fi you didn’t make it.

Auto-operator in the shell script

Example of an application for an opt-out

Scenarios with many different circumstances and outcomes use the explanation when life is good. This operator accepts the following format.

if condition1
, then
approval1
elif condition2
, then
approval2
other
approval3
fi

For example, we have a lottery script that checks whether the number 90, 60 or 30 has been entered.

#! /bin/bash

echo Enter your account

read x

if [[ $x -eq 90 ] ;
, then
is the same as if you had won first prize.

elif [[ $x -eq 60 ]] ;
followed by
echo You have won the second prize.

eleven [[[[ $x -eq 30 ]] ;
dan
Echo You won second prize
another
Echo Try
fi again

Information Validation Statement

3. Instructions for use if with AND logic

You can use the operator as with the logical operator AND to perform a task under two conditions. The && operator is used to indicate AND logic.

#! /bin/bash

echo ‘Enter your user ID’
read user_id

echo ‘Enter your tag_no’
read tag_id

if [[[[($user_id == tecmint && $tag_id -eq 3990) ] ;
then
echo Login successful
still
echo Login failed
fi

5. Operating instruction If with OR logic

When using the OK logic represented by the symbol ||, the script must meet each of the conditions to produce the expected results.

#! /bin/bash

echo ‘Please enter any number’
Read number

if [[(number -eq 55 |number -eq 80) ];
then
gives the echo Congratulations! You win.
, another
, ultrasound. Sorry, try again.
fi.

If a logical OR operator

Use hinged structures

Damping cycles allow users to perform a series of tasks until a certain result is achieved. It’s useful for repetitive tasks. In this section we will look at some loops that can also be found in other programming languages.

While the hinge

It is one of the easiest cycles to use. The syntax is quite simple:

when the teams are full.

In the next cycle, all numbers from 1 to 10 are displayed at runtime.

#! /bin/bash
# Simple while loop
counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
do

Let’s dissect the noose:

The variable counter is set to 1. And although the variable is less than or equal to 10, the countervalue is increased until the condition is met. In the echo line, $counter prints all numbers from 1 to 10.

.

For hinge

As in the while loop, the for loop is used for iterative code execution. That is to say… Repeat the execution of the code as often as possible, depending on the needs of the user.

The syntax is as follows:

for var in 1 2 3 4 5 N
do
command1
command2
executed

A cycle for a cycle under iteration goes through 1 – 10 and processes their values on the screen.

For the hinge in the shell Script

The best way to do this is to define the field with double square brackets { }, as shown in the figure, instead of entering all numbers.

#! /bin/bash
# Specify the range of the loop.

for calling in {1…10}
do
echo $num
done

Main positioning parameters

The location parameter is a special variable that is referred to in the script when values are passed to the shell but cannot be assigned. The parameters of the station are $0 $1 $2 $3 $3 ……. up to $9. After the value of $9, the parameters must be placed between brackets, for example ${10}, ${11}. …and so on.

When the script runs, the first position parameter, which is 0, takes the name of the shell script. Parameter 1 takes the first variable sent to the terminal, 2 – the second, 3 – the third and so on.

Let’s make the test.sh script as shown in the figure.

#! /bin/bash
echo Scriptername: $0
echo My last name: $1
echo My middle name : $2

Then run the script and give the first and last name as arguments :

# ? bash test.sh James Chiari?

Shoe position parameters

You can see on the output that the first variable to be printed is the shell script name, in this case test.sh. The names corresponding to the position parameters defined in the shell script are then printed.

Position parameters are useful because they define the input data instead of explicitly assigning a value to a variable.

Communication output of the housing

Let’s start by answering a simple question: What’s an exit code?

Every command executed on the command line by a user or a command line script has an execution state. The output state is an integer.

Output status 0 means that the command has been executed successfully and without error. Any value between 1 and 255 indicates that the command was not executed or was unsuccessful.

To find out the original status of the order, do you use $? Variable vagina.

Output status 1 indicates a general error or unacceptable errors, such as editing files without sudo authorization.

Output status 2 indicates the incorrect use of an embedded command line or variable.

The output status 127 indicates an illegal command, which normally generates the error Command not found.

Output status query

Exit-shell command processing in scenario

In bash scripts, you can save the output of the command in a variable for later use. This is called shell command exchange and can be done in the following way.

variable=$(command)
OR
variable=$(/path/k/command)
OR
variable=$(command argument 1 argument 2…)

For example, you can save the date command in a variable called today and call a shell script to open today’s date.

#! /bin/bash

today=$(date)

The echo of the day

Date of printing housing policy

Let’s take another example. Suppose you want to find valid users to connect to Linux. How’d you do it? First of all, the /etc/passwd file contains a list of all users (system and process users as well as login users).

To view the file, you must use the command cat. However, to reduce the number of names, use the grep command to find users with the /bin/bash attribute and the cut -c 1-10 command, as shown in the figure, to display the first 10 characters of names.

We have saved the cat command in the variable login_users.

#!/bin/bash
login_users=$(cat /etc/passwd | grep /bin/bash | cut -c 1-10)
echo ‘Here is the list of users to connect:
echo $login_users

List of logged-in users

This is the end of our training manual for creating simple shell scripts. We hope you found it useful.

Related Tags:

create shell script windows,create shell script mac,create empty file in shell script,save and execute bash script,linux create a shell command,shell script to create a file in a directory,1.Shell scripts are written using text editors. On Linux systems, there are a number to choose from: Vim, Emacs, Nano, Pico, Kedit, Gedit, Geany, Notepad++ …2.Start to type in basic commands that you would like the script to run.Be sure to type each command on a separate line.For example, to print out words to the …3.Now that the file has been saved, it needs to be made executable. This is done using the chmod command. On your Linux command line type:chmod 555 …,1.Shell scripts are written using text editors. On Linux systems, there are a number to choose from: Vim, Emacs, Nano, Pico, Kedit, Gedit, Geany, Notepad++ …,2.Start to type in basic commands that you would like the script to run.Be sure to type each command on a separate line.For example, to print out words to the …,3.Now that the file has been saved, it needs to be made executable. This is done using the chmod command. On your Linux command line type:chmod 555 …,write shell script mac,linux script file extension,writing python scripts in linux,how to write a shell,how to create shell script in windows,file manipulation in shell script,how to save shell script in vi editor,how to create a shell script in ubuntu,why bin/bash in shell script,meaning in shell script,bash file extension,bash command -c option,run bash script mac,bash script example,bash script file,bash script run command,add script to path windows,add script to path linux,run bash script from any directory,add script to path mac,how to make a script executable,shell script comment multiple lines,advanced shell script examples,shell script practical examples,100 shell programs in unix pdf,script with variables,read shell script,give the command for executing script.,shell script file extension,first line in any shell script begins with,how to create a shell script in linux,how to run shell script in linux,linux shell script example,shell scripting programs in linux with examples,linux script command,-z option in shell script,create a script