1. 程式人生 > >shell 指令碼介紹

shell 指令碼介紹

[[email protected] scripts]# file /etc/init.d/iptables (file 主要是用來檢視檔案型別) /etc/init.d/iptables: POSIX shell script text executable [[email protected] scripts]# ls ip.sh [[email protected] scripts]# /server/scripts/ip.sh (這是絕對路徑正常的執行指令碼) -bash: /server/scripts/ip.sh: Permission denied [
[email protected]
scripts]# ls -l ip.sh -rw-r--r-- 1 root root 21 Nov 21 09:33 ip.sh (預設建立的ip.sh指令碼檔案的許可權跟普通檔案的許可權是一樣的都是644) [[email protected] scripts]# sh /server/scripts/ip.sh (sh的這種 方式是最常用的方式) [email protected] scripts]# x=1 定義了變數 [[email protected] scripts]# echo [
[email protected]
scripts]# echo $x   (取變數裡面的內容就是用$ ) [[email protected] scripts]# echo ${LANG} en_US.UTF-8 [[email protected] scripts]# echo $LANG en_US.UTF-8 [[email protected] scripts]# echo ${LANG} (引用系統環境變數要用${}的方式引用才行) en_US.UTF-8 [[email protected]
scripts]# echo $LANG en_US.UTF-8 [[email protected] scripts]# echo $LANG $PATH $PS1 這些都是環境變數,也稱全域性變數 en_US.UTF-8 /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [\[email protected]\h \W]\$ [[email protected] scripts]#   [[email protected] scripts]# vim show.sh   echo $OLDBOY "show.sh" [New] 2L, 25C written [[email protected] scripts]# cat show.sh #!/bin/bash echo $OLDBOY [[email protected] scripts]# OLDBOY=10 [[email protected] scripts]# echo $OLDBOY 10 [[email protected] scripts]# sh /server/scripts/show.sh (執行顯示結果為空的原因是$OLDBOY是一個區域性變數只能在命令列執行,如果放在腳本里面是不會執行的,要想$OLDBOY可以使用就得 變成全域性變數才行,或者變成環境變數就必須在OLDBOY前面加上export 才行) [[email protected] scripts]# sh show.sh   [[email protected] scripts]# export OLDBOY [[email protected] scripts]# sh /server/scripts/show.sh 10   [[email protected] scripts]# sh show.sh   [[email protected] scripts]# vim show.sh #!/bin/bash echo $LANG [[email protected] scripts]# sh show.sh (這個系統環境變數或者是全域性變數就可以執行結果了) en_US.UTF-8 [[email protected] scripts]# env 顯示系統環境變數 [[email protected] scripts]# unset OLDBOY 取消環境變數  [[email protected] scripts]# vim /etc/profile.d/  是登入系統就要執行的指令碼 colorls.csh  cvs.csh      glib2.csh    lang.csh     less.csh     vim.csh      which2.sh     colorls.sh   cvs.sh       glib2.sh     lang.sh      less.sh      vim.sh        [[email protected] scripts]# vim /etc/profile.d/show.sh #!/bin/bash ip a s eth0 [[email protected] scripts]# chmod +x  /etc/profile.d/show.sh 要賦予執行許可權才能執行 [[email protected] scripts]# ll /etc/profile.d/show.sh -rwxr-xr-x 1 root root 26 Nov 21 11:01 /etc/profile.d/show.sh   Last login: Tue Nov 20 15:40:58 2018 from 10.0.0.1 斷開重新連線系統就會自動顯示ip地址   2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000     link/ether 00:0c:29:46:5d:0e brd ff:ff:ff:ff:ff:ff     inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0     inet6 fe80::20c:29ff:fe46:5d0e/64 scope link        valid_lft forever preferred_lft forever 我們要把我們寫好的指令碼放到/etc/profile.d/下面才行,這樣系統開機進入系統後就可以自動執行這個指令碼了,不需要我們在手動執行每一個指令碼了,切記這個要執行的指令碼必須要有x 執行許可權才可以   如 /etc/profile.d/show.sh   /etc/profile.d/test.sh 這樣開機的時候就會自動執行 show.sh和test.sh 這兩個指令碼了 [[email protected] scripts]# ls ip.sh  show-arg.sh  show.sh [[email protected] scripts]# cat show-arg.sh #!/bin/bash   # # # # echo '$0:' $0 '$1,$2,$3:'  $1 $2 $3 '$#:' $# $0:在腳本里面$0表示指令碼的名字 $1 在腳本里面表示第1個引數 a $2 在腳本里面表示第2個引數 b $3  在腳本里面表示第3個引數 c$# 表示指令碼一共有多少個引數 引數的個數 $#  表示指令碼一共有多少個引數 引數的個數 以上這些都表示的是位置的引數 [[email protected] scripts]# sh show-arg.sh $0: show-arg.sh $1,$2,$3: $#: 0 [[email protected] scripts]# [[email protected] scripts]# sh show-arg.sh a b c (show-arg.sh 表示指令碼名稱) $0: show-arg.sh $1,$2,$3: a b c $#: 3   [[email protected] scripts]# /etc/init.d/iptables Usage: iptables {start|stop|reload|restart|condrestart|status|panic|save} (Usage 與$0 一樣表示指令碼名稱) start|stop|reload|restart|condrestart|status|panic|save 這些都相當於是指令碼的引數,就是$加數字的方式實現的   檢視上一個命令的返回值是否正確與否的判斷顯示 [[email protected] scripts]# llll -bash: llll: command not found [[email protected] scripts]# echo $? 127  (返回值為數字表示上一個命令的執行時錯誤的) [[email protected] scripts]# touch test.txt [[email protected] scripts]# echo $? 0 返回值為0表示執行命令式正確的 一般在執行一些安裝服務或軟體比價多的情況下,你不知道是否執行成功了時,可以用echo $?來檢測 如果執行成功結果顯示為0,非0表示執行錯誤   向變數裡面傳引數方法一 [[email protected] scripts]# vim test.sh #!/bin/bash x=$1 y=$2 echo $x $y [[email protected] scripts]# sh test.sh 10 20 10 20 10  傳參給$1 20 傳給$2 [[email protected] scripts]# read -p "input x y:"  x y -p表示在螢幕上顯示輸出 input x y:10 20 [[email protected] scripts]# echo $x 10 [[email protected] scripts]# echo $y 20 向 x y 兩個變數裡面寫入了10和20 這種是互動式的向變數裡面寫東西 向變數裡面傳引數方法二 [[email protected] scripts]# cat read.sh #!/bin/bash   read -p "input x y:" x y echo $x $y [[email protected] scripts]# sh read.sh input x y:30 50 30 50 shell 裡面的迴圈的條件 [[email protected] scripts]# [ -f /root/oldboy.alex.txt ] 判斷這個檔案是否存在,如果存在返回值就是0,如果不存在返回值就是非0 [[email protected] scripts]# echo $? 1 (輸出結果顯示1表示這個檔案不存在) [[email protected] scripts]# [ -f /root/oldboy.txt  ] -f 表示檔案 [[email protected] scripts]# echo $? 0 (返回值為0表示檔案是存在的) [[email protected] scripts]# [ -d /root  ] [[email protected] scripts]# echo $? 0 [[email protected] scripts]# [ -d /rot  ] -d判斷目錄 [[email protected] scripts]# echo $? 1 [[email protected] scripts]# [ 1 -eq 1 ] [[email protected] scripts]# [[email protected] scripts]# echo $? 0 [[email protected] scripts]# [ 1 -eq 10 ] [[email protected] scripts]# echo $? 1   ####條件語句的指令碼##3 一個條件的單分支結構語句判斷 [[email protected] scripts]# cat if.sh #!/bin/bash num1=$1 num2=$2 if [ $num1 -gt $num2 ];then echo $num1 greater  than $num2 fi [[email protected] scripts]# sh /server/scripts/if.sh 20 10 20 greater than 10       兩個條件的雙分支條件語句 [[email protected] scripts]# vim if.sh #!/bin/bash num1=$1 num2=$2 if [ $num1 -gt $num2 ];then         echo $num1 greater than $num2 else         echo $num1 less than $num2 fi [[email protected] scripts]# sh if.sh 10 20 10 less than 20 [[email protected] scripts]# sh if.sh 30 20 30 greater than 20   [[email protected] scripts]# cat if.sh #!/bin/bash num1=$1 num2=$2 if [ $# -ne 2 ];then     echo "Usage: please input 2 number: num1 num2" (Usage 表示說明的意思  ) fi  (這個條件語句表示執行完後沒有退出)   if [ $num1 -gt $num2 ];then     echo $num1 greater than $num2 else     echo $num1 less than $num2 fi   [[email protected] scripts]# sh if.sh 10 輸入一個數字提示報錯資訊 Usage: please input 2 number: num1 num2  提示輸入兩個數字 if.sh: line 8: [: 10: unary operator expected 10 less than   [[email protected] scripts]# cat if.sh #!/bin/bash num1=$1 num2=$2 if [ $# -ne 2 ];then     echo "Usage: please input 2 number: num1 num2"     exit (執行完退出) fi   if [ $num1 -gt $num2 ];then     echo $num1 greater than $num2 else     echo $num1 less than $num2 fi     [[email protected] scripts]# sh if.sh 10 20 10 less than 20 [[email protected] scripts]# sh if.sh 20 10 20 greater than 10      [[email protected] scripts]# sh if.sh 10 10 10 eqal 10  

