Linux commands-Basic variable types and their operations

Source: Internet
Author: User

[Email protected] ~]# cd/install/
[[email protected] install]# mkdir-p test && CD test

-S modification Time:
[[Email protected] test]# Date
Thursday, November 30, 2017 21:55:03 CST
[[Email protected] test]# Date ' +%f%T%A '
Thursday, November 30, 2017 21:56:25 CST
[[Email protected] test]# Date ' +%f '
2017-11-30
[[Email protected] test]# Date ' +%t '
21:57:11
[[Email protected] test]# Date ' +%a '
Thursday
[[email protected] test]# date-s "2017-11-30 14:44:40"
Thursday, November 30, 2017 14:44:40 CST
Update motherboard and Chip time
[Email protected] test]# clock-w
[[Email protected] test]# Date ' +%f%T%A '
2017-11-30 14:45:47 Thursday


[Email protected] test]# Myname=lisi
[Email protected] test]# echo $myname
Lisi
[Email protected] test]# echo my neme is $myname
My neme is Lisi
[Email protected] test]# echo $mynameis man
Mans
[Email protected] test]# echo ${myname}is man
Lisiis Mans


Say some instructions.
Parentheses () are similar to anti-single-quote effects:
[Email protected] test]# echo ' echo KKK '
Kkk
[[email protected] test]# echo $ (echo KKK)
Kkk


The numbers represent subscripts, and the subscripts start at 0:
[Email protected] test]# echo ${#myname}
4
[[email protected] test]# echo ${myname:3}
I
[[email protected] test]# echo ${myname:4}


The single slash is the replacement of the first match result, and the double slash is all replaced:
[[email protected] test]# echo ${myname/si/gang}
Ligang
[[email protected] test]# echo ${myname/si/}
Li
[Email protected] test]# Myname=liyongfuyongfu
[[email protected] test]# echo ${myname/yongfu/gang}
Ligangyongfu
[[email protected] test]# echo ${myname//yongfu/gang}
Liganggang


% means match last; #表示匹配第一个:
[[email protected] test]# echo ${myname/%yongfu/gang}
Liyongfugang
[[email protected] test]# echo ${myname/#yongfu/gang}
Liyongfuyongfu
[[email protected] test]# echo ${myname/#li/gang}
Gangyongfuyongfu


The echo output will wrap, and printf will not break the line:
[Email protected] test]# printf KKK
Kkk[[email protected] test]# echo printf
Printf


:-The value is not automatically assigned when it is not, and the value is automatically assigned when it is not, but the default value is output when there is no value;
[Email protected] test]# Kk=liyongfu
[[email protected] test]# echo ${kk:-ligang}
Liyongfu
[[email protected] test]# echo ${km}

[Ro[email protected] test]# echo ${km:-ligang}
Ligang
[[email protected] test]# echo ${km}

[[email protected] test]# echo ${kl:=ligang}
Ligang
[[email protected] test]# echo ${KL}
Ligang

Mathematical operations:
(()):
[Email protected] test]# sum=0
[Email protected] test]# ((sum=sum+10))
[Email protected] test]# echo $sum
10
[[Email protected] test]# ((sum = sum + 10))
[Email protected] test]# echo $sum
20

[Email protected] test]# a=10
[Email protected] test]# b=20
[Email protected] test]# c=+
[[Email protected] test]# ((sum = ${a} ${c} ${b}))
[Email protected] test]# echo $sum
30

[Email protected] test]# EX=3+3-5*0/5
[Email protected] test]# echo $ex
3+3-5*0/5
[[Email protected] test]# ((sum = $ex))
[Email protected] test]# echo $sum
6


Digital comparison with-gt/-lt/-eq/-ge/-le/-ne;
string comparison >, <, = =,! =
[[Email protected] test]# [3>2] && echo "Yes" | | echo "No"
No
[[Email protected] test]# [3 > 2] && echo "Yes" | | echo "No"
Yes
[[Email protected] test]# [3 < 2] && echo "Yes" | | echo "No"
Yes
[[Email protected] test]# [3-GT 2] && echo "Yes" | | echo "No"
Yes
[[Email protected] test]# [3-LT 2] && echo "Yes" | | echo "No"
No
[[Email protected] test]# [3-ge 2] && echo "Yes" | | echo "No"
Yes
[[Email protected] test]# [3-le 2] && echo "Yes" | | echo "No"
No
[[Email protected] test]# [3-eq 2] && echo "Yes" | | echo "No"
No
[[Email protected] test]# [3-ne 2] && echo "Yes" | | echo "No"
Yes
[Email protected] test]#
[Email protected] test]#
[Email protected] test]# He=ligang
[Email protected] test]# Wo=yongfu
[Email protected] test]# [$he = = $wo] && echo "Yes" | | echo "No"
No
[Email protected] test]# [$he! = $wo] && echo "Yes" | | echo "No"
Yes


-F to determine if the file exists;-D to determine if the folder exists;
[[Email protected] test]# [-F "$fp"] && echo "exists" | | echo "NOT EXISTS"

