A summary of---variables, comparisons, and tests used in shell programming

Source: Internet
Author: User

One, variable

Environment variables, positional variables, pre-defined variables


1. Environment Variables
Env #查看环境变量

2. Position variable
VI location.sh
#!/bin/bash
sum= ' expr + $ '
echo "$ + $ = $SUM"
./location #则12为第一个位置变了 $1,32 changed the second position. Location is a predefined variable

3. Pre-defined variables
$##表示命令行中位置参数的个数
$*#表示所有位置参数的内容
$?#表示执行后的返回状态, 0 is normal.
$#表示当前执行的脚本或程序名称

VI yu.sh
#!/bin/bash
name=bak-' Date +%y%m%d '. tar.gz
Tar zcf $name $* & >/dev/null
echo "Execute $ script"
echo "Complete $# object"
echo "Content is $*"

=========================================================================================


second, condition test operation []
1. File Test
-D #判断是否为directory [-d/etc] && echo "This is a directory"
-E#判断是否exist[-e/var/log/messages] && echo "exist"
-F #判断是否为file[-F/ETC/PASSWD] && echo "This is a file"
-R #判断是否有read[-R Read] && echo "Premission is a read"
-W #判断是否有write[w write] && echo "Premission is a write"
-X #判断是否有execute[-X Execute] && ECHP "Premission is a execute"
-L #判断是否为link
-B #判断是否为black[-b/dev/sr0] && echo "This is a block"
-C #判断是否为chatset[-C/DEV/VCSA] && echo "This is a charset"

2, integer value comparison
-eq #1等于2
-ne #1不等于2
-gt #1大于2
-lt #1小于2
-ge #1大于或等于2
-le #1小于或等于2
User= ' who | Wc-l ' # $user to 3
[$user-lt 4] && echo Yes

3. String comparison
=#第一个字符串等于第二个字符串
!= #第一个字符串不等于第二个字符串
-Z #检查字符串是否为空, a variable that is not defined or given a null value is treated as an empty string
[$LANG = en_US. UTF-8] && echo Yes

4. Logic Test
&& # "Plus", the return value of the entire test command is 0 when both the current face and the next two conditions are true
|| # "Logical OR", as long as one of the preceding and subsequent two conditions is established, the return value of the entire test command is 0
! # "Logical No", the return value of the entire test command is 0 only if the specified condition is not true

[$LANG! = zh_cn. UTF-8] && [$user! =] && echo Yes # # $user =3
[$LANG! = zh_cn. UTF-8] | | [$user! = 3] && echo Yes

A summary of---variables, comparisons, and tests used in shell programming

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.