1,$$:显示当前的进程id号

ghostwu@dev:~/linux/shell/how_to_use_var$ cat show_pid.sh
#!/bin/bash
echo $$
sleep
ghostwu@dev:~/linux/shell/how_to_use_var$ bash show_pid.sh &
[]
ghostwu@dev:~/linux/shell/how_to_use_var$ ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid
ghostwu : pts/ :: bash show_pid.sh
ghostwu : pts/ :: grep --color=auto show_pid
ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid | grep -v grep
ghostwu : pts/ :: bash show_pid.sh
ghostwu@dev:~/linux/shell/how_to_use_var$ kill
ghostwu@dev:~/linux/shell/how_to_use_var$ ps -ef | grep show_pid
ghostwu : pts/ :: grep --color=auto show_pid
[]+ Terminated bash show_pid.sh

2,(())用于整数的常用运算符

>把两个整数的运算结果赋值给一个变量,前面要加$

ghostwu@dev:~/linux/shell/how_to_use_var$ a=((+))
bash: syntax error near unexpected token `('
ghostwu@dev:~/linux/shell/how_to_use_var$ a=$((+))
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $a

3,四则运算

ghostwu@dev:~/linux/shell/how_to_use_var$ bash calc.sh
a+b=
a-b=
a*b=
a/b=
a**b=
a%b=
ghostwu@dev:~/linux/shell/how_to_use_var$ cat calc.sh
#!/bin/bash
a=$
b=$
echo "a+b=$(($a+$b))"
echo "a-b=$(($a+$b))"
echo "a*b=$(($a+$b))"
echo "a/b=$(($a+$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"

4,let用于整数运算,类似(())

ghostwu@dev:~/linux/shell/how_to_use_var$ i=
ghostwu@dev:~/linux/shell/how_to_use_var$ let i=i+
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $i

不使用let,是不会计算变量的值

ghostwu@dev:~/linux/shell/how_to_use_var$ i=
ghostwu@dev:~/linux/shell/how_to_use_var$ i=i+
ghostwu@dev:~/linux/shell/how_to_use_var$ echo $i
i+

5,bash内置命令read,通过参数-p 提示信息,读入变量的值

ghostwu@dev:~/linux/std_err_out$ read -p "pls input 2 number:" a b
pls input number:
ghostwu@dev:~/linux/std_err_out$ echo $a $b

10,test -f 判断普通文件是否存在

ghostwu@dev:~/linux/std_err_out$ ls
ghostwu.txt output_error.txt std_out1.txt std_out.txt
ghostwu@dev:~/linux/std_err_out$ test -f ghostwu.txt && echo || echo ghostwu@dev:~/linux/std_err_out$ test -f ghostwu2.txt && echo || echo

test -z 测试字符串长度是否为0

ghostwu@dev:~/linux/std_err_out$ test -z "hello" && echo  || echo 

ghostwu@dev:~/linux/std_err_out$ test -z "" && echo  || echo

中括号[]与test一样.

ghostwu@dev:~/linux/std_err_out$ [ -f ghostwu.txt ] && echo  || echo 

ghostwu@dev:~/linux/std_err_out$ [ -f ghostwu2.txt ] && echo  || echo

11,判断一个变量值或者字符串是否为整数?

利用expr做计算时变量或者字符串必须是整数的规则,把一个变量或字符串和一个已知的整数(非0)相加,看命令返回的值是否为0。如果为0,就认为做加法的变量或字符串为整数,否则不是整数。

ghostwu@dev:~/linux/shell/flow_control$ i=
ghostwu@dev:~/linux/shell/flow_control$ expr $i + >/dev/null
ghostwu@dev:~/linux/shell/flow_control$ echo $? ghostwu@dev:~/linux/shell/flow_control$ i='a'
ghostwu@dev:~/linux/shell/flow_control$ expr $i + >/dev/null >&
ghostwu@dev:~/linux/shell/flow_control$ echo $?

linux shell脚本之-变量极速入门与进阶(2)的更多相关文章

  1. linux shell脚本之-变量极速入门与进阶(1)

    1,如果创建shell脚本? 使用任意文本编辑软件,一般为vim,创建.sh结尾的文件,在文件的最开头用 #!/bin/bash 注明shell的类型 如: ghostwu@dev:~/linux/s ...

  2. Linux shell 脚本中变量的数学计算【转】

    本文转载自:http://blog.csdn.net/qinghezhen/article/details/9194287 首先从一个例子说起: x=1+1 echo $x 你是不是期待着输出2啊?让 ...

  3. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  4. linux shell 脚本攻略学习20--awk命令入门详解

    awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...

  5. Linux Shell 脚本入门

    linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...

  6. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  7. Linux shell 脚本攻略之正则表达式入门

    摘自:<Linux shell 脚本攻略> 下面是类似的解释:

  8. Shell脚本编程30分钟入门

    Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...

  9. Linux Shell编程、变量、控制语句

    为什么要学习Shell编程 1)Linux运维工程师在进行服务器集群管理时,需要编写Shell程序来进行服务器管理. 2)对于JavaEE和Python程序员来说,工作的需要,你的老大会要求你编写一些 ...

随机推荐

  1. samba服务配置(二)

    需求: 某公司销售部门提出一个文件共享需求,要求部门共享目录有三个,第一个共享目录所有销售部门人员都具有可读可写权限: 第二个共享目录所有销售人员只读权限,经理级别的销售人员具有可读可写权限:第三个共 ...

  2. iOS架构模式浅析

    这是以前旧博客在13年规划写的一个系列,写了一部分内容,还没有完成.现在重新整理编写.计划从基础知识六大设计原则.设计模式中类的关系开始,然后会对iOS开发中的常用架构模式进行介绍,最后对GoF的23 ...

  3. 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件

    [源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...

  4. LOJ#6387 「THUPC2018」绿绿与串串 / String (Manacher || hash+二分)

    题目描述 绿绿和 Yazid 是好朋友.他们在一起做串串游戏. 我们定义翻转的操作:把一个串以最后一个字符作对称轴进行翻转复制.形式化地描述就是,如果他翻转的串为 RRR,那么他会将前 ∣R∣−1个字 ...

  5. 音视频编解码——YUV视频格式详解

    一.YUV 介绍 YUV是一种颜色编码方方式,通常由彩色摄像机进行取像,然后把取得的彩色图像信号经过分色.分别放大校正后得到RGB,再经过矩阵变换得到亮度信号Y和两个色差信号B-Y(即U).R-Y(即 ...

  6. Java学习笔记53(网络编程:TCP协议案例)

    简易的案例 客户端: package demo; import java.io.IOException; import java.io.InputStream; import java.io.Outp ...

  7. LeetCode--No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  8. spring boot 上传文件

    spring.servlet.multipart.max-file-size=23KBspring.servlet.multipart.maxRequestSize=23KB <form act ...

  9. Java 架构师+高并发+性能优化+Spring boot大型分布式项目实战

    视频课程内容包含: 高级 Java 架构师包含:Spring boot.Spring cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat.Spring.MongoDB.Zer ...

  10. 如何用python爬取两个span之间的内容

    Python用做数据处理还是相当不错的,如果你想要做爬虫,Python是很好的选择,它有很多已经写好的类包,只要调用,即可完成很多复杂的功能,此文中所有的功能都是基于BeautifulSoup这个包. ...