Introduction to shell scripting
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
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.
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).
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.
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 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`
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.
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。
str='this is a string'
single quote String restrictions:
your_name='qinjx' str="Hello, I know your are "$your_name"! "
your_name="qinjx" greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting $greeting_1
string="abcd" echo ${#string} #output: 4
string="alibaba is a great company" echo ${string:1:4} # exports:liba
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