1. 程式人生 > >Shell程式設計-自定義函式和shell指令碼除錯

Shell程式設計-自定義函式和shell指令碼除錯

1.自定義函式
函式代表著一個或一組命令的集合,表示一個功能模組,常用於模組化程式設計
一下是關於函式的重要說明
    在shell中,函式必須先定義,再呼叫
    使用 return value來獲取函式的返回值
    函式在當前shell中執行,可以使用指令碼中的變數


函式的格式如下:
函式名()
{
 命令1...
 命令2...
}


標準格式:其中function和()可省略,至少保留一個
[ function ] functionname [()]
{
    action
    [return int;]
}



例子:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop

執行:
./func.sh



例子:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop

執行結果:

[
[email protected]
_0_16_centos shell]# ./func2.sh hello
starting
Redirecting to /bin/systemctl start  httpd.service
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service
[[email protected]_0_16_centos shell]#


例子:修改,我們傳入了引數,需要在裡面呼叫的時候獲取到引數,否則,在自定義函式裡面相當於沒有呼叫到引數:
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
echo "restart"
service httpd restart
}

stop $1


執行結果:
[
[email protected]
_0_16_centos shell]# ./func1.sh hello
starting
Redirecting to /bin/systemctl start  httpd.service
$#=1
stopping
Redirecting to /bin/systemctl stop  httpd.service



2.函式返回值
函式返回值,只能通過$?系統變數獲得,可以顯示加:return 返回值,如果不加,將以最後一條命令執行結果作為返回值。return 後跟數值n(0-255)


例子:返回值必須是int型別0-255範圍
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1

restart

執行報錯:
[
[email protected]
_0_16_centos shell]# ./func3.sh
starting
Redirecting to /bin/systemctl start  httpd.service
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service
./func3.sh: line 17: return: abc: numeric argument required
[[email protected]_0_16_centos shell]#


我們可以使用最後一條命令的執行結果$?,也可以在function中顯示返回int值(0-255)




3.資料庫指令碼除錯
針對某些shell指令碼拷貝到某些環節可能會報錯,或者其中的業務邏輯不符合結果,那麼就需要進行指令碼除錯。

shell提供了指令碼除錯的方法:
1)語法問題:bash -n shellname
bash -n func3.sh


例子:
#!/bin/bash

test{
hello
}

function start(){
echo "starting"
service httpd start
}

start

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1

restart

其中方法test的定義有問題:這樣我們可以檢測語法的問題。
[[email protected]_0_16_centos shell]# bash -n func3.sh
func3.sh: line 5: syntax error near unexpected token `}'
func3.sh: line 5: `}'
[[email protected]_0_16_centos shell]#



2)除錯shell指令碼:bash -x shellname

[[email protected]_0_16_centos shell]# bash -x func3.sh
+ start
+ echo starting
starting
+ service httpd start
Redirecting to /bin/systemctl start  httpd.service
+ stop
+ echo '$#=0'
$#=0
+ echo stopping
stopping
+ service httpd stop
Redirecting to /bin/systemctl stop  httpd.service
+ restart
+ return abc
func3.sh: line 17: return: abc: numeric argument required

可以看到指令碼的呼叫順序,以及每一步的執行列印的結果

3)顯示包內容,並顯示執行的結果bash -v shellname

[[email protected]_0_16_centos shell]# bash -v func3.sh
#!/bin/bash

function start(){
echo "starting"
service httpd start
}

start
starting
Redirecting to /bin/systemctl start  httpd.service

stop(){
echo '$#='$#
echo "stopping"
service httpd stop
}

restart(){
return "abc"
}

stop $1
$#=0
stopping
Redirecting to /bin/systemctl stop  httpd.service

restart
func3.sh: line 17: return: abc: numeric argument required

[[email protected]_0_16_centos shell]#


4)指定除錯的程式碼:set -x放到程式碼中的位置
例子:
原來的if的判斷shell:
#!/bin/bash

read -t 10 -p "please input your name and age:" name age
echo $name $age

#read -t 30  -sp "input your age:" age
#echo $age


#read -t 30 -n 1  -p "input your gender[m|f]:" gender
#echo $gender


[[email protected]_0_16_centos es]# cat if.sh
#!/bin/bash

read -p "please input your name " name

echo $name

if [ "$name"==root ];then
        echo "welcome super man!"
elif [ "$name"==spark ];then
        echo "welcome spark "$name
else
        echo "welcome out bye"
fi

執行:
[[email protected]_0_16_centos es]# bash -x if.sh
+ read -p 'please input your name ' name
please input your name root
+ echo root
root
+ '[' root==root ']'
+ echo 'welcome super man!'
welcome super man!


如果加上set -x
如下:
[[email protected]_0_16_centos es]# bash if.sh
please input your name root
root
+ echo 'welcome super man!'
welcome super man!


可以看到除錯的範圍不同。

另外-n是檢測程式碼的問題
-v是顯示程式碼















相關推薦

Shell程式設計-定義函式shell指令碼除錯

1.自定義函式函式代表著一個或一組命令的集合,表示一個功能模組,常用於模組化程式設計一下是關於函式的重要說明    在shell中,函式必須先定義,再呼叫    使用 return value來獲取函式的返回值    函式在當前shell中執行,可以使用指令碼中的變數函式的格

shell呼叫定義函式及傳參

1 單個引數 #!/bin/bash function LoopPrint() { count=0; while [ $count -lt $1 ] ; do echo $count; let +

學會使用MySQL中定義函式儲存過程

一、快速瞭解什麼是儲存過程和函式?   儲存過程和函式是事先經過編譯並存儲在資料庫中的一段 SQL 語句的集合,呼叫儲存過程 和函式可以簡化應用開發人員的很多工作,減少資料在資料庫和應用伺服器之間的傳輸,對 於提高資料處理的效率是有好處的。   在對儲存過程或函式進行操作時,需要

