Starting the Linux small class (6)

Learn the basics of bash shell
Bash shell script the first line is #! At the beginning, before executing bash shell script, you need to modify the file permissions and ownership, so that it becomes an executable file, permissions using chmod and chown file and consequential amendments ownership.
Use echo output. Use the escape character \ (to cancel the special meaning of a single character), single quotes '' (reserved literal character), double quotation marks "" (does not preserve the dollar sign $ ,, anti-quotes '' backslash \ literals )
[the root @ localhost ~] # # echo Hello

[root@localhost ~]# echo # hello

hello

[the root @ localhost ~] # echo 'Hello'
the Hello
[the root @ localhost ~] # echo " 'Hello'"
'Hello'
using a variable, the variable name is typically uppercase letters, they may be numbers, letters (uppercase and lowercase) and _ underscore character composition, but can not start with a number. = Sign assigned variable value, which spaces can not be used with a variable name or value separated.
Two common types of variable data stored string values and integer values, string values when assigning a variable, it is recommended to enclose quotes, bash avoid interpreted as a space character a word delimiter.
Extended variable, in front of the variable name with a dollar sign $, to recall the variable's value.
Command substitution, the call instruction is replaced outputs the execution command, the command may be in backquotes in the old form to invoke a command replaced, such as '<COMMAND>', however, the preferred method is to use the latest $ () syntax, $ (<COMMAND>).
Arithmetic expansion, perform simple integer arithmetic operator, [] is enclosed, the arithmetic expression is evaluated by using $ bash, and then replace the result of evaluating, using the syntax $ [<EXPRESSION>].
Some common arithmetic expression operators and their meanings
<vARIABLE> ++ variables after incrementation
<vARIABLE> - - after the variable counter is decremented
++ <vARIABLE> variable is incremented pre
- <vARIABLE> pre-decrement variable

  • Unary minus
  • One yuan adder
    ** exponentiation
    • Addition - Subtraction * Multiplication / Division% Remainder
      for loop, loop processing items sequentially one by one <LIST> provided, and exits after the last item in the list process.
      for <the VARIABLE> in <the LIST>; do
      <the COMMAND>
      ...
      <the COMMAND> REFERENCING <the VARIABLE>
      DONE
      conditional statements and control structure optimization bash shell scripts
      binary integer comparison operator for comparing
      -eq equal to [ "$ a" -eq "$ B"]
      -ne is not equal to [ "$ A" -ne "$ B"]
      -gt greater than [ "$ A" -gt "$ B"]
      -ge than or equal to [ "$ a" -ge " B $ "]
      -LT-less than [" $ a "-LT-" $ B "]
      -le less [" $ a "-le" $ b "]
      binary string comparison operator for comparing
      = equal to [" $ a "=" $ B "]
      == equal to [" $ A "==" $ B "]
      ! = not equal to [" $ a "! =" $ b "]

使用条件语句if/then
if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
fi
使用if/then/else语句
if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
else
<STATEMENT>
...
<STATEMENT>
fi
使用if/then/elif/then/else语句
if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
elif <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
else
<STATEMENT>
...
<STATEMENT>
fi
使用case语句
case <VALUE> in
<PATTERN1>
<STATEMENT>
...
<STATEMENT>
;;
<PATTERN2>
<a STATEMENT>
...
<a STATEMENT>
;;
esac
practice what
create a user script to add
[root @ localhost ~] # vim user.sh
# / bin / bash!
IF [$ # -eq 0]; the then
echo 'Please provide parameters'
Exit. 1
Fi
IF [-f $. 1!]; the then
echo 'absent'
Exit. 1
Fi
the while Read Line
do
the useradd -s / bin / Fales $ Line
DONE <$. 1
Starting the Linux small class (6)
[the root @ localhost ~] # the chmod + x user.sh
create a user name file UserList
a
B
C
D
E
D
F
[the root @ localhost ~] # SH user.sh
provide the parameters
[root @ localhost ~] # sh user.sh aaa
absent
[root@localhost ~]# sh user.sh userlist
useradd: user 'd' already exists
[root@localhost ~]# cat /etc/passwd
Starting the Linux small class (6)

Unfinished, continued ~

Guess you like

Origin blog.51cto.com/11293100/2408437