linux下shell指令碼編程1

來源:互聯網
上載者:User

標籤:linux   centos   shell   shell編程   

1、 shell指令碼是什麼

它是一種指令碼語言,並非程式設計語言。

可以使用一些邏輯判斷、迴圈等文法。

可以自訂子函數,是系統命令的集合。

shell指令碼可以實現自動化營運,大大增加我們的工作效率。


2、shell指令碼結構以及執行方法

開頭行指定bash路徑: #! /bin/bash

以#開頭的行作為解釋說明

#注釋自己的指令碼內容,方便自己查閱;utf8字元集,支援中文;

指令碼的名字以.sh結尾,用於區分這是一個shell指令碼

執行指令碼方式有兩種:

chmod a+x 1.sh    添加x執行許可權;

./1.sh 可以直接執行,或者寫絕對路徑/root/shell/1.sh

如果沒有執行許可權可以 bash 1.sh    或 sh 1.sh

bash -x 1.sh 可以查看指令碼執行過程

實驗練習:

[[email protected] shell]# cat 1.sh #!/bin/bash#這是我的第一個指令碼echo "hello world"
[[email protected] shell]# ls -l-rw-r--r-- 1 root root 60 6月  16 19:28 1.sh[[email protected] shell]# chmod a+x 1.sh [[email protected] shell]# ls -l-rwxr-xr-x 1 root root 60 6月  16 19:28 1.sh[[email protected] shell]# ./1.shhello world[[email protected] shell]# /root/shell/1.sh hello world[[email protected] shell]# /bin/sh 1.shhello world[[email protected] shell]# bash -x 1.sh + echo ‘hello world‘hello world


執行bash和sh是一樣的,sh是bash的軟串連檔案;

[[email protected] ~]# ls -l /bin/bash -rwxr-xr-x. 1 root root 871700 10月 16 2014 /bin/bash[[email protected] ~]# ls -l /bin/shlrwxrwxrwx. 1 root root 4 3月   4 00:59 /bin/sh -> bash


3、學會date命令的用法

date  +%Y-%m-%d     date +%y-%m-%d 年月日

date  +%Y-%m-%d = date +%F 年月日

date  +%H:%M:%S = date +%T 時間

date +%s  時間戳記

date -d @1434248742    根據時間戳記算出目前時間

date -d "+1day" 一天后    date -d "-1day" 一天前

date -d  "-1month" 一月前

date -d  “-1min” 一分鐘前

date +%w     date +%W 星期


實驗練習:

[[email protected] shell]# date +%y15[[email protected] shell]# date +%Y2015[[email protected] shell]# date +%m06[[email protected] shell]# date +%d16[[email protected] shell]# date +%H14[[email protected] shell]# date +%M01[[email protected] shell]# date +%S54

從1970年 01月01日0點0分開始算起到現在多少秒;

[[email protected] shell]# date +%s1434434874[[email protected] shell]# date -d @14344348742015年 06月 16日 星期二 14:07:54 CST

CST是中國時間 +8小時

[[email protected] shell]# date -d @01970年 01月 01日 星期四 08:00:00 CST


[[email protected] shell]# date +%F2015-06-16[[email protected] shell]# date +%T14:04:17[[email protected] shell]# date +%Y-%m-%d2015-06-16[[email protected] shell]# date +"%Y-%m-%d %H:%M:%S"2015-06-16 14:05:13[[email protected] shell]# date +"%F %T"2015-06-16 14:05:38

周二

[[email protected] shell]# date +%w2

今年的第多少周,24周

[[email protected] shell]# date +%W24

全年有多少周,52周;

