好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > linux基礎(chǔ)之Shell Script入門介紹

linux基礎(chǔ)之Shell Script入門介紹

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

linux基礎(chǔ)之Shell Script

1 Shell Scipt
使用指令和基本程序設(shè)計(jì)結(jié)構(gòu)寫成的程序,可以完成復(fù)雜的處理流程

1.1 程序書寫

復(fù)制代碼 代碼如下:

#!/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 說(shuō)明使用的shell類型,不同shell語(yǔ)法可能不同,所以要說(shuō)明使用的是哪種shell
其它#開始的表示注釋,注釋一般需要說(shuō)明
程序功能
版本歷史
作者及聯(lián)系方式
設(shè)置好PATH變量,以便直接可以調(diào)用相應(yīng)路徑下的命令
程序主體部分
exit 0 表示程序執(zhí)行成功,向環(huán)境返回0
1.2 程序執(zhí)行
bash $bash sh01.sh #如果用sh sh01.sh而sh又不是指向bash,那么sh01.sh內(nèi)的語(yǔ)法就會(huì)不一致,因?yàn)橛?#sh去解釋了bash語(yǔ)法寫的shell script,針對(duì)這個(gè)程序,如果 #$type sh #得到sh is hashed (/bin/sh) #那么會(huì)輸出-e Hello world!,而非Hello world!

復(fù)制代碼 代碼如下:

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

注:用bash和用source的不同在于,用bash執(zhí)行時(shí),shell script其實(shí)是在在父程序bash下新建了一個(gè) bash子程序,這個(gè)子程序中執(zhí)行,當(dāng)程序執(zhí)行完后,shell script里定義的變量都會(huì)隨子程序的結(jié)束而消失, 而用source執(zhí)行時(shí),是在父程序bash中執(zhí)行,shell script里定義的變量都還在。

2 簡(jiǎn)單Shell練習(xí)

2.1 例1 接收用戶輸入

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

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

3 判斷式
3.1 測(cè)試文件是否存在
test -e filename會(huì)根據(jù)filename是否存在返回0或1,再交由echo顯示結(jié)果:

復(fù)制代碼 代碼如下:

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

3.2 test常用選項(xiàng)
3.2.1 文件類型

復(fù)制代碼 代碼如下:

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

3.2.2 權(quán)限
-r file :file是否有讀的權(quán)限

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

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

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

$ 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 使用[]判斷

測(cè)試文件是否存在

復(fù)制代碼 代碼如下:

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

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

4 Shell Script 參數(shù)

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

$ 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

注:從以上程序可以看出與參數(shù)有關(guān)的預(yù)設(shè)變量如何表示

5 條件表達(dá)式

5.1 if 結(jié)構(gòu)

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

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

5.2 if else 結(jié)構(gòu)

復(fù)制代碼 代碼如下:

# !/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

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

$ 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 函數(shù)

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

$ 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 循環(huán)
7.1 while

復(fù)制代碼 代碼如下:

# !/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

調(diào)用:

復(fù)制代碼 代碼如下:

$ 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

復(fù)制代碼 代碼如下:

# !/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

調(diào)用示例:

復(fù)制代碼 代碼如下:

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

8 shell script的追蹤與Debug
sh -n xx.sh # 語(yǔ)法檢查
sh -x xx.sh # 列出xx.sh的執(zhí)行過(guò)程

您可能感興趣的文章:
  • Linux基礎(chǔ)知識(shí)99問(wèn)(五)
  • Linux基礎(chǔ)知識(shí)99問(wèn)(四)
  • Linux基礎(chǔ)知識(shí)99問(wèn)(二)
  • Linux基礎(chǔ)知識(shí)99問(wèn)(一)
  • Linux下PHP+MYSQL+APACHE配置過(guò)程 (摘)
  • linux下安裝apache與php;Apache+PHP+MySQL配置攻略
  • linux php mysql數(shù)據(jù)庫(kù)備份實(shí)現(xiàn)代碼
  • linux下apache、mysql、php安裝配置詳細(xì)筆記
  • linux安裝全中文管理面板教程(php+mysql)
  • hi 感恩節(jié)——Linux基礎(chǔ)教程之mysql和php

標(biāo)簽:金昌 馬鞍山 淘寶邀評(píng) 邵陽(yáng) 巴彥淖爾 婁底 赤峰 許昌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux基礎(chǔ)之Shell Script入門介紹》,本文關(guān)鍵詞  linux,基礎(chǔ),之,Shell,Script,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux基礎(chǔ)之Shell Script入門介紹》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于linux基礎(chǔ)之Shell Script入門介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 娇嫩通房h| 亚洲精品456播放| 好男人影视www| 国产推特福利姬在线视频| 精品粉嫩小BBwBBwBBw| 美女的胸免费网站| 激性欧美激情在线播放16页| 美国一极片| 女澡堂洗澡高清播放视频| 国产精品一区伦免视频播放| 男人强行被开发尿孔漫画| 麻豆??视频在线观看免费网站黄| 秋霞亚洲精品毛片一级AV| 欧美午夜艳片欧美精品| 篱笆女人和井电视剧全集50多集| 工口里番无肉码全彩3d动态| 男男高H特虐_高H虐文| 丁香六月天婷婷| 欧美搞b| 秋葵app最新版安装1| 人妻巨大乳挤奶水HD免费看动漫| 2048最新国产の精品合集| 国产A级婬片A片免费妖精| 强制爱肉文| 我让四个舍友爽了一夜TS| 国产在线观看高清精品| 少妇一级婬片免费天气预报| 婚前试爱床戏视频大全| 久久久久中文| 性姿势108式大全图| 女人大荫蒂毛茸茸视频| 江湖遍地卖装备| 冲田杏梨午夜久久99视| 美女张开双腿让男人捅| 特殊的精华油摩擦| 色伦伦小说网| 二级毛片免费观看全程| 亚洲欧美专区精品久久| 男人扒开添女人下部全网站| 免费最爽乱婬无遮在线观看 | 女女操|