linux shell脚本之-变量极速入门与进阶(2)
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)的更多相关文章
- linux shell脚本之-变量极速入门与进阶(1)
1,如果创建shell脚本? 使用任意文本编辑软件,一般为vim,创建.sh结尾的文件,在文件的最开头用 #!/bin/bash 注明shell的类型 如: ghostwu@dev:~/linux/s ...
- Linux shell 脚本中变量的数学计算【转】
本文转载自:http://blog.csdn.net/qinghezhen/article/details/9194287 首先从一个例子说起: x=1+1 echo $x 你是不是期待着输出2啊?让 ...
- Linux Shell脚本入门--cut命令
Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...
- linux shell 脚本攻略学习20--awk命令入门详解
awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...
- Linux Shell 脚本入门
linux shell 脚本格式 #!/bin/sh#..... (注释)命令...命令... 使用vi 创建完成之后需设置权限 chmod +x filename.sh 执行命令: ./filena ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
- Linux shell 脚本攻略之正则表达式入门
摘自:<Linux shell 脚本攻略> 下面是类似的解释:
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux Shell编程、变量、控制语句
为什么要学习Shell编程 1)Linux运维工程师在进行服务器集群管理时,需要编写Shell程序来进行服务器管理. 2)对于JavaEE和Python程序员来说,工作的需要,你的老大会要求你编写一些 ...
随机推荐
- pgsqls修改表字段长度
alter table T_RPACT_PROTO_EDIT_RECORD alter column remark type VARCHAR(1024); 需要注意type关键字
- git 命令(补充篇)的本质理解
1 标签, git tag tag_name SHA 本质: 在某次commit 上打上标签tag_name ,标签在代码库中起着"锚点"的作用. 注意: commit 由 SHA ...
- CPU Hardwar
GPU负责把线程块分配到各个SM上处理. CUDA对申请的线程块何时运行,以及在哪个SM上运行是没有保证的.这恰好是GPU的优势,这种方式带来了灵活性,不需程序根据SM的数量去配置程序. 但是一个bl ...
- 笔记:BroadcastReceiver的运行过程
广播概述 广播用来在组件之间传递消息,可以是同进程或跨进程. 广播机制是基于发布订阅的事件驱动模型,使用上比Binder通信(跨进程接口回调)更低耦合.简单. ActivityManagerServi ...
- 杂七杂八的JavaScript
一.input 焦点定位 1.定位input:(this.$refs.searchInput as HTMLInputElement).focus(); 2.定位search,根据css选择器: ...
- MySQL:基础架构和工作流程
[参考文章]:01|基础架构:一条查询语句的执行流程 1. 基本架构 大体来说,MySQL可以分为Server层和存储引擎两部分. Server层包括链接器,分析器,优化器,执行器等,涵盖大多数核心服 ...
- 使用MultipartFile上传文件
转载地址:https://www.cnblogs.com/lunaticcoder/p/9813483.html(具体的看这个这个大佬的博客) 依赖包: <!-- 上传文件依赖组件 --> ...
- linux常用的BootLoader U-boot的前世今生
U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导. U-Boot从FADSROM.8xxROM.PPCBOOT逐步发展演化而 ...
- TCP/IP 笔记 - ICMPv4和ICMPv6 : Internet控制报文协议
ICMP是一种面向无连接的协议,负责传递可能需要注意的差错和控制报文,差错指示通信网络是否存在错误(如目的主机无法到达.IP路由器无法正常传输数据包等.注意,路由器缓冲区溢出导致的丢包不包括在ICMP ...
- Vue + Element UI 实现权限管理系统 前端篇(十四):菜单功能实现
菜单功能实现 菜单接口封装 菜单管理是一个对菜单树结构的增删改查操作. 提供一个菜单查询接口,查询整颗菜单树形结构. http/modules/menu.js 添加 findMenuTree 接口. ...