[[email protected] shell]# echo "365/7" | bc52
[[email protected] shell]# date -d "-1 day"2015年 06月 15日 星期一 14:16:31 CST[[email protected] shell]# date -d "-1 day" +"%F %T"2015-06-15 14:19:13[[email protected] shell]# date -d "+1 day" +"%F %T"2015-06-17 14:19:22[[email protected] shell]# date -d "+1 month" +"%F %T"2015-07-16 14:19:31[[email protected] shell]# date -d "+1 year" +"%F %T"2016-06-16 14:19:39[[email protected] shell]# date -d "+1 week" +"%F %T"2015-06-23 14:19:45[[email protected] shell]# date -d "-10 hour" +"%F %T"2015-06-16 04:19:59[[email protected] shell]# date -d "-10 min" +"%F %T"2015-06-16 14:10:15[[email protected] shell]# date -d "-10 sec" +"%F %T"2015-06-16 14:20:14


4、shell指令碼中的變數

當指令碼中使用某個字串較頻繁並且字串長度很長時就應該使用變數代替。

使用條件陳述式時,常常使用變數    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))  或者 c=$[$a+$b]


實驗練習:

引用某個命令的結果,使用變數代替

[[email protected] shell]# file=`which yum`[[email protected] shell]# echo $file/usr/bin/yum[[email protected] shell]# rpm -qf $fileyum-3.2.29-60.el6.centos.noarch

變數只在當前shell下生效,子shell不會生效;

要想子shell也生效,使用export file 聲明變數;

使用者互動的變數:

[[email protected] shell]# cat 2.sh #!/bin/bash#與使用者互動的變數read -p "請輸入一個數字:" numecho $num[[email protected] shell]# sh 2.sh 請輸入一個數字:333333

參數的變數:

[[email protected] shell]# cat 3.sh #!/bin/bash#關於參數的變數echo "\$1=$1"echo "\$2=$2"echo "\$3=$3"echo "\$#=$#"echo "\$0=$0"[[email protected] shell]# sh 3.sh ABC linux world$1=ABC$2=linux$3=world$#=3$0=3.sh

數值變數:

[[email protected] shell]# a=1;b=2[[email protected] shell]# c=$a+$b[[email protected] shell]# echo $c1+2[[email protected] shell]# c=$[$a+$b][[email protected] shell]# echo $c3[[email protected] shell]# c=$(($a+$b))[[email protected] shell]# echo $c3


5、shell中的邏輯判斷

格式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    (greater than)

小於< lt     (less than)

大於等於 >= ge

小於等於 <=  le

等於    ==eq    (equal)

不等於   != ne

實驗練習:

[[email protected] shell]# cat if.sh #!/bin/bash#if判斷語句,條件為真,列印true;if :then    echo truefi[[email protected] shell]# sh if.shtrue

if判斷語句2;

[[email protected] shell]# cat if2.sh #!/bin/bash#if判斷語句,條件為真,返回true;if [ 1 == 1 ]thenecho "true"fi[[email protected] shell]# sh -x if2.sh + ‘[‘ 1 == 1 ‘]‘+ echo truetrue[[email protected] shell]# sh if2.sh true

if判斷語句3;

[[email protected] shell]# cat if3.sh #!/bin/bash#if判斷語句,條件為真返回true,條件為假,返回false;if [ "1" == "2" ]thenecho "true"elseecho "false"fi[[email protected] shell]# sh if3.sh false

變數,進行比較

[[email protected] shell]# cat if4.sh #!/bin/bash#if判斷語句,變數進行比較;a=1if [ "$a" == "2" ]thenecho "true"elseecho "false"fi[[email protected] shell]# sh -x if4.sh + a=1+ ‘[‘ 1 == 2 ‘]‘+ echo falsefalse

多個判斷要加elif

[[email protected] shell]# cat if5.sh #!/bin/bash#if判斷語句,多個判斷使用elif;a=1if [ "$a" == "2" ]thenecho "true"elif [ "$a" -lt 10 ]thenecho "no false"elseecho "false"fi[[email protected] shell]# sh if5.sh no false

[ $a -lt 3 ] 也可以這樣代替 (($a<3));使用方括弧請一定注意空格;

[[email protected] shell]# a=1;if(($a<3)); then echo OK;fiOK[[email protected] shell]# a=1;if [ $a -lt 3 ]; then echo OK;fiOK

