shell命令总结3
1.[root@localhost ~]# date "+%Y%m%d %H%M%S" //在脚本中date这个命令它的作用是用来打印当前系统时间。%Y表示年,%m表示月,%d表示日期,%H表示小时,%M表示分钟,%S表示秒
20130402 104156
2.[root@localhost sbin]# cat zh888.sh // 编写一个求和的脚本
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
a=1
b=2
sum=$[$a+$b]
echo "sum is $sum"
数学计算要用’[ ]’括起来并且外头要带一个’$’。脚本结果为:
[root@localhost sbin]# sh zh888.sh
sum is 3
3.[root@localhost sbin]# cat zh888.sh //shell还可以交互模式
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
echo "please input a number:"
read x
echo "please input another number:"
read y
sum=$[$x+$y]
echo "the sum of tow numbers is: $sum"
[root@localhost sbin]# sh zh888.sh //这就用到了read命令了,它可以从标准输入获得变量的值,后跟变量名。”read x”表示x变量的值需要用户通过键盘输入得到。脚本执行过程如下:
平时在sh -x zh888.sh来查看执行过程用于排错。
please input a number:
22
please input another number:
33
the sum of two numbers is: 55
4.[root@localhost sbin]# cat zh888.sh //read -p 选项类似echo的作用
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
read -p "please input a number:" x
read -p "please input another number:" y
sum=$[$x+$y]
echo "the sum of tow numbers is: $sum"
[root@localhost sbin]# sh zh888.sh //执行脚本如下:
please input a number:22s
please input another number:33
the sum of two numbers is: 55
5.[root@localhost sbin]# cat zh888.sh //在脚本中,你会不会奇怪,哪里来的$1和$2,这其实就是shell脚本的预设变量,其中$1的值就是在执行的时候输入的1,而$2的值就是执行的时候输入的$2,当然一个shell脚本的预设变量是没有限制的
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
sum=$[$1+$2]
echo $sum
[root@localhost sbin]# sh -x zh888.sh 1 33//执行过程如下:
+ sum=34
+ echo 34
34
6.[root@localhost sbin]# cat zh888.sh //另外还有一个$0,不过它代表的是脚本本身的名字。不妨把脚本修改一下。
#!/bin/bash
# shell script zh888.sh
#for get two numbers sum
#zh888 2013-04-02
echo "$0 $1 $2"
[root@localhost sbin]# sh zh888.sh 1 3 //执行结果如下:
zh888.sh 1 3
7.[root@localhost sbin]# cat ifzh888.sh ///出现了 ((a<60))这样的形式,这是shell脚本中特有的格式,用一个小括号或者不用都会报错,请记住这个格式.
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02
read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
fi
在shell脚本中我们同样可以使用if逻辑判断。在shell中if判断的基本语法为:
1)不带else
if 判断语句; then
command
fi
2)带有else
if 判断语句 ; then
command
else
command
fi
[root@localhost sbin]#cat ifzh888.sh //添加else语句:
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02
read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
else
echo "good you passed the exam."
fi
[root@localhost sbin]# sh -x ifzh888.sh //执行过程如下:
+ read '-pplease input you score:' a
please input you score:23
+ (( a<60 ))
+ echo 'you didn'\''t pass the exam.'
you didn't pass the exam.
[root@localhost sbin]# sh -x ifzh888.sh
+ read '-pplease input you score:' a
please input you score:78
+ (( a<60 ))
+ echo 'good you passed the exam'
good you passed the exam
3)带有elif
if 判断语句一 ; then
command
elif 判断语句二; then
command
else
command
fi
[root@localhost sbin]# cat ifzh888.sh //脚本修改如下:
#!/bin/bash
# shell script zh888.sh
#using "if"in script.
#zh888 2013-04-02
read -p"please input you score:" a
if((a<60));then
echo "you didn't pass the exam."
elif ((a>60))&&((a<85));then
echo "good! you passed the exam"
else
echo "very good you score is very high!"
fi
转载于:https://blog.51cto.com/zh888/1169118
shell命令总结3相关推荐
- Python 标准库之 os (获取当前目录、读取/设置环境变量、重命名文件、运行shell命令、创建/删除/查看目录文件、判断目录/文件/存在、获取绝对路径、获取文件名、获取换行符、获取路径分隔符)
1. os与sys模块的官方解释如下: os This module provides a portable way of using operating system dependent funct ...
- 2021年大数据Kafka(四):❤️kafka的shell命令使用❤️
全网最详细的大数据Kafka文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 Kafka的shell命令使用 一.创建topic 二.生产 ...
- 2021年大数据Hadoop(八):HDFS的Shell命令行使用
2021大数据领域优质创作博客,带你从入门到精通,该博客每天更新,逐渐完善大数据各个知识体系的文章,帮助大家更高效学习. 有对大数据感兴趣的可以关注微信公众号:三帮大数据 目录 HDFS的Shell命 ...
- jenkins 插件执行shell命令时,提示“Command not found”处理方法
首先提示找不到"Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具 ...
- hbase 数据插入指定rowkey_「HBase大爆炸」HBase之常用Shell命令
HBase之常用Shell命令 1.进入 HBase客户端命令操作界面 2.查看帮助命令 3.查看当前数据库中有哪些表 4.创建一张表 创建user表,包含info.data两个列族 或者 5.添加数 ...
- awk命令中执行多条shell命令
awk中使用的shell命令,有2种方法: 一.使用system() 二.使用print cmd | "/bin/bash" http://www.gnu.org/software ...
- 【Linux学习笔记】 -- 基本Shell命令
常见的目录名均基于文件系统层级标准(filesystem hierarchy standard,FHS) Linux的四个部分: 1 Linux内核:控制所有硬软件,必要时分配硬件根据需要执行软件 系 ...
- Linux中shell命令的用法和技巧
使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...
- Linux shell命令总结
01 前言 Linux shell命令应该算是非常入门的东西,但是实际上在使用的时候,会遇到各种各样的问题,前几天我在我们的项目上需要做一个功能,根据进程名字杀死这个进程,下面是过程 1.我们正常需要 ...
- Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群(三十六)...
第一种:普通做法 首先,编号写WordCount.scala程序. 然后,打成jar包,命名为WC.jar.比如,我这里,是导出到windows桌面. 其次,上传到linux的桌面,再移动到hdf ...
最新文章
- 要求用户提供输入,直到他们给出有效的答复
- JS事件:target与currentTarget区别
- CS231n课程笔记翻译:图像分类笔记(下)
- 首发:适合初学者入门人工智能的路线及资料下载
- JavaScript学习(十)
- vue加跨域代理静态文件404_vue-cli 设置跨域代理 线上地址报404
- C++继承和组合——带你读懂接口和mixin,实现多功能自由组合
- Golang web filter 轻量级实现
- 【Vegas原创】Exchange报550 5.7.1 Unable to relay for …错误的分析
- Python学习(三)-----用户输入和while循环
- 龙芯OpenJDK更新策略:没必要跟进小版本,最后大版本更新
- C语言程序设计 目录
- web开源FlowPlayer视频播放器
- CentOS7服务器安装GPU显卡驱动和CUDA简单方法
- 数据分析 - 预测模型(学习笔记)
- Linux之用户和权限
- Keras实现小数量集图片分类——6类别Birds数据集分类
- 网易的java微专业_网易微专业Java高级架构师
- Win10电脑连接不上无线网络怎么解决
- 【用es6写个机选彩票】
热门文章
- 文件锁操作《精通Unix下C语言编程与项目实践》之三
- Jupyter Notebook 常用的快捷键
- Codeforces 985C (贪心)
- (转)计算机网络基础知识总结
- js确保正确this的几种写法
- Web---JSP-EL表达式
- JSP-05- JSP总结
- Javascript 你不知道的事
- 真机测试报错ERROR/AndroidRuntime: java.lang.RuntimeException: setParameters failed解决办法
- windows7 安装IIS没有default web site 解决方法