[TOC]
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、南岔ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的南岔網(wǎng)站制作公司
#!/bin/bash
,這是shell的固有格式,指定接下來(lái)要運(yùn)行的命令,是通過(guò)那一個(gè)解釋器來(lái)操作的#! /bin/bash
# chkconfig: 2345 10 90 定義啟動(dòng)級(jí)別,
# description: Activates/Deactivates all network interfaces configured to \ 描述信息 這兩行一定要,沒(méi)有這兩行就不能添加到chkconfig列表里去
# start at boot time.
#
.sh
結(jié)尾,用于區(qū)分這是一個(gè)shell腳本[root@mydb1 test]# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Apr 24 2019 /bin/sh -> bash
[root@mydb1 test]# ls -l /bin/bash
-rwxr-xr-x 1 root root 906568 Mar 23 2017 /bin/bash
bash -x 1.sh
bash -n 1.sh
只能檢查語(yǔ)法上的錯(cuò)誤date +%Y-%m-%d, date +%y-%m-%d
年月日date +%H:%M:%S = date +%T
時(shí)間date +%s
時(shí)間戳date -d @1504620492
date -d "+1day"
一天后date -d "-1 day"
一天前date -d "-1 month"
一月前date -d "-1 min"
一分鐘前date +%w, date +%W
星期
演示:
[root@mydb1 test]# date +%y 簡(jiǎn)寫年
19
[root@mydb1 test]# date +%Y
2019
[root@mydb1 test]# date +%m 月份
11
[root@mydb1 test]# date +%M 分
40
[root@mydb1 test]# date +%d 天
20
[root@mydb1 test]# date +%D 月/日/年
11/20/19
[root@mydb1 test]# date +%Y%m%d 年月日
20191120
[root@mydb1 test]# date +%F 年-月-日
2019-11-20
[root@mydb1 test]# date +%H 時(shí)
11
[root@mydb1 test]# date +%s 時(shí)間戳,距離197001010000到現(xiàn)在過(guò)去多少秒
1574221403
[root@mydb1 test]# date +%S 秒
30
[root@mydb1 test]# date +%T 時(shí):分:秒
11:49:36
[root@mydb1 test]# date +%h 英文月份
Nov
[root@mydb1 test]# date +%H:%M:%S 等同于T
11:52:40
[root@mydb1 test]# date +%w 星期幾
3
[root@mydb1 test]# date +%W 一年中第幾周
46
[root@mydb1 test]# cal 日歷形式
November 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
[root@mydb1 ~]# date +'%Y%m%d' -d '1 days ago'
20191119
[root@mydb1 ~]# date +'%Y%m%d' -d '-1 days'
20191119
[root@mydb1 ~]# date +%F -d '1 year' 1年以后
2020-11-20
[root@mydb1 ~]# date +%F -d '-1 year' 1年以前
2018-11-20
[root@mydb1 ~]# date +%T
13:55:03
[root@mydb1 ~]# date +%T -d '-1 hour' 1小時(shí)以前
12:55:18
[root@mydb1 ~]# date +%s
1574229561
[root@mydb1 ~]# date -d @1574229561 將時(shí)間戳轉(zhuǎn)換為具體日期
Wed Nov 20 13:59:21 CST 2019
[root@mydb1 ~]# date +%s -d "2019-11-20 14:01:01" 將具體時(shí)間轉(zhuǎn)換為時(shí)間戳
1574229661
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
#!/bin/bash
a=2
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
#!/bin/bash
a=3
if [ $a -gt 4 ]
then
echo ">1"
elif [ $a -lt 6 ]
then
echo "<6 && >1"
else
echo nook
fi
[ -f file ]
判斷是否是普通文件,且存在
#!/bin/bash
f="/tmp/test.txt"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
如果判斷不存在,加上感嘆號(hào)!
#!/bin/bash
f="/tmp/abc.txt"
if [ ! -f $f ]
then
touch $f
fi
[ -d file ]
判斷是否是目錄,且存在[ -e file ]
判斷文件或目錄是否存在[ -r file ]
判斷文件是否可讀[ -w file ]
判斷文件是否可寫[ -x file ]
判斷文件是否可執(zhí)行if [ -z "$a" ]
這個(gè)表示當(dāng)變量a的值為空時(shí)會(huì)怎么樣
#!/bin/bash
if [ ! -f /tmp/abc.log ]
then
echo "/tmp/abc.log not exist"
exit
fi
n=`wc -l /tmp/abc.log`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo $n
fi
if [ -n "$a" ]
表示當(dāng)變量a的值不為空,可以判斷一個(gè)文件的內(nèi)容不為空
[root@localhost tmp]# if [ -n if.sh ];then echo ok;fi 判斷文件不用雙引號(hào)
ok
[root@localhost tmp]# if [ -n "$a" ];then echo $a;else echo "a is null";fi 如果是變量一定要加雙引號(hào)
a is null
if grep -q '123'
1.txt; then表示如果1.txt中含有'123'的行時(shí)會(huì)怎么樣
[root@localhost tmp]# grep -w 'zabbix' /etc/passwd
zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
[root@localhost tmp]#
[root@localhost tmp]# if grep -w 'zabbix' /etc/passwd;then echo "zabbix exist";fi
zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
zabbix exist
[root@localhost tmp]# if grep -wq 'zabbix' /etc/passwd;then echo "zabbix exist";fi -q表示僅僅做過(guò)濾,不把過(guò)濾的內(nèi)容顯示出來(lái)
zabbix exist
if [ ! -e file ]; then
表示文件不存在時(shí)會(huì)怎么樣if (($a<1)); then …
等同于 if [ $a -lt 1 ]; then…
[ ]
中不能使用<,>,==,!=,>=,<=這樣的符號(hào)格式:
case 變量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
- 2|3)
command
;;
案例:
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done
echo $sum
文件列表循環(huán)
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
? ?? ls -d $a
fi
done
本文名稱:3shell編程知識(shí)
本文URL:http://m.2m8n56k.cn/article24/jcgsce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)公司、做網(wǎng)站、全網(wǎng)營(yíng)銷推廣、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)