What is shell programming? | Complete Guide

What is Shell Scripting

Shell Programming is also referred to as Shell Script. It is constructed using shell commands i.e. each command in Linux is processes by a shell a single line at a time. In this blog, I will cover What is Shell Programming and How to write shell scripts using different commands.

What is a shell script?

Example

Let’s see an example:

#!/bin/sh
cd ~
mkdir infohubblog
cd infohubblog

for ((i=0; i<10; i++)); do
    touch test_$i.txt
done
what is shell programming

Example explanation

  • Line 1: Specify the script interpreter, here we use /bin/sh as the interpreter
  • Line 2: Switch to the home directory of the current user
  • Line 3: Create a directory infohubblog
  • Line 4: Switch to the infohubblog directory
  • Line 5: Cycle conditions, a total of 10 cycles
  • Line 6: Create a test_1…10.txt file
  • Line 7: End of loop body

cd, mkdir, and touch are all programs that come with the system, usually in the /bin or /usr/bin directory. for, do, done are keywords of the sh scripting language.

The concept of Shell and Shell script

The shell refers to an application program that provides an interface through which the user accesses the services of the operating system kernel. Ken Thompson’s sh is the first Unix Shell, and Windows Explorer is a typical graphical interface Shell.

Shell script (shell script) is a script program written for the shell. The shell in the industry usually refers to a shell script, but readers should know that shell and shell script are two different concepts. For reasons of habit and for the sake of brevity, the “shell programming” in this article refers to shell script programming, not the development of the shell itself (such as Windows Explorer extension development).

Environment

Shell programming is the same as java and php programming, as long as there is a text editor that can write codes and a script interpreter that can interpret and execute.

OS

The current mainstream operating systems all support shell programming. The shell programming described in this document refers to the shell under Linux. It basically talks about the functions under the POSIX standard. Therefore, it is also applicable to Unix and BSD (such as Mac OS).

Linux

The default installation of Linux comes with a shell interpreter.

Mac OS

Mac OS not only brings the two most basic interpreters of sh and bash, but also built-in interpreters that are not commonly used such as ksh, csh, and zsh.

Emulator on Windows

Windows does not have a built-in shell interpreter when it leaves the factory, so you need to install it yourself. In order to use grep, awk, curl and other tools at the same time, it is best to install a cygwin or mingw to simulate the linux environment.

Script interpreter

sh

That is, Bourne shell, POSIX (Portable Operating System Interface) standard shell interpreter, its binary file path is usually /bin/sh, developed by Bell Labs.

This article is about sh. If you use other languages ​​for shell programming, please refer to the documentation of the corresponding language yourself.

bash

Bash is an alternative to the Bourne shell and belongs to the GNU Project. The binary file path is usually /bin/bash. The industry usually mixes bash, sh, and shell. For example, you will often see in the copywriting of hiring operation and maintenance engineers: familiar with Linux Bash programming, proficient in Shell programming.

In CentOS, /bin/sh is a symbolic link to /bin/bash:

[[email protected] ~]# ls -l /bin/*sh
-rwxr-xr-x. 1 root root 903272 Feb 22 05:09 /bin/bash
-rwxr-xr-x. 1 root root 106216 Oct 17  2012 /bin/dash
lrwxrwxrwx. 1 root root      4 Mar 22 10:22 /bin/sh -> bash

But not on Mac OS. /bin/sh and /bin/bash are two different files, although their size differs only by about 100 bytes:

