Introduction to shell scripting


What is Shell Scripting

example

Look at an example.

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

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

Example explanation

  • Line 1: Specify the script interpreter, here /bin/sh is used as the interpreter
  • Line 2: switch to the current user's home directory
  • Line 3: Create a directory shell_tut
  • Line 4: switch to shell_tut directory
  • Line 5: Loop condition, 10 loops in total
  • Line 6: Create a test_1... 10.txt file
  • Line 7: end of loop body

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

The concept of shell and shell scripting

A shell is an application that provides an interface through which the user accesses the services of the operating system kernel. Ken Thompson's sh was the first Unix Shell, and Windows Explorer is a typical GUI Shell.

A shell script (shell script), is a scripting program written for a shell. By shell the industry usually refers to shell scripts, but readers should know that shell and shell script are two different concepts. For reasons of convention and brevity, all references to "shell programming" in this article refer to shell scripting, not to developing the shell itself (e.g. Windows Explorer extension development).

environments

Shell programming is the same as java and php programming, as long as you have a text editor that can write code and a script interpreter that can interpret and execute it.

OS

Current mainstream operating systems support shell programming, the shell programming described in this document refers to the shell under Linux, talking about basically the functions under the POSIX standard, so that also applies to Unix and BSD (such as Mac OS).

Define the variable

Define the variabletime, Variable names without dollar signs($), as if:

your_name="qinjx"

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

In addition to explicitly assigning values directly, variables can be assigned values with statements such as.

for file in `ls /etc`

Use of variables

To use a defined variable, simply prefix the variable name with a dollar sign, e.g.

your_name="qinjx"
echo $your_name
echo ${your_name}

The brackets outside the variable name are optional, and can be added or not. They are added to help the interpreter identify the variable's bounds, as in the following case.

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

If you don't giveskill Variables in brackets, write asecho "I am good at $skillScript", The interpreter then takes the$skillScript Think of it as a variable( Its value is empty), The code execution will not be what we expect。

It is a good programming habit to recommend adding flower brackets to all variables. When IntelliJ IDEA writes a shell script, the IDE prompts to add the brackets.

attach importance to Define the variable

Variables that have been defined and can be redefined, e.g.

your_name="qinjx"
echo $your_name

your_name="alibaba"
echo $your_name

It's legal to write it that way, However, note that, The second assignment cannot be written$your_name="alibaba", Use of variables Add the dollar sign only when the。

single quote

str='this is a string'

single quote String restrictions:

  • Any characters in single quotes are output as is, and variables in single quoted strings are invalid
  • single quote cannot appear in the string single quote( right single quote Not even after using the escape character)

double quote

your_name='qinjx'
str="Hello, I know your are "$your_name"! 
"
  • You can have variables in double quotes
  • Escape characters can appear in double quotes

string operation

Splice String

your_name="qinjx"
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"

echo $greeting $greeting_1

Gets the length of the string.

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

Extract substrings

string="alibaba is a great company"
echo ${string:1:4} # exports:liba

Find substring

string="alibaba is a great company"
echo `expr index "$string" is`# exports:8, The meaning of this statement is: Find the wordis Position in this name

Recommended>>
1、What does the sharing economy change in our lives and what does it bring to security companies
2、Gene Chain GHT Disrupting Medicine and Bringing about a Revolution in Genetic Science
3、Huang Renxun said NVIDIA volume exceeds AMD ten times layout of artificial intelligence or become the winner
4、Asking Blockchain Hangzhou Station A Methodology for Reconstructing Industrial Ecology in the New Era
5、Xinjiangs first regionallevel geospatial big data construction project launched

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号