加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell训练day6 8.20

发布时间:2020-12-16 01:43:19 所属栏目:安全 来源:网络整理
导读:shell 看100 道linux shell题。 函数减少重复代码。 shell是系统命令的集合 第一行以#!/bin/bash开头 #!/bin/bash echo "123" w ls 特殊情况:chkconfig 和description不是注释。 sh -x 1.txt sh -n 1.txt 查看脚本执行过程 bash -x 1.sh 查看脚本是否语法错
shell
看100 道linux shell题。
函数减少重复代码。
shell是系统命令的集合

第一行以#!/bin/bash开头
#!/bin/bash
echo "123"
w
ls
特殊情况:chkconfig 和description不是注释。

sh -x 1.txt

sh -n 1.txt

查看脚本执行过程 bash -x 1.sh
查看脚本是否语法错误 bash -n 1.sh

?date +%Y-%m-%d,date +%y-%m-%d 年月日
? date +%H:%M:%S = date +%T 时间
? date +%s 时间戳
? date -d @1504620492
? date -d "+1day" 一天后
? date -d "-1 day" 一天前
? date -d "-1 month" 一月前
? date -d "-1 min" 一分钟前
? date +%w,date +%W 星期

[[email?protected] shell]# date +%F
2019-08-22

[[email?protected] shell]# date +%T
10:12:42

[[email?protected] shell]# date +%F-%T
2019-08-22-10:13:29
[[email?protected] shell]# date +%Y-%m-%d-%H:%M:%S
2019-08-22-10:14:03

[[email?protected] shell]# date +%w --周几
4
[[email?protected] shell]# date +%W --今年的第多少周
33

[[email?protected] shell]# cal
August 2019
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

一天前
[[email?protected] shell]# date -d "-1 day" +%F
2019-08-21
一个月前
[[email?protected] shell]# date -d "-1 month" +%F
2019-07-22
一年前
[[email?protected] shell]# date -d "-1 year" +%F
2018-08-22

[[email?protected] shell]# date +%s --时间戳
1566440271
[[email?protected] shell]# date -d @1566440271 --把时间戳换算成日期
Thu Aug 22 10:17:51 CST 2019

具体时间换算成时间戳
[[email?protected] shell]# date +%s -d "2018-09-09 06:30:22"
1536445822

[[email?protected] shell]# date "+%Y-%m-%d %H:%M:%S"
2019-08-22 14:21:16

变量
? 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
? 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi
? 引用某个命令的结果时,用变量替代 n=wc -l 1.txt
? 写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY
? 内置变量 $0,$1,$2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数
? 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

逻辑判断
?格式1:if 条件 ; then 语句; fi
? 格式2:if 条件; then 语句; else 语句; fi
? 格式3:if …; then … ;elif …; then …; else …; fi
? 逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
? 可以使用 && || 结合多个条件
? if [ $a -gt 5 ] && [ $a -lt 10 ]; then
? if [ $b -gt 5 ] || [ $b -lt 3 ]; then

[[email?protected] shell]# if [ $a -gt 3 ]; then echo ok; fi
ok

[[email?protected] shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi

[[email?protected] shell]# cat if2.sh
#!/bin/bash
a=2
if [ $a -gt 3 ]
then
echo ok
else
echo "not ok"
fi

[[email?protected] shell]# cat if3.sh
#!/bin/bash
a=6
if [ $a -gt 3 ]
then
echo ">3"
elif [ $a -lt 6 ]
then
echo "<6"
else
echo "not ok"
fi

支持>,但是一般使用 -gt这种a=2;if (($a>1)); then echo ok; fiok

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读