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



本文转自zh888 51CTO博客,原文链接:http://blog.51cto.com/zh888/1169118,如需转载请自行联系原作者

相关文章
|
8月前
|
存储 Unix Shell
Shell 输出命令完全指南:echo 与 printf 的深度剖析
本文深入解析了 Shell 编程中 `echo` 和 `printf` 两个核心输出命令的用法与区别。`echo` 简单易用,适合基础输出;`printf` 功能强大,支持复杂格式化。文章从语法、转义序列、高级技巧到实际应用场景(如日志记录、进度显示)逐一讲解,并对比两者的性能与适用场景,帮助开发者根据需求灵活选择。最后通过进阶技巧和常见问题解答,进一步提升对两者的掌握程度。
391 1
|
9月前
|
网络协议 Shell 网络安全
面试官想听的不仅是命令——如何结构化回答“容器无Shell时如何测试外网”?
“说说看,如果一个Pod的容器没有Shell,如何测试它能否访问外网?”
面试官想听的不仅是命令——如何结构化回答“容器无Shell时如何测试外网”?
|
11月前
|
运维 Shell 数据库
Python执行Shell命令并获取结果:深入解析与实战
通过以上内容,开发者可以在实际项目中灵活应用Python执行Shell命令,实现各种自动化任务,提高开发和运维效率。
320 20
|
11月前
|
安全 Shell 数据处理
使用Python执行Shell命令并获取结果
在实际应用中,可以根据需要选择适当的参数和方法来执行Shell命令,并处理可能出现的各种情况。无论是系统管理、自动化任务还是数据处理,掌握这些技巧都将极大地提高工作效率。
366 12
|
人工智能 Shell iOS开发
AI Shell:在命令行里“对话” AI ,微软推出将 AI 助手引入命令行的 CLI 工具,打造对话式交互命令行
AI Shell 是一款强大的 CLI 工具,将人工智能直接集成到命令行中,帮助用户提高生产力。AI Shell 支持多种 AI 模型和助手,通过多代理框架提供丰富的功能和灵活的使用模式。
1623 7
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
317 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
Java Shell Windows
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
282 1
|
Unix Shell Linux
常见的shell命令
shell常用命令
319 11
|
Shell 知识图谱
Shell printf 命令
10月更文挑战第3天
125 1
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余