[email protected] infohubblog % ls -l /bin/*sh
-r-xr-xr-x  1 root  wheel   623472 Sep 22  2020 /bin/bash
-rwxr-xr-x  1 root  wheel   529424 Sep 22  2020 /bin/csh
-rwxr-xr-x  1 root  wheel   110848 Sep 22  2020 /bin/dash
-r-xr-xr-x  1 root  wheel  1300256 Sep 22  2020 /bin/ksh
-rwxr-xr-x  1 root  wheel    31440 Sep 22  2020 /bin/sh
-rwxr-xr-x  1 root  wheel   529424 Sep 22  2020 /bin/tcsh
-rwxr-xr-x  1 root  wheel   637840 Sep 22  2020 /bin/zsh

High-level programming language

In theory, as long as a language provides an interpreter (not just a compiler), this language can be competent for script programming. Common interpreted languages ​​can be used for script programming, such as: Perl, Tcl, Python , PHP, Ruby. Perl is the oldest scripting programming language, and Python has become a pre-built interpreter for some Linux distributions over the years.

Compiled languages, as long as they have an interpreter, can also be used for script programming. For example, C shell is built-in (/bin/csh), Java has a third-party interpreter Jshell, and Ada has a paid interpreter AdaScript.

The following is an example of a PHP Shell Script (assuming the file name is test.php):

#!/usr/bin/php
<?php
for ($i=0; $i < 10; $i++)
        echo $i . "\n";

implement:

/usr/bin/php test.php

or:

chmod +x test.php
./test.php

How to choose a shell programming language

Familiar vs unfamiliar

If you have mastered a programming language (such as PHP, Python, Java, JavaScript), it is recommended that you directly use this language to write scripts, although some places will be a bit verbose, but you can use it in this language field Experience (unit testing, single-step debugging, IDE, third-party libraries).

The new learning cost is very small, as long as you learn how to use the shell interpreter (Jshell, AdaScript).

Simple vs. Advanced

If you feel that writing shell scripts in a familiar language (such as Java, C) is too verbose, you just want to do things like backup files, install software, download data, and learn to use sh, bash will be a good idea.

The shell only defines a very simple programming language, so if your script program is more complex, or the data structure to be manipulated is more complex, then you should still use scripting languages ​​such as Python and Perl, or you should already have Very good high-level language. Because sh and bash are weak in this respect, for example:

  • Its functions can only return strings, not arrays
  • It does not support object-oriented, you can not achieve some elegant design patterns
  • It is interpreted and executed while interpreting. It is not even precompiled like PHP. If your script contains errors (for example, calling a non-existent function), as long as this line is not executed, no error will be reported.

Environmental compatibility

If your script is provided to other users, use sh or bash, your script will have the best environmental compatibility. Perl has been standard for Linux for a long time, and python has become the standard for some Linux distributions over the years. As for Mac OS, it installs Perl, python, ruby, PHP, Java and other mainstream programming languages ​​by default.

The first shell script

write

Open a text editor and create a new file with the extension name sh (sh stands for shell). The extension name does not affect the execution of the script. It is good to know what the name means. If you write a shell script in PHP, use PHP as the extension name.

Enter some code, the first line is generally like this:

#!/bin/bash
#!/usr/bin/php

“#!” is an agreed mark, which tells the system what interpreter the script needs to execute.

run

There are two ways to run Shell scripts:

As an executable program

chmod +x test.sh
./test.sh

Note that it must be written as ./test.sh instead of test.sh. It is the same for running other binary programs. Write test.sh directly, and the linux system will look for test.sh in the PATH, but only /bin , /sbin, /usr/bin, /usr/sbin, etc. are in the PATH, your current directory is usually not in the PATH, so if you write test.sh, you won’t find the command. Use ./test.sh to tell the system, Just find it in the current directory.

To run a bash script in this way, the first line must be written correctly so that the system can find the correct interpreter.

The “system” here is actually the shell application (imagine Windows Explorer), but I deliberately wrote it as a system to facilitate understanding. Since this system refers to the shell, is it a script that uses /bin/sh as an interpreter? Can the first line be omitted? Yes.

As an interpreter parameter

This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:

/bin/sh test.sh
/bin/php test.php

Scripts run in this way do not need to specify the interpreter information in the first line, and it is useless to write them.

variable

Define variables

When defining a variable, do not add a dollar sign ($) to the variable name, such as:

your_name="Durgesh Kashyap"

Note that there can be no spaces between the variable name and the equal sign, which may be different from all programming languages ​​you are familiar with.

In addition to explicitly assigning values ​​directly, you can also use statements to assign values ​​to variables, such as:

for file in `ls /etc`

Use variables

To use a defined variable, just add a dollar sign in front of the variable name, such as:

your_name="Durgesh Kashyap"
echo $your_name
echo ${your_name}

The curly braces outside the variable name are optional. You can add them or not. The curly braces are to help the interpreter identify the boundaries of the variables. For example, in the following situation:

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"
done

If you don’t add curly braces to the skill variable and write echo “I am good at $skillScript”, the interpreter will treat $skillScript as a variable (its value is empty), and the code execution result is not what we expect.

It is recommended to add curly braces to all variables. This is a good programming practice. When IntelliJ IDEA writes a shell script, the IDE will prompt to add curly braces.

Redefine variables

The defined variables can be redefined, such as:

your_name="Durgesh Kashyap"
echo $your_name

your_name="alibaba"
echo $your_name

It is legal to write in this way, but note that you cannot write $your_name=”alibaba” in the second assignment, and only add the dollar sign when using variables.

Annotation

Lines beginning with “#” are comments and will be ignored by the interpreter.

Multi-line comments

There is no multi-line comment in sh, only one # sign can be added to each line. like this:

#--------------------------------------------
# This is a script to automatically open ipa, based on webfrogs's ipa-build:https://github.com/webfrogs/xcode_shell/blob/master/ipa-build

# Function: automatically package for eao ios app, output is ipa package of 14 channels
# Features: automatic packaging, no need to enter any parameters
#--------------------------------------------

##### User configuration area start #####
#
#
# The root directory of the project, it is recommended to put this script in the root directory of the project, there is no need to change it here
# Application name, make sure it is consistent with the target_name.app name under Product in Xcode
#
##### User configuration area end  #####

During the development process, if a large section of code needs to be commented temporarily, and then uncommented after a while, what should I do? Adding a # symbol to each line is too laborious. You can enclose the code to be annotated with a pair of curly braces and define it as a function. If there is no place to call this function, this code will not be executed, and it will be the same as the comment.

String

The string is the most commonly used and most useful data type in shell programming (except for numbers and strings, there are no other types that are easy to use, haha). Strings can be in single quotes, double quotes, or without quotes. The difference between single and double quotes is similar to PHP.

apostrophe

str='this is a string'

Restrictions on single-quoted strings:

  • Any characters in single quotes will be output as-is, variables in single quoted strings are invalid
  • Single quotation marks cannot appear in single quotation marks (not even after using escape characters for single quotation marks)

Double quotes

your_name='Durgesh Kashyap'
str="Hello, I know your are \"$your_name\"! \n"
  • Variables can be in double quotes
  • Escape characters can appear in double quotes

String manipulation

Concatenated string

your_name="Durgesh Kashyap"
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"

echo $greeting $greeting_1

Get the length of the string:

string="abcd"
echo ${#string} #Output:4

Extract substring

string="K Tech is a great company"
echo ${string:1:5} #Output:Tech

Find substring

string="K Tech is a great company"
echo `expr index "$string" is`#Output: 8, the meaning of this sentence: find out the position of the word is in this sentence

Array

Unlike languages ​​such as Java and PHP, the flow control of sh cannot be empty, such as:

<?php
if (isset($_GET["q"])) {
    search(q);
}
else {
    //do nothing
}

You can’t write this in sh/bash. If the else branch is not executed, don’t write this else.

Also note that if [$foo -eq 0] in sh, this square bracket is very different from the parenthesis after if in Java/PHP, it is an executable program (same as cd, ls, grep), unexpected Bar? On CentOS, it is in the /usr/bin directory:

ll /usr/bin/[
-rwxr-xr-x. 1 root root 33408 6  22 2012 /usr/bin/[

Because the square bracket is an executable program here, a space must be added after the square bracket, and it cannot be written as if [$foo -eq 0]

if else

if

if condition
then
    command1 
    command2
    ...
    commandN 
fi

Write as one line (applicable to terminal command prompt):

if `ps -ef | grep ssh`;  then echo hello; fi

The fi at the end means if spelled in reverse, and you will encounter similar ones later

if else

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if else-if else

if condition1
then
    command1
elif condition2
    command2
else
    commandN
fi

for while

for

Demonstrated in the opening example:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

Write in one line:

for var in item1 item2 ... itemN; do command1; command2… done;

C-style

for

for (( EXP1; EXP2; EXP3 ))
do
    command1
    command2
    command3
done

while

while condition
do
    command
done

Infinite loop

while :
do
    command
done

or

while true
do
    command
done

or

for (( ; ; ))

until

until condition
do
    command
done

case

case "${opt}" in
    "Install-Puppet-Server" )
        install_master $1
        exit
    ;;

    "Install-Puppet-Client" )
        install_client $1
        exit
    ;;

    "Config-Puppet-Server" )
        config_puppet_master
        exit
    ;;

    "Config-Puppet-Client" )
        config_puppet_client
        exit
    ;;

    "Exit" )
        exit
    ;;

    * ) echo "Bad option, please choose again"
esac

The syntax of case is very different from the C family language. It requires an esac (the case is reversed) as the end tag. Each case branch uses a closing parenthesis, and two semicolons are used to indicate a break.

function

File contains

You can use source and. Keywords, such as:

source ./function.sh
. ./function.sh

In bash, source and. Are equivalent. They both read the content of function.sh and execute its content (similar to include in PHP). For better portability, the second way of writing is recommended.

Including a file is the same as executing a file. You must also write the path of the file. You can’t just write the file name. For example, in the above example:

. ./function.sh

Can’t write:

. function.sh

If function.sh is a parameter passed in by the user, how to get its absolute path? the way is:

real_path=`readlink -f $1`#$1 is the parameter entered by the user, such as function.sh
. $real_path

Hope you like our blog. Please subscribe to our blog for receiving updates on email.

Happy Coding!

Be the first to comment on "What is shell programming? | Complete Guide"

Leave a comment

Your email address will not be published.


*