[root@Redis01 ~]# cd /install/
[root@Redis01 install]# mkdir -p test && cd test

-s修改时间:
[root@Redis01 test]# date
2017年 11月 30日 星期四 21:55:03 CST
[root@Redis01 test]# date `+%F %T %A`
2017年 11月 30日 星期四 21:56:25 CST
[root@Redis01 test]# date '+%F'
2017-11-30
[root@Redis01 test]# date '+%T'
21:57:11
[root@Redis01 test]# date '+%A'
星期四
[root@Redis01 test]# date -s "2017-11-30 14:44:40"
2017年 11月 30日 星期四 14:44:40 CST
更新主板和芯片时间
[root@Redis01 test]# clock -w
[root@Redis01 test]# date '+%F %T %A'
2017-11-30 14:45:47 星期四

[root@Redis01 test]# myname=lisi
[root@Redis01 test]# echo $myname
lisi
[root@Redis01 test]# echo my neme is $myname
my neme is lisi
[root@Redis01 test]# echo $mynameis man
man
[root@Redis01 test]# echo ${myname}is man
lisiis man

说一些指令
小括号()和反单引号效果相似:
[root@Redis01 test]# echo `echo kkk`
kkk
[root@Redis01 test]# echo $(echo kkk)
kkk

数字表示的是下标,下标是从0开始:
[root@Redis01 test]# echo ${#myname}
4
[root@Redis01 test]# echo ${myname:3}
i
[root@Redis01 test]# echo ${myname:4}

单斜线是替换第一次匹配结果,双斜线是全部替换:
[root@Redis01 test]# echo ${myname/si/gang}
ligang
[root@Redis01 test]# echo ${myname/si/}
li
[root@Redis01 test]# myname=liyongfuyongfu
[root@Redis01 test]# echo ${myname/yongfu/gang}
ligangyongfu
[root@Redis01 test]# echo ${myname//yongfu/gang}
liganggang

%表示匹配最后一个;#表示匹配第一个:
[root@Redis01 test]# echo ${myname/%yongfu/gang}
liyongfugang
[root@Redis01 test]# echo ${myname/#yongfu/gang}
liyongfuyongfu
[root@Redis01 test]# echo ${myname/#li/gang}
gangyongfuyongfu

echo输出会换行;printf不会换行:
[root@Redis01 test]# printf kkk
kkk[root@Redis01 test]# echo printf
printf

:-没值时不会自动赋值;而:=没值时会自动赋值;但没值时都会输出默认值;
[root@Redis01 test]# kk=liyongfu
[root@Redis01 test]# echo ${kk:-ligang}
liyongfu
[root@Redis01 test]# echo ${km}

[root@Redis01 test]# echo ${km:-ligang}
ligang
[root@Redis01 test]# echo ${km}

[root@Redis01 test]# echo ${kl:=ligang}
ligang
[root@Redis01 test]# echo ${kl}
ligang

数学运算:
(()):
[root@Redis01 test]# sum=0
[root@Redis01 test]# ((sum=sum+10))
[root@Redis01 test]# echo $sum
10
[root@Redis01 test]# ((sum = sum + 10))
[root@Redis01 test]# echo $sum
20

[root@Redis01 test]# a=10
[root@Redis01 test]# b=20
[root@Redis01 test]# c=+
[root@Redis01 test]# ((sum = ${a} ${c} ${b}))
[root@Redis01 test]# echo $sum
30

[root@Redis01 test]# ex=3+3-5*0/5
[root@Redis01 test]# echo $ex
3+3-5*0/5
[root@Redis01 test]# ((sum = $ex))
[root@Redis01 test]# echo $sum
6

数字比较用-gt/-lt/-eq/-ge/-le/-ne;
字符串比较>、<、==、!=
[root@Redis01 test]# [ 3>2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 > 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 < 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -gt 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -lt 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -ge 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]# [ 3 -le 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -eq 2 ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ 3 -ne 2 ] && echo "yes" || echo "no"
yes
[root@Redis01 test]#
[root@Redis01 test]#
[root@Redis01 test]# he=ligang
[root@Redis01 test]# wo=yongfu
[root@Redis01 test]# [ $he == $wo ] && echo "yes" || echo "no"
no
[root@Redis01 test]# [ $he != $wo ] && echo "yes" || echo "no"
yes

-f判断文件是否存在;-d判断文件夹是否存在;
[root@Redis01 test]# [ -f "$fp" ] && echo "exists" || echo "not exists"

[root@Redis01 test]# echo this is a txt>>a.txt
[root@Redis01 test]# cat a.txt
this is a txt
[root@Redis01 test]# [ -f "$fp" ] && cat a.txt || touch a.txt
this is a txt