相關推薦

shell 指令碼介紹

[[email protected] scripts]# file /etc/init.d/iptables (file 主要是用來檢視檔案型別) /etc/init.d/iptables: POSIX shell script text executable [[em

十六週二次課 2018.02.05 shell指令碼介紹shell指令碼結構和執行、date命令用法、shell指令碼中的變數

20.1 shell指令碼介紹微信公眾號部落格,20.2 shell指令碼結構和執行建立目錄,然後我們進去在裡面寫指令碼第一行是他表示接下來的命令是通過這一個直譯器操作解析的的,通常都是/bin/bash(如果你是在本機上執行那麼不用寫也行,因為它知道接下來的命令能夠在這臺機

20.1 shell指令碼介紹 20.2 shell指令碼結構和執行 20.3 date命令用法 20.4 shell指令碼中的變數

20.1 shell指令碼介紹20.2 shell指令碼結構和執行20.3 date命令用法20.4 shell指令碼中的變數shell指令碼介紹shell是一種指令碼語言可以使用邏輯判斷、迴圈等語法可以自定義函式shell是系統命令的集合shell指令碼可以實現自動化運維,

Liunx----Shell指令碼簡單介紹 Shell執行環境和Shell變數

Shell 指令碼(shell script),是一種為 shell 編寫的指令碼程式。 Shell 程式設計跟 java、php 程式設計一樣,只要有一個能編寫程式碼的文字編輯器和一個能解釋執行的指令碼直譯器就可以了。 Linux 的 Shell 種類眾多,常見的有: Bourne Shell(

Shell指令碼介紹,變數,運算,條件判斷,迴圈,函式)

shell是人機互動的翻譯 注意的是,shell和Linux核心合在一起才是Linux。 Shell指令碼命令由兩種工作方式, 一種是互動式,寫一句命令,執行一句命令 一種是批處理,一次執行多個命令,先把命令寫好,然後在執行  舉個例子: [[emai

Linux shell指令碼開頭#!/bin/bash和#!/bin/sh介紹與區別

一直以為在Linux shell指令碼中#都是代表著註釋功能,同樣在指令碼開始的#!/bin/sh也只是告訴使用者這是一個shell指令碼,最近查了下,才發現不是這個意思,分享下。 一、意思 #!/bin/sh是指此指令碼使用/bin/sh來解釋執行,#!是特殊的表示符,其後面跟的是此解釋此指令碼的she

陣列-在Shell指令碼中的基本使用介紹

Shell指令碼在運維工作中是極其重要的,而陣列在shell腳本里的運用無論是在迴圈或運算方面都是非常實用的一個環節。 下面是對shell指令碼中陣列方面一些操作在此進行記錄,希望能幫助到有興趣的朋友~1.陣列定義 [[email protected] ~]# a=(1 2 3 4 5 6

shell指令碼中的grep命令引數使用方法介紹(轉載)

用‘grep’搜尋文字檔案如果您要在幾個文字檔案中查詢一字串,可以使用‘grep’命令。‘grep’在文字中搜索指定的字串。舉個例子:假設您正在‘/usr/src/linux/Documentation’目錄下搜尋帶字串‘magic’的檔案: $ grep magic /u

Linux shell指令碼基礎學習詳細介紹(完整版)2

詳細介紹Linux shell指令碼基礎學習(五) Linux shell指令碼基礎前面我們在介紹Linux shell指令碼的控制流程時,還有一部分內容沒講就是有關here document的內容這裡繼續。 Linux shell指令碼基礎已經被分成好幾個部分了,這裡對

CentOS 7 Shell指令碼程式設計第九講 read命令簡單介紹

開發十年,就只剩下這套架構體系了! >>>   

shell指令碼中的各種表示式介紹和使用

#前言:在shell指令碼中,有各種的表示式,包括有條件測試表達式,檔案表示式,字串表示式,整數表示式,接下來我們來了解一下他們的使用方法 1.條件測試表達式 #首先來看一下條件測試語法 #條件測試語法 #說明 1.test <測試表達式>

shell指令碼的函式介紹和使用案例

#前言:今天我們來聊聊shell指令碼中的函式知識,看一下函式的優勢,執行過程和相關的使用案例,我們也來看一下shell和python的函式書寫方式有什麼不同 #簡介 1、函式也具有別名類似的功能 2、函式是把程式裡多次呼叫相同的程式碼部分定義成一份,然後給這份程式碼定義個名字,如果出現重複的就呼叫就行了

shell指令碼中的if條件語句介紹和使用案例

#前言:在生產工作中if條件語句是最常使用的,如使用來判斷服務狀態,監控伺服器的CPU,記憶體,磁碟等操作,所以我們需要熟悉和掌握if條件語句。  簡介 if條件語句,簡單來說就是:如果,那麼。有if單分支結構,雙分支結構,多分支結構   1.單分支結構 #語法結構: if <

shell指令碼中的case條件語句介紹和使用案例

#前言:這篇我們接著寫shell的另外一個條件語句case,上篇講解了if條件語句。case條件語句我們常用於實現系統服務啟動指令碼等場景,case條件語句也相當於if條件語句多分支結構,多個選擇,case看起來更規範和易讀   #case條件語句的語法格式 case "變數" in 值1

linux的shell基礎介紹(1)

linux shell 8.1 shell介紹:器之間的交互1、shell是一個命令解釋器,提供用戶和機器之間的交互2、 支持特定語法,比如邏輯判斷、循環3、每個用戶都可以有自己特定的shell4、 CentOS7默認shell為bash(Bourne Agin Shell)5、 還有zsh、ksh等

linux的shell基礎介紹(2)

linux shell 8.6 管道符和作業控制:1、cat 1.txt |wc -l ; cat 1.txt |grep ‘aaa‘2、ctrl z 暫停一個任務3、jobs查看後臺的任務4、bg[id]把任務調到後臺5、fg[id]把任務調到前臺6、命令後面加&直接丟到後臺管道符的作用:把

shell簡單介紹

Linux shell簡單介紹 shell是一個命令解釋器,提供用戶和機器之間的交互支持特定語法,比如邏輯判斷,循環。每個用戶都可以有自己特定的shellCentos7默認shell為bash(Bourne Agin Shell)還有zsh,kshshell簡單介紹

shell(1)介紹、腳本結構和執行、date命令、腳本中的變量、腳本中邏輯判斷、文件目錄屬性判斷

shell語法 一、shell腳本介紹我自定的shell腳本基本是放在/usr/local/sbin/目錄下。 二、Shell腳本結構和執行腳本命令:bash +腳本文件路徑sh +腳本文件路徑查看腳本執行過程-x :bash -x 1.sh查看腳本是否語法

【轉】通過ionice和nice降低shell指令碼執行的優先順序

對於一些執行時會造成系統滿載的指令碼, 例如資料庫備份, 會影響當時其他服務的響應速度, 可以通過ionice和nice對其IO優先順序和CPU優先順序進行調整例如降低"/usr/local/bin/backup.sh"的IO優先順序, 讓其他程序順暢執行: /usr/bin/ionice -c2 -

shell指令碼 序列 併發問題

首先,如果只執行一個shell指令碼,那麼shell指令碼執行時是序列的,是從頭執行到尾的。 而且,如果在指令碼中,如果執行內部的shell函式,或者中途連線資料庫進行操作,這個過程也是序列的,不執行完這一條,不執行下一條語句。 但是,如果執行多個shell指令碼,可以併發執行,可以通過