亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

linux中Shell Script有什么用

發布時間:2021-10-08 15:14:48 來源:億速云 閱讀:113 作者:小新 欄目:開發技術

這篇文章給大家分享的是有關linux中Shell Script有什么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1 Shell Scipt
使用指令和基本程序設計結構寫成的程序,可以完成復雜的處理流程

1.1 程序書寫

代碼如下:


#!/bin/bash
# Program:
#       This program shows "Hello Wrold" in your screen.
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo -e "Hello World!\a\n"
exit 0

第一行 #!/bin/bash 說明使用的shell類型,不同shell語法可能不同,所以要說明使用的是哪種shell
其它#開始的表示注釋,注釋一般需要說明
程序功能
版本歷史
作者及聯系方式
設置好PATH變量,以便直接可以調用相應路徑下的命令
程序主體部分
exit 0 表示程序執行成功,向環境返回0
1.2 程序執行
bash $bash sh01.sh #如果用sh sh01.sh而sh又不是指向bash,那么sh01.sh內的語法就會不一致,因為用 #sh去解釋了bash語法寫的shell script,針對這個程序,如果 #$type sh #得到sh is hashed (/bin/sh) #那么會輸出-e Hello world!,而非Hello world!

代碼如下:


$./xxx.sh $chmod +x sh01.sh $./sh01.sh
source $ source sh01.sh

注:用bash和用source的不同在于,用bash執行時,shell script其實是在在父程序bash下新建了一個 bash子程序,這個子程序中執行,當程序執行完后,shell script里定義的變量都會隨子程序的結束而消失, 而用source執行時,是在父程序bash中執行,shell script里定義的變量都還在。

2 簡單Shell練習

2.1 例1 接收用戶輸入

代碼如下:


# !/bin/bash
# Program:
#       This program is used to read user's input
# Site: www.mlszssj.com
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Your first name:" firstname # tell user to input
read -p "Your last name:" lastname # tell user to input
echo -e "\nYour full name: $firstname $lastname"
exit 0

調用:

代碼如下:


$ bash sh02.sh
Your first name:Minix
Your last name:007
Your full name: Minix 007

2.2 例2 按日期建立相似名字的文件

代碼如下:


# !/bin/bash
# Program:
#       This program is used to create files according to date
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
# Get filename from user
echo -e "I will use 'touch' to create three files."
read -p "Please input your filename:" tmpfilename
# Prevent the user input [Enter]
# Check whether filename exists or not
filename=${tmpfilename:-"filename"}
# Get the final filename according to date
date1=$(date --date='2 days ago' +%Y%m%d) # date of 2 days ago
date2=$(date --date='1 days ago' +%Y%m%d) # date of yesterday
date3=$(date +%Y%m%d) # date of today
filename1=${filename}${date1}
filename2=${filename}${date2}
filename3=${filename}${date3}
# Create file
touch "$filename1"
touch "$filename2"
touch "$filename3"
exit 0

調用:

代碼如下:


$ bash sh03.sh
I will use 'touch' to create three files.
Please input your filename:WhoKnows
$ ls W*
WhoKnows20130201  WhoKnows20130202  WhoKnows20130203

3 判斷式
3.1 測試文件是否存在
test -e filename會根據filename是否存在返回0或1,再交由echo顯示結果:

代碼如下:


$ test -e sh01.sh  && echo "Exists" || echo "Not exists"
Exists
$ test -e sh0x.sh  && echo "Exists" || echo "Not exists"
Not exists

3.2 test常用選項
3.2.1 文件類型

代碼如下:


-e file :file是否存在
-f file :file是否存在且為文件
-d file :file是否存在且為目錄

3.2.2 權限
-r file :file是否有讀的權限

3.2.3 文件新舊比較
-nt file1 file2 : file1 是否比 file2新

3.2.4 整數,字符串,多重條件判斷
-z string: string是否為空
例:輸出指定文件類型及屬性

代碼如下:


# !/bin/bash
# Program:
#       This program is used to output type and permission of the target file
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
# Get filename from user
echo -e "Input name of the file that you want to check.\n"
read -p "Filename:" filename
test -z $filename && echo "You must input a filename." && exit 0
# Check whether the file exists or not
test ! -e $filename && echo "The file '$filename' DO NOT exists" && exit 0
# Check type and permission of the file
test -f $filename && filetype="regular file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
# Output result
echo "The filename:$filename is a $filetype"
echo "And Permissions are :$perm"
exit 0

調用:

代碼如下:


$ bash sh04.sh
Input name of the file that you want to check.

Filename:sh01.sh
The filename:sh01.sh is a regular file
And Permissions are :readable writable executable

3.3 使用[]判斷

測試文件是否存在

代碼如下:


