Shell programming specifications and variables

Shell script concept

  • The command to be executed in order to save a text file
  • Executable permissions to the file, you can run
  • Shell various control statements may be combined to perform more complex operations

Shell script scenarios

  • Repetitive operations
  • Batch Transactions
  • Automated operation and maintenance
  • Service health monitoring
  • Timing task execution
  • .......

Shell role - command interpreter, "translator"

  • Interposed between the kernel and user, is responsible for interpreting the command line

The user's login Shell

  • After logging in default of Shell program, usually/bin/bash
  • Different Shell internal instructions, operating environment, etc. will be different

    [root@localhost ~]# cat /etc/shells
    /bin/sh
    /bin/bash
    /sbin/nologin
    ......

    Write script code

  • Use via text editor
  • One per line Linuxcommand, executed in the order written
[root@localhost ~]# vim first.sh
cd /boot/
pwd
ls -lh vml*

Given executable permissions

  • Making your script executable property
[root@localhost ~]# chmod +x first.sh        

Execute the script file

  • Method One: Script file path (absolute and relative paths)

    [root@localhost ~]# ./first.sh          //必须拥有x(执行)权限,执行文件,但不会改变所在路径
  • Method Two: sh script file path

    [root@localhost ~]# sh /first.sh      //执行文件,但不会改变所在路径
  • Method three: source script file path

    [root@localhost ~]# source /first.sh
  • Method four: the script file path

    [root@localhost ~]# . /first.sh

Better script constitution

  • Script statements
  • Comment Information
  • Executable statement
    [root@localhost ~]# vim /first.sh
    #!/bin/bash
    # This is my first Shell-Script.
    cd /boot
    echo "当前的目录位于:”                 //输出友好提示信息
    pwd
    echo "其中以vmI开头的文件包括:”
    Is -Ih vml*

    Redirection and pipeline operations

Interactive hardware

  • Standard Input: receiving input data from a user of the device
  • Standard Output: output data to the user through the apparatus
  • Standard Error: execution error messages through the device report
Types of Device file File Description Number Default Device
Standard Input /dev/stdin 0 keyboard
Standard output /dev/stdout 1 monitor
Standard error /dev/stderr 2 monitor

Redirection

Types of Operators use
Redirect input < Reads data from the specified file, rather than from the keyboard
Redirect output > Save the output to the specified file (overwrite the original content)
>> The output is appended to the specified file
Standard error output 2> Save the error information to the specified file (overwrite the original content)
2>> The error message is appended to the specified file
Mix output &> Save the standard output and standard error to the same file content

Pipeline Operation Symbol“|”

  • The left side of the output command, the command processing target as the right

    cmd1 | cmd2 [...| camdn]
    [root@locadhost ~]# grep "bash$" /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    Teacher:/home/teacher:/bin/bash
    [root@localhost ~]# grep“bash$”letc/passwd I awk -F: '{print $1,$7}'
    root /bin/bash
    teacher /bin/bash

The role of Shell variables, type

The role of variables

  • For the flexible management Linuxto provide specific parameters of the system, there are two meanings

    • Variable names: use a fixed name, preset by the system or user-defined

    • Variable values: it can be set varies depending on the user, the system environment

      Type of the variable

  • Custom variable: defined by the user, modify and use
  • Environment variables: maintained by the system for setting the work environment
  • Location variables: to pass parameters to the script from the command line
  • Predefined variables: Bashbuilt for a class of variables can not be directly modified

    Custom Variables

  • Variable names begin with a letter or an underscore, case sensitive, all-caps recommendations
    变量名=变量值

    View a variable value

    echo $变量名
    [root@locadhost ~]# Rroduct=Python
    Version=2.7.13
    [root@locadhost ~]# ehco $Product
    Python
    [root@locadhost ~]# ehco $Product $Version
    Python 2.7.13

    Use quotation marks when assignment

  • Double quotes: Allows the $reference variable value other symbols
  • Single quotes: Do not quote the value of other variables, $treated as ordinary characters
  • Anti apostrophe: Replace command, the extract command output

    Content is entered from the keyboard variable assignment

    resd [-p "提示信息"] 变量名

    Object-oriented: good object will be built over a direct call using
    process-oriented: each process have their own input

Setting a variable scope

格式1:export 变量名 ...
格式2:export 变量名=变量值 ...
  • Both formats can be mixed
    [root@locadhost ~]# echo "$Producvt $Version"
    Benet 6.0
    [root@locadhost ~]# export Product Version
    [root@locadhost ~]# bash
    [root@locadhost ~]# echo "$Product $Version"
    Benet 6.0

    Integer variable computing

    expr 变量1 运算符 变量2  [运算符 变量3]...

    Common operator

  • Adding:+
  • Subtraction:-
  • Multiplication:\*
  • Division:/
  • Modulus (remainder) operator:%

Special Shell Variables

Environment Variables

  • Created by the system in advance, to set the user's working environment
  • Profile:/etc/profile ,~/.bash_ profile

Common environmental variables

  • PWD、PATH
  • USER、SHELL、 HOME
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/root/bin
[root@localhost ~]# PATH="$PATH:/root"
[root@localhost ~]# echo $PATH
 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/root/bin:/root

Position variable

  • He expressed as $ n, n is a number between 1 to 9
[root@localhost ~]# ./myprog.sh one two three four five six //$1,第1个位置参数,$2,第2个位置参数

+ Format requirements back representatives

Predefined variables

  • $ #; The number of variables in the command line position
  • $ *: The contents of all position variables
  • $:? The status returned after the execution of the previous command, when the return value of 0 indicates the state to perform normal, non-zero value indicates that the implementation of an exception or error
  • $ 0: currently executing process / program name
[root@localhost ~]# vim mybak.sh
#!/bin/bash
TARFILE=beifen-'date +%s .tgz
tar zcf $TARFILE $* &> /dev/null
echo "已执行$0脚本,“
echo“共完成$#个对象的备份”
echo”具体内容包括: $*”
[root@localhost ~]# chmod +x mybak.sh
[root@localhost ~]# ./mybak.sh /etc/passwd /etc/shadow
已执行./mybak.sh脚本,
共完成2个对象的备份
具体包括:/etc/passwd /etc/shadow

Guess you like

Origin blog.51cto.com/14473285/2440026