Shell Basics: Variable Types & Operators

Source: Internet
Author: User
Tags echo command

Shell variable shell supports three types of variables
    • User-defined variables: User-defined variables, variable names begin with an English letter or underscore, and are case-sensitive.
    • Positional variables: Variables that are passed to the script according to location, default support 9 position variable $1,$2,$3...$9
    • Predefined variables: Predefined special variables that can only be used and cannot be modified
User-defined variables
#定义变量: Variable name = variable value, you can use the "constant/other variable/Command execution result" assignment var1= ' test var ' var2= "Test variable $var 1" var3= ' Date ' #单引号中将 $ is treated as $, does not recognize $ as a special symbol, Cannot refer to his variable echo ' The price is $ $ ' #双引号中可以通过 $ To reference the variable, but the escape symbol cannot be resolved, to resolve the escape character and font format needs to be preceded by the echo command with the-e parameter echo "test/n test" ECHO-E "test/n Test "#" can run out of script commands and assign values to the corresponding variables, and the following results will print out 123.sql results and the current date str= ' cat 123.sql ' strdate= ' Date ' echo $str $date ' #使用变量: $ variable name echo "$var $var 1" #变量后直接连接字符串需要用 {} separated so that the variable can recognize the echo "Test variable ${var}test" # clears the variable unset var1
Positional variables

New execute Database command script based on parameters

#!/bin/bash#mysql.shconn= "/usr/local/mysql/bin/mysql-uroot-p123" Case ' in   select        ' sql= "select * FROM Test.user "        ;;   Delete)       sql= "Delete from Test.user where ID =$2"       ;;   Insert)      sql= "insert into Test.user (UNAME,PSD) VALUES (' $ $ ', ' $ $ ')";      ;   Update)     sql= "Update test.user set Uname= ' $ $ ', psd= ' $4 ' where id= ' $"     ;; Esac$conn-e "$sql"

Run the script and enter a different number of parameters with a space separator

#显示test数据库下user表的所有数据
./mysql.sh Select
# Delete the user with the specified ID
./mysql.sh Delete 4
#插入新用户
./mysql.sh Insert NewUser NEWPSD
#更新id = 2 User Properties
./MYSQLSH Update 2 Newuser2 NEWPSD2
Pre-defined variables

The shell script supports four forms of predefined variables:

    • $#: The number of all positional parameters
    • $*: Contents of all positional parameters
    • $: The currently executing script name (program or process name)
    • $?: The status of the previous command execution, for 0 for execution success, for other numbers (eg:127 ...) Failed on behalf of execution
!/bin/bash#test Pre-defined bash variableEcho "the location parameter of the current script has ${#}"Echo "the name of the script currently executing is: $"if[ [ $? ==0] ]; Then Echo "the previous command executed successfully"Else  Echo  "the previous command failed to execute"fi forIinch$*; Do Echo "position variable is $i" Done

Operator

The shell is a scripting language, variables do not distinguish between types, the program at the time of actual operation according to the context of the type of judgment, the default type is string, if you need to treat variables as an int type for subtraction operations, you need to use special syntax.

    • $ ((variable 1 operator 1 variable 2 operator 2 variable 3): This method is used more, eg: $ ((2+3+4)); $ ((2*3))
    • Expr variable 1 operator variable 2 operator variable 3: The variable and operator must be separated by a space, this method is less used, and multiplication to use transfer \*, eg:expr 2 + 3 + 4; Expr 2 \* 3

Shell Common operators

    • +
    • /
    • %

Shell Basics: Variable Types & Operators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.