[root@linux-01 ~]# mkdir shell
[root@linux-01 ~]# cd shell
[root@linux-01 shell]# vim 01.sh
[root@linux-01 shell]# sh 01.sh
123
19:20:52 up 15:41, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 187月18 2days 0.33s 0.33s -bash
root pts/0 192.168.96.1 18:50 4.00s 0.06s 0.03s w
01.sh
[root@linux-01 shell]# bash 01.sh
123
19:30:05 up 15:51, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 187月18 2days 0.33s 0.33s -bash
root pts/0 192.168.96.1 18:50 5.00s 0.03s 0.00s w
01.sh
[root@linux-01 shell]# bash -n 01.sh #检查脚本是否语法错误#
[root@linux-01 shell]# bash -n 01.sh
01.sh:行5: 寻找匹配的 `'' 是遇到了未预期的文件结束符
01.sh:行6: 语法错误: 未预期的文件结尾
[root@linux-01 shell]#
date命令用法
[root@linux-01 shell]# bash 01.sh
2018年 10月 22日 星期一 19:49:08 CST # date
2018----------------------------------date +%Y
18------------------------------------date +%y
10------------------------------------date +%m
49====================================date +%M
22------------------------------------date +%d
10/22/18------------------------------date +%D
20181022------------------------------date +%Y%m%d
2018-10-22----------------------------date +%F
19------------------------------------date +%H
1540208948----------------------------date +%s
19:49:08------------------------------date +%T
10月4908------------------------------date +%h%M%S
10月:49:08--------------------------- date +%h:%M:%S
19:49:08 - - - - - - - - - - date +%T
2018-10-22 - - - - - - - - - date +%F
1 - - - - - - - - - - - date +%w
43 - - - - - - - - - - - date +%W
[root@linux-01 shell]#cal
十月 2018
日 一 二 三 四 五 六
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
a大于3 输出 ok
#!bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
[root@linux-01 shell]# bash if1.sh
ok
a小于3 输出 nook
[root@linux-01 shell]# vi if2.sh
#!bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@linux-01 shell]# bash if2.sh
nook
[root@linux-01 shell]# vi if2.sh
#!bin/bash
a=3
if [ $a -gt 4 ]
then
echo ">1"
elif [ $a -lt 2 ]
then
echo nook
else
echo " <4 && >2 "
fi
[root@linux-01 shell]# bash if2.sh
<4 && >2
文件目录属性判断
#!/bin/bash
# 判断目录是否存在#
f="/root/shell/aminglinux"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# vi iff.sh
[root@linux-01 shell]# bash -x iff.sh
+ f=/root/shell/aminglinux
+ '[' -f /root/shell/aminglinux ']'
+ touch /root/shell/aminglinux
[root@linux-01 shell]# bash -x iff.sh
+ f=/root/shell/aminglinux
+ '[' -f /root/shell/aminglinux ']'
+ echo /root/shell/aminglinux exist
/root/shell/aminglinux exist
[root@linux-01 shell]#
[root@linux-01 shell]# vi iff2.sh
#!/bin/bash
f="/root/shell/aminglinux"
if [ -d $f ]
then
echo $d exist
else
touch $f
fi
[root@linux-01 shell]# bash -x iff2.sh
+ f=/root/shell/aminglinux
+ '[' -d /root/shell/aminglinux ']'
+ touch /root/shell/aminglinux
[root@linux-01 shell]# ls
01.sh aminglinux if1.sh if2.sh iff2.sh iff.sh
如果目录存在就删除
[root@linux-01 shell]# ls /tmp/
aminglinux systemd-private-1c39b1cbee544b9c99a62fe208b51a7c-chronyd.service-qKG4b0 yum.log
ks-script-6Xg0i9 vmware-root
[root@linux-01 shell]# vi iff3.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
rm -f $f
else
touch no
fi
[root@linux-01 shell]# bash -x iff3.sh
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ rm -f /tmp/aminglinux
[root@linux-01 shell]# ls /tmp/
ks-script-6Xg0i9 vmware-root
systemd-private-1c39b1cbee544b9c99a62fe208b51a7c-chronyd.service-qKG4b0 yum.log
if特殊用法
#!/bin/bash
if [ ! -f /tmp/lalal ]
then
echo "/tmp/lalal no"
exit
fi
n='wc -l /tmp/lalal'
if [ -z "$n" ]
then
echo asdas
exist
elif [ $n -gt 100 ]
then
echo sda
fi
[root@linux-01 shell]# bash -x if3.sh
+ '[' '!' -f /tmp/lalal ']'
+ echo '/tmp/lalal no'
/tmp/lalal no
+ exit