[TOC]
創(chuàng)新互聯(lián)堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10年網(wǎng)站建設(shè)經(jīng)驗創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營銷服務(wù)商,為您提供做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)站設(shè)計、成都h5網(wǎng)站建設(shè)、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、成都微信小程序服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。#!/bin/bash
,這是shell的固有格式,指定接下來要運行的命令,是通過那一個解釋器來操作的#! /bin/bash
# chkconfig: 2345 10 90 定義啟動級別,
# description: Activates/Deactivates all network interfaces configured to \ 描述信息 這兩行一定要,沒有這兩行就不能添加到chkconfig列表里去
# start at boot time.
#
.sh
結(jié)尾,用于區(qū)分這是一個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
只能檢查語法上的錯誤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
星期
演示:
[root@mydb1 test]# date +%y 簡寫年
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 時
11
[root@mydb1 test]# date +%s 時間戳,距離197001010000到現(xiàn)在過去多少秒
1574221403
[root@mydb1 test]# date +%S 秒
30
[root@mydb1 test]# date +%T 時:分:秒
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小時以前
12:55:18
[root@mydb1 ~]# date +%s
1574229561
[root@mydb1 ~]# date -d @1574229561 將時間戳轉(zhuǎn)換為具體日期
Wed Nov 20 13:59:21 CST 2019
[root@mydb1 ~]# date +%s -d "2019-11-20 14:01:01" 將具體時間轉(zhuǎn)換為時間戳
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
如果判斷不存在,加上感嘆號!
#!/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" ]
這個表示當變量a的值為空時會怎么樣#!/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" ]
表示當變量a的值不為空,可以判斷一個文件的內(nèi)容不為空[root@localhost tmp]# if [ -n if.sh ];then echo ok;fi 判斷文件不用雙引號
ok
[root@localhost tmp]# if [ -n "$a" ];then echo $a;else echo "a is null";fi 如果是變量一定要加雙引號
a is null
if grep -q '123'
1.txt; then表示如果1.txt中含有'123'的行時會怎么樣[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表示僅僅做過濾,不把過濾的內(nèi)容顯示出來
zabbix exist
if [ ! -e file ]; then
表示文件不存在時會怎么樣if (($a<1)); then …
等同于 if [ $a -lt 1 ]; then…
[ ]
中不能使用<,>,==,!=,>=,<=這樣的符號格式:
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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標題:3shell編程知識-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://bm7419.com/article22/iggjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站營銷、虛擬主機、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容