$ [ -e "sh01.sh" ] ; echo $?
0
$ [ -e "sh0x.sh" ] ; echo $?
1

注意[]內空格必須有
這種方法和test的test -e "sho1.sh" ; echo $? 是一致的

4 Shell Script 參數

代碼如下:


# !/bin/bash
# Program:
#       This program is used to ouput parameter of the shell script
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
echo "The script's name is ==> $0"
echo "Total parameter number is ==> $#"
# Check whether number of the parameter is less than 2
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2.Stop here." && exit 0
echo "The whole parameter is ==> '$@'"
echo "The first parameter is ==> $1"
echo "The first parameter is ==> $2"
exit 0

調用:

代碼如下:


$ bash sh05.sh 1a 2b 3c 4d
The script's name is ==> sh05.sh
Total parameter number is ==> 4
The whole parameter is ==> '1a 2b 3c 4d'
The first parameter is ==> 1a
The first parameter is ==> 2b

注:從以上程序可以看出與參數有關的預設變量如何表示

5 條件表達式

5.1 if 結構

代碼如下:


# !/bin/bash
# Program:
#       This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
    echo "OK, continue"
    exit 0
fi
if [ "$choice" == "N" ] || [ "$choice" == "n" ];then
    echo "Oh, interupt"
    exit 0
fi
exit 0

調用:

代碼如下:


$ bash sh06.sh
Please input [Y/N]y
OK, continue
$ bash sh06.sh
Please input [Y/N]n
Oh, interupt

5.2 if else 結構

代碼如下:


# !/bin/bash
# Program:
#       This program is used to show if else expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Please input [Y/N]" choice
if [ "$choice" == "Y" ] || [ "$choice" == "y" ];then
    echo "OK, continue"
    exit 0
elif [ "$choice" == "N" ] || [ "$choice" == "n" ];then
    echo "Oh, interupt"
    exit 0
else
    echo "Input [Y/N]"
fi
exit 0

5.3 case

代碼如下:


# !/bin/bash
# Program:
#       This program is used to show case expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
    "1")
        echo "Your choice is ONE"

    "2")
        echo "Your choice is TWO"

    "3")
        echo "Your choice is THREE"

esac
exit 0

調用:

代碼如下:


$ bash sh08.sh
Tell me your choice:[1-3]=>2
Your choice is TWO
$ bash sh08.sh
Tell me your choice:[1-3]=>1
Your choice is ONE
$ bash sh08.sh
Tell me your choice:[1-3]=>3
Your choice is THREE

6 函數

代碼如下:


# !/bin/bash
# Program:
#       This program is used to test function
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
function myprint(){
    echo -n "Your choice is "
}
read -p "Tell me your choice:[1-3]=>" choice
case $choice in
    "1")
        myprint;echo "ONE"

    "2")
        myprint;echo "TWO"

    "3")
        myprint;echo "THREE"

esac
exit 0

調用:

代碼如下:


$ bash sh09.sh
Tell me your choice:[1-3]=>1
Your choice is ONE
$ bash sh09.sh
Tell me your choice:[1-3]=>2
Your choice is TWO
$ bash sh09.sh
Tell me your choice:[1-3]=>3
Your choice is THREE

7 循環
7.1 while

代碼如下:


# !/bin/bash
# Program:
#       This program shows while expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
while [ "$choice" != "yes" ]
do
    read -p "Give your choice [yes/no]:" choice
done
exit 0

調用:

代碼如下:


$ bash sh20.sh
Give your choice [yes/no]:no
Give your choice [yes/no]:no
Give your choice [yes/no]:nx
Give your choice [yes/no]:yes

7.2 for

代碼如下:


# !/bin/bash
# Program:
#       This program is used to demo for expression
# History:
# 2013/2/3 on_1y First release
PATH=$PATH
export PATH
for choice in 1 2 3
do
    echo "your choice is $choice"
done
exit 0

調用示例:

代碼如下:


$ bash sh21.sh
your choice is 1
your choice is 2
your choice is 3

8 shell script的追蹤與Debug
sh -n xx.sh # 語法檢查
sh -x xx.sh # 列出xx.sh的執行過程

感謝各位的閱讀!關于“linux中Shell Script有什么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

邹平县| 岗巴县| 崇明县| 辽宁省| 崇州市| 永清县| 庆阳市| 台东市| 镇巴县| 巴马| 连山| 沂南县| 阿尔山市| 西昌市| 林甸县| 天等县| 卢龙县| 石城县| 威远县| 尖扎县| 齐齐哈尔市| 木兰县| 宜春市| 米脂县| 乐山市| 锦屏县| 龙川县| 确山县| 灵丘县| 交城县| 嘉峪关市| 永福县| 建德市| 盐山县| 石泉县| 五华县| 禹州市| 司法| 惠州市| 唐山市| 体育|