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,如需转载请自行联系原作者

相关文章
|
7天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
24天前
|
安全 Shell Linux
【Shell 命令集合 系统管理 】Linux 锁定终端 vlock命令 使用指南
【Shell 命令集合 系统管理 】Linux 锁定终端 vlock命令 使用指南
35 1
|
24天前
|
Shell Linux C语言
【Shell 命令集合 系统管理 】Linux 显示系统的平均负载情况 tload命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示系统的平均负载情况 tload命令 使用指南
39 1
|
24天前
|
安全 Shell Linux
【Shell 命令集合 系统管理 】Linux 切换当前用户身份为另一个用户 su命令 使用指南
【Shell 命令集合 系统管理 】Linux 切换当前用户身份为另一个用户 su命令 使用指南
33 1
|
24天前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示当前登录到系统的用户信息 who命令 使用指南
42 1
|
24天前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 显示目前登入系统的用户信息 w命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示目前登入系统的用户信息 w命令 使用指南
42 2
|
24天前
|
存储 Unix Shell
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
32 1
|
24天前
|
存储 Shell Linux
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
30 1
|
24天前
|
搜索推荐 Shell Linux
【Shell 命令集合 系统管理 】Linux 管理用户配置文件 userconf命令 使用指南
【Shell 命令集合 系统管理 】Linux 管理用户配置文件 userconf命令 使用指南
31 2
|
24天前
|
Shell Linux 数据库
【Shell 命令集合 系统管理 】Linux 创建新用户的命令 useradd命令 使用指南
【Shell 命令集合 系统管理 】Linux 创建新用户的命令 useradd命令 使用指南
41 1