校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > linux基礎之Shell Script入門介紹

linux基礎之Shell Script入門介紹

熱門標簽:華鋒e路航港口地圖標注 打電話機器人接我是他的秘書 如果做線上地圖標注 河南信譽好的不封卡電話外呼系統 江蘇云電銷機器人公司 揭陽智能電話機器人推薦 地圖標注員都是年輕人 百度地圖標注錯了有責任嗎 客服外呼系統怎么樣

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.jb51.net
# 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 sh10.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 sh11.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基礎知識99問(五)
  • Linux基礎知識99問(四)
  • Linux基礎知識99問(二)
  • Linux基礎知識99問(一)
  • Linux下PHP+MYSQL+APACHE配置過程 (摘)
  • linux下安裝apache與php;Apache+PHP+MySQL配置攻略
  • linux php mysql數據庫備份實現代碼
  • linux下apache、mysql、php安裝配置詳細筆記
  • linux安裝全中文管理面板教程(php+mysql)
  • hi 感恩節——Linux基礎教程之mysql和php

標簽:金昌 馬鞍山 淘寶邀評 邵陽 巴彥淖爾 婁底 赤峰 許昌

巨人網絡通訊聲明:本文標題《linux基礎之Shell Script入門介紹》,本文關鍵詞  linux,基礎,之,Shell,Script,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《linux基礎之Shell Script入門介紹》相關的同類信息!
  • 本頁收集關于linux基礎之Shell Script入門介紹的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    国产一区二区三区日韩| aaa亚洲精品| 日韩一区在线播放| 欧美电影影音先锋| 成人晚上爱看视频| 免费的成人av| 亚洲综合一区二区三区| 26uuu另类欧美| 欧美人与z0zoxxxx视频| 不卡一区二区三区四区| 久久精品99国产精品| 一区二区在线观看av| 久久亚洲欧美国产精品乐播| 欧美裸体一区二区三区| 91污在线观看| 福利一区二区在线观看| 久久国产精品99久久久久久老狼| 亚洲人吸女人奶水| 欧美激情艳妇裸体舞| 日韩精品中文字幕一区二区三区| 欧美在线制服丝袜| 99精品国产99久久久久久白柏| 亚洲成a人片在线观看中文| 中文字幕制服丝袜一区二区三区| 欧美mv日韩mv国产网站app| 欧美又粗又大又爽| 色婷婷综合久久久中文一区二区| 国产91丝袜在线18| 国产一区不卡精品| 精品一区二区三区免费观看| 视频一区欧美日韩| 亚洲777理论| 午夜视频一区二区三区| 自拍偷自拍亚洲精品播放| 国产欧美综合在线观看第十页| 精品久久久久久最新网址| 91精品国产综合久久精品app| 欧美亚男人的天堂| 欧美在线三级电影| 在线视频国内一区二区| 欧美性猛片aaaaaaa做受| 色94色欧美sute亚洲线路一久| 不卡的看片网站| 成人性生交大片免费看中文| 福利一区二区在线| 不卡在线视频中文字幕| www.爱久久.com| 99久久综合99久久综合网站| 成人av网在线| 91日韩精品一区| 在线看国产一区二区| 在线日韩av片| 欧美一区二区在线视频| 日韩欧美久久久| 精品国产免费一区二区三区香蕉| 精品国产一区二区亚洲人成毛片 | 成人影视亚洲图片在线| 国产精品资源网| 成人免费毛片片v| av一区二区不卡| 色综合久久天天综合网| 欧美日本一区二区三区| 欧美一区二区三区视频在线观看| 日韩欧美一区中文| 国产片一区二区三区| ●精品国产综合乱码久久久久| 自拍偷拍亚洲综合| 日韩高清国产一区在线| 国产一区二区在线看| 成人免费高清在线| 欧美色男人天堂| 精品久久五月天| 日韩毛片精品高清免费| 调教+趴+乳夹+国产+精品| 久久99精品国产麻豆不卡| 成人高清伦理免费影院在线观看| 在线亚洲高清视频| 欧美videos大乳护士334| 国产欧美一区二区三区网站| 伊人色综合久久天天| 久久激情五月激情| 91天堂素人约啪| 日韩丝袜美女视频| 国产精品久久久久四虎| 日本强好片久久久久久aaa| 成人成人成人在线视频| 欧美日韩亚洲丝袜制服| 国产欧美一区二区在线| 亚洲成人黄色小说| 成人中文字幕在线| 91精品一区二区三区久久久久久| 久久精品视频在线看| 亚洲午夜精品在线| 国产成人av电影在线观看| 欧美天堂一区二区三区| 国产亚洲欧洲一区高清在线观看| 亚洲综合区在线| 成人永久看片免费视频天堂| 欧美美女网站色| 1000部国产精品成人观看| 激情综合色播激情啊| 在线观看av不卡| 日本一区二区电影| 美女www一区二区| 欧美制服丝袜第一页| 中文乱码免费一区二区| 日本aⅴ免费视频一区二区三区| 99re在线视频这里只有精品| 精品免费99久久| 亚洲一二三四区| 成人午夜在线免费| 亚洲精品一区二区三区影院| 亚洲国产日韩a在线播放| 成人黄色小视频在线观看| 欧美成人女星排行榜| 亚洲国产一区二区三区| www.久久久久久久久| 国产日本欧洲亚洲| 久久激五月天综合精品| 91精品国产综合久久国产大片| 六月婷婷色综合| 欧美一区二区视频在线观看| 亚洲日本乱码在线观看| 成人免费看的视频| 久久久精品2019中文字幕之3| 麻豆免费看一区二区三区| 欧美伦理影视网| 丝袜亚洲另类欧美综合| 欧美日韩在线一区二区| 亚洲精品乱码久久久久| 91在线视频播放| 中文字幕在线不卡国产视频| 国产91精品精华液一区二区三区| 精品成人a区在线观看| 久久91精品国产91久久小草| 日韩一区和二区| 青青草视频一区| 欧美大片在线观看一区二区| 日本美女一区二区| 日韩午夜激情电影| 蜜桃视频在线观看一区| 日韩一级片在线播放| 日韩精品成人一区二区在线| 欧美精品视频www在线观看| 污片在线观看一区二区| 51精品国自产在线| 免费在线观看不卡| 日韩欧美国产三级电影视频| 久久福利视频一区二区| 欧美精品一区二| 国产凹凸在线观看一区二区| 国产精品视频免费看| 99久久综合国产精品| 亚洲一二三区视频在线观看| 欧美在线观看你懂的| 日韩国产欧美视频| 欧美精品一区二区三区很污很色的| 国产专区欧美精品| 国产精品欧美极品| 91福利国产精品| 亚洲国产精品一区二区www在线| 欧美伦理影视网| 韩国女主播一区二区三区| 国产日韩欧美不卡在线| av网站免费线看精品| 亚洲成av人片一区二区| 精品成人佐山爱一区二区| 成人aa视频在线观看| 亚洲va在线va天堂| 精品精品国产高清一毛片一天堂| 国产激情一区二区三区四区| 国产精品萝li| 777久久久精品| 国产一区二区精品在线观看| 国产精品国产a| 欧美一级高清片| 福利一区二区在线观看| 午夜天堂影视香蕉久久| 久久美女艺术照精彩视频福利播放 | 日韩一区二区在线看片| 国产91精品精华液一区二区三区| 一区二区三区四区五区视频在线观看 | 麻豆精品在线看| 日韩一区欧美一区| 日韩手机在线导航| 色综合久久久网| 激情综合色播激情啊| 岛国一区二区在线观看| 亚洲激情一二三区| 欧美不卡在线视频| 91偷拍与自偷拍精品| 国产一区二区久久| 同产精品九九九| 国产精品久久一卡二卡| 日韩天堂在线观看| 欧美色涩在线第一页| 成人h版在线观看| 久久精品国产秦先生| 亚洲国产三级在线| 日韩伦理免费电影|