[Email protected] test]# echo this is a txt>>a.txt
[email protected] test]# cat A.txt
This is a txt
[[Email protected] test]# [-F "$fp"] && cat A.txt | | Touch A.txt
This is a txt

[[Email protected] test]# [-D "$fp"] && echo "exists" | | echo "NOT EXISTS"

(1):
[Email protected] test]# VI test.sh
[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量, the value of the variable points to a directory
fp=/install/test/a
[-D $fp] && {
echo "DIR is exists";
echo "DIR is exists"
}|| {
echo "dir not exists";
echo "Dir not exosts"
}
[Email protected] test]# sh test.sh

(2):
[Email protected] test]# VI test.sh
[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量, the value of the variable points to a directory
fp=/install/test/a
If [-D $fp];then
echo "DIR is exists";
echo "DIR is exists"
Else
echo "dir not exists";
echo "Dir not Exost"
Fi
[Email protected] test]# sh test.sh

(3): Fixed variable value
[[[email protected] test]# VI test.sh
[[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量, the value of the variable points to a directory
age=90
If [$age-gt];then
Echo "He is old"
Elif [$age-gt];then
Echo " He's middle-aged "
Elif [$age-gt];then
Echo" He's young "
Elif [$age-gt];then
Echo" He's a teenager "
Else
Echo "He's a kid."
fi
[[email protected] test]# sh test.sh
He's middle aged

(4): Run time, pass value
[Email protected] test]# VI test.sh
[email protected] test]# cat test.sh
#!/bin/sh
#定义一个变量, the value of the variable points to a directory
#第一个参数是, which represents the name of the current script file, starting with the number 1 (that is, $1,$2 ...) Represents the name passed to the script file
Filename=$0
echo "script file is $fileName"
Age=$1
If [$age-gt];then
Echo, "He's old."
elif [$age-gt];then
echo "He's middle-aged."
elif [$age-gt];then
echo "He's a young man."
elif [$age-gt];then
echo "He's a teenager."
Else
Echo, "He's a kid."
Fi
[[Email protected] test]# SH test.sh 35
Script file is test.sh
He's a teenager.

Sequence:
[[email protected] test]# echo {1..5}
1 2 3) 4 5
[Email protected] test]# echo {A.. Z
A b c d e F g h i j k l m n o p q R S t u v w x y Z
[Email protected] test]# echo {A.. H
A b c d e F g h I J K L M N O P Q R S T U V W X Y Z [] ^ _ ' a B C D E f g h
[[email protected] test]# SEQ 5
1
2
3
4
5

TR truncation set delimiter;
[[email protected] test]# seq 5|tr ' \ n '
1 2 3 4 5 [[email protected] test]# seq-s ' 5
1 2 3) 4 5
-S directly set the split mode;
[[email protected] test]# seq-s ' 5 50
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 4 7 48 49 50

1 2 50 where 1 represents the starting number, 5 is the step, and 50 is the ending number;
[[email protected] test]# seq-s ' 1 5 50
1 6 11 16 21 26 31 36 41 46


(()) can only operate on integers; BC allows all real numbers to be calculated;
The-L indicates the package required to import the run calculation; BC is the operation ID;
[Email protected] test]# a=1.2
[Email protected] test]# b=2.6
[Email protected] test]# sum=0
[Email protected] test]# ((sum=a+b))
[Email protected] test]# echo ${a}+${b}|bc-l
3.8
[Email protected] test]# echo 190+100|bc-l
290


Integer calculation recommended (()), because the performance is better;
BC in the middle of the use of pipelines, walked the disk IO, poor performance;
[[email protected] test]# seq-s ' + ' 100
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40 +41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+ 77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[[email protected] test]# seq-s ' + ' 100|bc-l
5050

The content inside the inverted single quotation marks is executed as a statement;
The contents of the single quotation mark are treated as strings;
[[Email protected] test]# ((sum= ' seq-s ' + ' 1000 '))
[Email protected] test]# echo $sum
500500
[[email protected] test]# echo $ ((' seq-s ' + ' 1000 '))
500500
[[email protected] test]# echo $ ((seq-s ' + ' 1000))
-bash:seq-s ' + ' 1000:syntax error:invalid arithmetic operator (Error token is "' + ' 1000")

For Loop two ways:
[Email protected] test]# sum=0
[[email protected] test]# for i in ' seq ';d O ((Sum=${sum}+${i}));d one
[Email protected] test]# echo $sum
[[email protected] test]# for ((i=1;i<=100;i++));d O ((Sum+=${i}));d one
[Email protected] test]# echo $sum


While in both ways, note that the dam variable assignment is required every time:
[Email protected] test]# sum=0
[Email protected] test]# I=1
[email protected] test]# while [$i-le];d o ((sum+=i));((i++));d One
[Email protected] test]# echo $sum

[Email protected] test]# sum=0
[Email protected] test]# I=1
[[email protected] test]# while ((i <=));d O ((sum+=i));((i++));d One
[Email protected] test]# echo $sum

Linux commands-Basic variable types and their operations

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.