&& 並且   前面的執行成功後才執行後面的;

|| 或者   前面的執行不成功執行後面的;

[[email protected] shell]# a=5[[email protected] shell]# if [ $a -lt 10 ]&&[ $a -gt 2 ];then echo OK;fiOK[[email protected] shell]# if [ $a -lt 10 ]||[ $a -gt 2 ];then echo OK;fiOK


奇數偶數判斷,輸入的數字除以2,餘數為0為偶數,非0為奇數;

[[email protected] shell]# cat 4.sh#!/bin/bashread -p "enter a number:" nn1=$[$n%2]if [ $n1 -eq 0 ]thenecho "你輸入的數字是偶數"elseecho "你輸入的數字是奇數"fi[[email protected] shell]# sh 4.shenter a number:23你輸入的數字是奇數[[email protected] shell]# sh 4.shenter a number:90你輸入的數字是偶數

判斷輸入的是否是數字,不是數位直接退出;數位話判斷奇數或偶數;

`echo $n|grep -c ‘[^0-9]‘`    匹配輸入的字元為非數位行數,如果為1說明不是數字。

[[email protected] shell]# cat 5.sh #!/bin/bash#判斷輸入的是否是數字,不是數位直接退出;數位話判斷是奇數或偶數;read -p "請輸入一個數字:" nn2=`echo $n|grep -c ‘[^0-9]‘`if [ $n2 -eq 1 ]thenecho "你輸入的不是純數字,請重新輸入"exit 1fin1=$[$n%2]if [ $n1 -eq 0 ]thenecho "你輸入的數字是偶數"elseecho "你輸入的數字是奇數"fi[[email protected] shell]# sh 5.sh 請輸入一個數字:abc你輸入的不是純數字,請重新輸入[[email protected] shell]# sh 5.sh 請輸入一個數字:323你輸入的數字是奇數

6、if 判斷檔案、目錄屬性

[ -f file ]判斷是否是普通檔案,且存在

[ -d file ] 判斷是否是目錄,且存在

[ -e file ] 判斷檔案或目錄是否存在

[ -r file ] 判斷檔案是否可讀

[ -w file ] 判斷檔案是否可寫

[ -x file ] 判斷檔案是否可執行

實驗練習:

1.sh存在的話執行後面的

[[email protected] shell]# [ -f 1.sh ] && echo "1.sh exist"1.sh exist

21.sh不存在執行後面的。

[[email protected] shell]# [ -f 21.sh ] || echo "1.sh not exist"1.sh not exist

判斷檔案是否存在

[[email protected] shell]# cat test.sh #!/bin/bash#判斷1.sh是否存在;if [ -e 1.sh ]thenecho "1.sh exist"elseecho "1.sh not exist"fi[[email protected] shell]# sh test.sh 1.sh exist


exec的用法,結合date變數實驗

exec命令用於調用並執行指令的命令。exec命令通常用在shell指令碼程式中,可以調用其他的命令。如果在當前終端中使用命令,則當指定的命令執行完畢後會立即退出終端。

[[email protected] shell]# cat date.sh #!/bin/bash#exec的用法,結合date變數實驗;d=`date +%F`exec >/tmp/$d.log 2>&1echo "Begin at `date`"ls /tmp/abccd /dddecho "End at `date`"
[[email protected] shell]# sh date.sh [[email protected] shell]# cat /tmp/2015-06-16.log Begin at 2015年 06月 16日 星期二 16:49:54 CSTls: 無法訪問/tmp/abc: 沒有那個檔案或目錄date.sh: line 7: cd: /ddd: 沒有那個檔案或目錄End at 2015年 06月 16日 星期二 16:49:54 CST

exec >/tmp/$d.log 2>&1  表示執行下面的行,輸出正確或錯誤的資訊都寫到/tmp/目錄下 日期.log裡面;


本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1662833

linux下shell指令碼編程1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.