MySQL定義函式儲存過程的區別:

  自定義函式和儲存過程的區別: 1)一般來說,儲存過程實現的功能要複雜一點,而函式的實現的功能針對性比較強。儲存過程,功能強大,可以執行包括修改表等一系列資料庫操作;使用者定義函式不能用於執行一組修改全域性資料庫狀態的操作。 2)對於儲存過程來說可以返回引數,如記錄集,而函式只能返回值或者表物件。函式只能

GO基礎程式設計-定義函式

想知道更多區塊鏈技術,請百度【鏈客區塊鏈技術問答 社群】 定義格式 函式構成程式碼執行的邏輯結構。在Go語言中,函式的基本組成為:關鍵字func、函式名、引數列表、返回值、函式體和返回語句。 Go 語言函式定義格式如下: func FuncName(/引數列表/) (o1 typ

初學mysql(十)-資料庫之儲存過程、函式與遊標-定義函式流程控制(下)

上一篇部落格講了儲存過程、函式、以及遊標,這一篇部落格接著上一篇部落格來說。首先說說mysql資料庫中的流程控制及自定義函式的使用。 自定義函式: 根據所需要的功能,使用流程控制來完成所需要的功能,完成功能的程式碼就稱為自定義函式。要想完成自定義函式就必須學會流程控制的使

laravel定義函式定義

1. 建立檔案 app/helpers.php<?php // 示例函式 function foo() { return "foo"; }2. 修改專案 composer.json在專案 composer.json 中 autoload 部分裡的 files 欄位加

Mysql中的定義函式定義過程

MYSQL中建立儲存過程和函式分別使用CREATE PROCEDURE和CREATE FUNCTION使用CALL語句來呼叫儲存過程,儲存過程也可以呼叫其他儲存過程函式可以從語句外呼叫,能返回標量值建立儲存過程語法CREATE PROCEDURE sp_name ([ pro

MySQL利用定義函式儲存過程建立海量表,並使用索引優化

昨天學習韓順平老師的視訊時明白了上一章explain的意義,為了自己的聯絡,我學著建立了一個海量表,供自己練習使用。 程式碼如下: #建立表DEPT CREATE TABLE dept( /*部門表*/ deptno MEDIUMINT UN

如何在 Linux Shell 程式設計定義使用函式

函式是一段可複用的程式碼。我們通常把重複的程式碼放進函式中並且在不同的地方去呼叫它。庫是函式的集合。我們可以在庫中定義經常使用的函式,這樣其它指令碼便可以不再重複程式碼而使用這些函式。 呼叫函式 在 Shell 中呼叫函式和呼叫其它命令是一模一樣的。例如,如果你

Shell指令碼中的定義函式

在寫程式時,經常會用到函式,一般開發工具擁有豐富的函式庫。但有時還需要根據自己的需要自定義函式滿足我們的需求。 在linux中,寫shell指令碼也一樣,有時會用到自定義函式。 函式,最簡單的定義為:將一組命令集或語句形成一個可用塊,這些塊稱為函式。 1、定義函式的格式:

shell中的定義函式,返回值

shell中的自定義函式 直接看截圖和程式碼吧,一目瞭然! #!/bin/bash #如下語句會報錯,因為在呼叫test1函式之前要先宣告test1函式 #也就是說,函式的呼叫語句要放在函式的宣告的後面(呼叫函式語句的位置要放在函式宣告語句的位置的後面) #test1

shell中的定義函式

shell中的自定義函式 自定義函式 基本語法 [ function ] funname[()] { Action; [return int;] } 呼叫直接寫函式名:funname [值] 注意: 1 函式體不能為空,函式體為空會報錯,裡面寫一條語句,比如 echo

linux shell 定義函式(定義、返回值、變數作用域)介紹

inux shell 可以使用者定義函式,然後在shell指令碼中可以隨便呼叫。下面說說它的定義方法,以及呼叫需要注意那些事項。 一、定義shell函式(define function) 語法: [ function ] funname [()] {     act

awk呼叫函式---內部定義函式外部shell定義函式

背景 在處理資料的時候對於一些文字需要做預處理;或者這些文字是通過一系列的處理演變得出;這些都寫在awk中一是程式顯得臃腫混亂,二是耦合性太強。 shell函式 先溫習一下普通shell的寫法 [[email protected] ~]

shell 定義函式

#定義一個數組 my_array[0]=-pc #遍歷陣列 for i in ${my_array[*]} do #如果第一個函式入參在陣列中並且第二個函式入參為2 if [ $1 = $i -a $2 -eq 2 ] then return 0

shell定義函式

轉載地址:https://www.cnblogs.com/oxspirt/p/7246075.html“sum=$(fsum 2 5)”這種方式,是將標準輸出(echo 出來的東子)傳遞給主程式的變數,而不是返回值!12345678910111213141516#!/bin/

shell定義函數

function shell 自定義一個打印函數參數的函數 [root@lynn-04 shell]# vim fun1.sh #!/bin/bash pri(){ echo "第一個參數為 $1" echo "第二個參數為 $2" echo "文件名

Shell 程式設計》09_while 迴圈 until 迴圈

《Shell 程式設計》09_while 迴圈和 until 迴圈 標籤(空格分隔): Shell 文章目錄 《Shell 程式設計》09_while 迴圈和 until 迴圈 9.1 當型和直到型迴圈語法 9.1.

Shell 程式設計》07_函式

《Shell 程式設計》07_函式 標籤(空格分隔): Shell 文章目錄 《Shell 程式設計》07_函式 7.1 Shell 函式的概念與作用介紹 7.2 Shell 函式的語法 7.3 Shell 函式的執行