函数
格式:
function 方法名(){
方法体
return 数字;
}
注意:function和return可加可不加
$0...$n表示接收参数,$0永远是脚本名称(忽略)
方法的调用直接使用方法名不加()
return只能返回数字范围在0-255之间
$?接收返回值只能接收一次
#!/bin/sh
show(){
return 255
}
show
echo $?
echo $?
echo "-----------------"
show1(){
return 256
}
show1
echo $?
echo "---------------------"
show2(){
x=10
y=20
echo $[x+y]
[root@master ~]# vim ac.sh [root@master ~]# sh ac.sh 255 0 ----------------- 0 --------------------- 30
获取当前日期时间
date +"%Y-%m-%d %H:%M:%S"
格式化日期时间:
date -d today +"%Y-%m-%d %H:%M:%S"
[root@master ~]# date +"%Y-%m-%d %H:%M:%S" 2022-03-01 10:06:56 [root@master ~]# date -d today +"%Y-%m-%d %H:%M:%S" 2022-03-01 10:07:03
加减日期时间
date +%Y%m%d //显示当前年月日
date +%Y%m%d --date="+1 day" //显示后一天的日期
date +%Y%m%d --date="-1 day" //显示前一天的日期
date +%Y%m%d --date="-1 month" //显示上一月的日期
date +%Y%m%d --date="+1 month" //显示下一月的日期
date +%Y%m%d --date="-1 year" //显示前一年的日期
date +%Y%m%d --date="+1 year" //显示下一年的日期
[root@master ~]# date +%Y-%m-%d --date="-1 day" 2022-02-28 [root@master ~]# date +%Y-%m-%d --date="+1 day" 2022-03-02 [root@master ~]# date +%Y-%m-%d --date="-1 month " 2022-02-01 [root@master ~]# date +%Y-%m-%d --date="+1 month " 2022-04-01 [root@master ~]# date +%Y-%m-%d --date="-1 year " 2021-03-01 [root@master ~]# date +%Y-%m-%d --date="+1 year " 2023-03-01 [root@master ~]# date +%Y-%m-%d 2022-03-01
重定向:执行命令的结果写写入到文件
标准输入文件(stdin):stdin的文件描述符为0,默认从stdin读取数据。
标准输出文件(stdout):stdout 的文件描述符为1,默认向stdout输出数据。
标准错误文件(stderr):stderr的文件描述符为2,向stderr流中写入错误信息。
1>:正确信息重定向,错误信息留在终端
2>:错误信息重定向,正确信息留在终端
[root@master soft]# cd shell01/ [root@master shell01]# touch test.txt [root@master shell01]# ll 总用量 0 -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# touch show.txt [root@master shell01]# ll 总用量 0 -rw-r--r--. 1 root root 0 3月 2 09:53 show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt show.txt -rw-r--r--. 1 root root 0 3月 2 09:53 show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt end.txt ls: 无法访问end.txt: 没有那个文件或目录 -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt end.txt >> show.txt ls: 无法访问end.txt: 没有那个文件或目录 [root@master shell01]# cat show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt show.txt -rw-r--r--. 1 root root 50 3月 2 09:56 show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt show.txt >> show.txt [root@master shell01]# ll test.txt end.txt ls: 无法访问end.txt: 没有那个文件或目录 -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt
[root@master shell01]# ll test.txt end.txt >> show.txt ls: 无法访问end.txt: 没有那个文件或目录 [root@master shell01]# ll test.txt end.txt 1> show.txt ls: 无法访问end.txt: 没有那个文件或目录
[root@master shell01]# cat show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt end.txt > show.txt ls: 无法访问end.txt: 没有那个文件或目录 [root@master shell01]# ll test.txt end.txt 1> show.txt ls: 无法访问end.txt: 没有那个文件或目录
[root@master shell01]# ll test.txt end.txt 2> show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt
使用>,>>默认是把正确信息写入文件
想要把错误信息和正确信息写入文件需要后面追加2>&1(把错误信息当做正确信息去处理)
[root@master shell01]# ll test.txt end.txt ls: 无法访问end.txt: 没有那个文件或目录 -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt end.txt 2>&1 show.txt ls: 无法访问end.txt: 没有那个文件或目录 -rw-r--r--. 1 root root 53 3月 2 10:12 show.txt -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt [root@master shell01]# ll test.txt end.txt > show.txt 2>&1 [root@master shell01]# cat show.txt ls: 无法访问end.txt: 没有那个文件或目录 -rw-r--r--. 1 root root 0 3月 2 09:52 test.txt
定时器:
#!/bin/sh echo "helloworld">>test.txt
[root@master shell01]# vim cron.sh [root@master shell01]# sh cron.sh [root@master shell01]# cat test.txt helloworld [root@master shell01]# sh cron.sh [root@master shell01]# cat test.txt helloworld helloworld [root@master shell01]# ll 总用量 12 -rw-r--r--. 1 root root 38 3月 2 10:46 cron.sh -rw-r--r--. 1 root root 103 3月 2 10:22 show.txt -rw-r--r--. 1 root root 22 3月 2 10:47 test.txt
#!/bin/sh echo "helloworld">>/usr/local/soft/shell01/test.txt
[root@master shell01]# cat cron.sh #!/bin/sh echo "helloworld">>test.txt [root@master shell01]# cd .. [root@master soft]# ll 总用量 4 drwxr-xr-x. 8 10 143 255 3月 29 2018 jdk1.8.0_171 drwxr-xr-x. 2 root root 4096 2月 28 20:58 shell drwxr-xr-x. 2 root root 53 3月 2 10:49 shell01 drwxr-xr-x. 2 root root 72 2月 26 11:13 show [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# cat shell01/test.txt helloworld helloworld [root@master soft]# cd shell01 [root@master shell01]# pwd /usr/local/soft/shell01 [root@master shell01]# vim cron.sh [root@master shell01]# sh cron.sh [root@master shell01]# sh cron.sh [root@master shell01]# sh cron.sh [root@master shell01]# cd .. [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# sh shell01/cron.sh [root@master soft]# cat shell01/test.txt helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld
格式:
*(分钟) *(小时) *(星期) *(几号) *(月份) commend
通过crontab -e添加定时任务
查看定时任务是否执行了,看日志tail -f /var/log/cron
* * * * * sh /usr/local/soft/shell01/cron.sh
[root@master shell01]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@master shell01]# cat test.txt [root@master shell01]# cat test.txt helloworld helloworld
[root@master shell01]# cat test.txt helloworld helloworld helloworld helloworld helloworld helloworld [root@master shell01]# tail -f /var/log/cron Mar 2 15:38:02 master CROND[16963]: (root) CMD (sh /usr/local/soft/shell01/cron.sh) Mar 2 15:38:34 master crontab[16975]: (root) BEGIN EDIT (root) Mar 2 15:38:45 master crontab[16975]: (root) REPLACE (root) Mar 2 15:38:45 master crontab[16975]: (root) END EDIT (root) Mar 2 15:39:01 master crond[1033]: (root) RELOAD (/var/spool/cron/root) Mar 2 15:39:01 master CROND[16979]: (root) CMD (sh /usr/local/soft/shell01/cron.sh) Mar 2 15:40:01 master CROND[16995]: (root) CMD (/usr/lib64/sa/sa1 1 1) Mar 2 15:40:01 master CROND[16996]: (root) CMD (sh /usr/local/soft/shell01/cron.sh) Mar 2 15:41:01 master CROND[17011]: (root) CMD (sh /usr/local/soft/shell01/cron.sh) Mar 2 15:42:01 master CROND[17025]: (root) CMD (sh /usr/local/soft/shell01/cron.sh)
注意:
定时器或脚本中涉及到路径的全部给绝对路径
* * * * * 每分钟
*/1 * * * * 每分钟
0 */1 * * * 每小时