[root@Redis01 test]# [ -d "$fp" ] && echo "exists" || echo "not exists"

(1):
[root@Redis01 test]# vi test.sh
[root@Redis01 test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
fp=/install/test/a
[ -d $fp ] && {
  echo "dir is exists";
  echo "dir is exists"
}||{
  echo "dir not exists";
  echo "dir not exosts"
}
[root@Redis01 test]# sh test.sh

(2):
[root@Redis01 test]# vi test.sh
[root@Redis01 test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
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
[root@Redis01 test]# sh test.sh

(3):固定变量值
[root@Redis01 test]# vi test.sh
[root@Redis01 test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
age=90
if [ $age -gt 100 ];then
  echo "他是老年人"
elif [ $age -gt 60 ];then
  echo "他是中老年人"
elif [ $age -gt 40 ];then
  echo "他是青年人"
elif [ $age -gt 20 ];then
  echo "他是少年"
else
  echo "他是小孩"
fi
[root@Redis01 test]# sh test.sh
他是中老年人

(4):运行时,传值
[root@Redis01 test]# vi test.sh
[root@Redis01 test]# cat test.sh
#!/bin/sh
#定义一个变量,变量的值指向一个目录
#第一个参数是$0,它表示当前脚本文件的名字,从数字1开始(即$1,$2...)表示传给脚本文件的名字
fileName=$0
echo "script file is $fileName"
age=$1
if [ $age -gt 100 ];then
  echo "他是老年人"
elif [ $age -gt 60 ];then
  echo "他是中老年人"
elif [ $age -gt 40 ];then
  echo "他是青年人"
elif [ $age -gt 20 ];then
  echo "他是少年"
else
  echo "他是小孩"
fi
[root@Redis01 test]# sh test.sh 35
script file is test.sh
他是少年

序列:
[root@Redis01 test]# echo {1..5}
1 2 3 4 5
[root@Redis01 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
[root@Redis01 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
[root@Redis01 test]# seq 5
1
2
3
4
5

tr截断设置分隔符;
[root@Redis01 test]# seq 5|tr '\n' ' '
1 2 3 4 5 [root@Redis01 test]# seq -s ' ' 5
1 2 3 4 5
-s直接设置分割方式;
[root@Redis01 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 47 48 49 50

1 2 50其中的1表示起始数字,5表示步长,50表示结束数字;
[root@Redis01 test]# seq -s ' ' 1 5 50
1 6 11 16 21 26 31 36 41 46

(())只能运算整数;bc可以运算所有实数;
其中的-l表示导入运行计算所需包;bc是运算标识;
[root@Redis01 test]# a=1.2
[root@Redis01 test]# b=2.6
[root@Redis01 test]# sum=0
[root@Redis01 test]# ((sum=a+b))
[root@Redis01 test]# echo ${a}+${b}|bc -l
3.8
[root@Redis01 test]# echo 190+100|bc -l
290

整数计算推荐使用(()),因为性能比较好;
bc中间使用了管道,走了磁盘IO,性能不好;
[root@Redis01 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
[root@Redis01 test]# seq -s '+' 100|bc -l
5050

反单引号内的内容当作语句执行;
单引号内的内容当作字符串;
[root@Redis01 test]# ((sum=`seq -s '+' 1000`))
[root@Redis01 test]# echo $sum
500500
[root@Redis01 test]# echo $((`seq -s '+' 1000`))
500500
[root@Redis01 test]# echo $((seq -s '+' 1000))
-bash: seq -s '+' 1000: syntax error: invalid arithmetic operator (error token is "'+' 1000")

for循环两种方式:
[root@Redis01 test]# sum=0
[root@Redis01 test]# for i in `seq 100`;do ((sum=${sum}+${i}));done
[root@Redis01 test]# echo $sum
[root@Redis01 test]# for((i=1;i<=100;i++));do ((sum+=${i}));done
[root@Redis01 test]# echo $sum

while的两种方式,注意每次需要坝变量赋值:
[root@Redis01 test]# sum=0
[root@Redis01 test]# i=1
[root@Redis01 test]# while [ $i -le 100 ];do ((sum+=i));((i++));done
[root@Redis01 test]# echo $sum

[root@Redis01 test]# sum=0
[root@Redis01 test]# i=1
[root@Redis01 test]# while ((i <= 100));do ((sum+=i));((i++));done
[root@Redis01 test]# echo $sum

Linux命令-基本变量类型及其运算的更多相关文章

  1. Linux命令的类型

    1.内建命令: 由shell程序自带的命令,最常见的有cd.pwd等. 使用type命令即可查看命令属于哪种,比如: #type cd cd is a shell builtin ————>看到 ...

  2. linux删除某类型文件的命令

    使用linux命令行,删除某目录下某类型的文件,如:删除.rar结尾的所有文件. 命令如下: find . -name "*.rar" -type f -print -exec r ...

  3. 我使用过的Linux命令之file - 检测并显示文件类型

    摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...

  4. linux命令类型及执行顺序

    一 为什么要使用命令行   当初级Linux用户面对缺乏图形界面的Linux时很多人都会抱怨:为何要死守命令行?为什么不采用人机互交好.更简单的图形界面呢?事实上,图形界面在某些任务方面确实高效而且简 ...

  5. Linux命令之type - 显示命令的类型

    用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下之一 alias 别名 keyword 关键字,Shell保留字 function 函数,Shell函数 builtin 内建命令 ...

  6. Linux命令格式、终端类型和获取帮助的方法

    Linux用户类型 Root用户:超级管理员,权限很大 普通用户:权限有限 终端 terminal 终端类型 物理终端:鼠标.键盘.显示器 虚拟终端:软件模拟出来的终端 控制台终端: /dev/con ...

  7. 03_Linux基础-文件类型-主辅提示符-第1提示符-Linux命令-内外部命令-快捷键-改为英文编码-3个时间-stat-其他基础命令

    03_Linux基础-文件类型-主辅提示符-第1提示符-Linux命令-内外部命令-快捷键-改为英文编码-3个时间-stat-{1..100}-du-cd-cp-file-mv-echo-id-she ...

  8. Linux 命令小记

    1. pidof 进程名 :获取进程的pid,例如 pidof memcached 得到5333 2. unset Shell变量 :取消设置一个shell变量,从内存和shell的导出环境中删除它, ...

  9. LINUX命令总结 -------来自 水滴娃娃 的CSDN

    LINUX命令总结 标签: LINUX命令总结 2014-01-27 15:54 41039人阅读 评论(1) 收藏 举报  分类: linux(1)  版权声明:本文为博主原创文章,未经博主允许不得 ...

随机推荐

  1. 【ProtoBuffer】windows上安装ProtoBuffer3.1.0 (附已编译资源)

    ------- 17.9.17更新  --- 以下这些方法都是扯淡,对我的机器不适用,我后来花了最后成功安装并亲测可用的方法不是靠vs编过的,vs生成的库引入后函数全部报undefine refere ...

  2. hdu 1086 You can Solve a Geometry Problem too [线段相交]

    题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...

  3. PB开发境界 多个DW进行update

      多个DW进行update //菜鸟代码dw_1.Update()dw_2.Update()初级代码IF dw_1.Update() = 1 And dw_2.Update() = 1 THEN  ...

  4. 打开xmind 8 输入序列号

    XAka34A2rVRYJ4XBIU35UZMUEEF64CMMIYZCK2FZZUQNODEKUHGJLFMSLIQMQUCUBXRENLK6NZL37JXP4PZXQFILMQ2RG5R7G4QN ...

  5. c# 使用资源文件

    1.新建项目 2.新建资源文件 3. 代码中使用嵌入资源 using System;using System.Collections.Generic;using System.Text;using S ...

  6. UnityTips:使用反射调用内部方法拓展编辑器

    大家都知道Unity是一个C/C++的游戏引擎,C#只是Unity提供的脚本层.因此大部分功能都是通过C#来调用底层的C++代码的.而一些朋友可能不知道的是,其实Unity的C#代码中也有很多方法是我 ...

  7. C++ 基础知识回顾总结

    一.前言 为啥要写这篇博客?答:之前学习的C和C++相关的知识,早就被自己忘到一边去了.但是,随着音视频的学习的不断深入,和C/C++打交道的次数越来越多,看代码是没问题的,但是真到自己操刀去写一些代 ...

  8. wordpress背景添加跟随鼠标动态线条特效

    今天看到别人的博客,在鼠标移动背景时会出现线条动态特效 感觉挺有意思的,还有另一种,在背景点击时会跳出字符特意去找了方法,以为需要添加代码的,结果只要安装个插件就可以了,所以说wordpress就是方 ...

  9. 跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!

    背景 用 Python 做过爬虫的小伙伴可能接触过 Scrapy,GitHub:https://github.com/scrapy/scrapy.Scrapy 的确是一个非常强大的爬虫框架,爬取效率高 ...

  10. django项目微博第三方登录

    此处咱们用到的是 social_django,所以要把此应用注册到配置文件中, INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.a ...