linux腳本編寫教程:linux編寫shell腳本程序

目錄

Shell

Shell腳本的執行

Shell腳本編寫規範

Shell 中的變數

變數的算術運算

雙小括弧 (()) 數值運算命令的用法

let 運算命令的用法

expr 命令的用法

br 命令的用法

$[] 符號的運算示例

Shell腳本的條件測試

幾種條件測試語句

文件測試操作符

字元串測試操作符

整數二元比較操作符

邏輯操作符

測試表達式 test 、[] 、[[]] 、 (()) 的區別

if 條件判斷語句

case 條件判斷語句

for循環語句

while循環語句

Break、Continue、exit 循環控制語句

Shell腳本執行scrapy爬蟲和python腳本

Shell

Shell是一個命令解釋器,它的作用是解釋執行用戶輸入的命令及程序等。 用戶每輸入一條命令,Shell就執行一條。這種從鍵盤輸入命令,就可以立即得到回應的對話方式,稱為交互的方式。

當命令或程序語句不在命令行下執行,而是通過一個程序文件來執行時,該程序文件就被稱為Shell腳本。 在Shell腳本里內置了很多命令、語句及循環控制,然後將這些命令一次性執行完畢,這種通過文件執行腳本的方式稱為非交互的方式。 Shell腳本語言很適合用於處理純文本型的數據,而Linux系統中幾乎所有的配置文件、日誌文件,以及絕大對數的啟動文件都是純文本類型的文件。

實驗一

利用case語句編寫腳本,滿足下列要求

1.執行create時根據userfile和passfile建立用戶

2.執行delete時根據userfile刪除用戶

1.編寫腳本:

[root@localhost mnt]# vim user_ctrl.sh

#!/bin/bash

read -p “Please input the operation (create or delete ): ” OPERATION

//輸入你要執行的動作

case $OPERATION in

create) //第一種情況:create

read -p “Please input the userfile : ” USERFILE //提示輸入文件

[ -e $USERFILE ] || { //判斷是否存在

echo “$USERFILE is not exist “

exit 1

}

read -p “Please input the passwdfile : ” PASSFILE

[ -e $PASSFILE ] || {

echo “$PASSFILE is not exist “

exit 1

}

USERLINE=`awk ‘BEGIN{N=0}{N++}END{print N}’ $USERFILE` //計算userfile文件行數

for LINE_NUM in `seq 1 $USERLINE` //利用循環建立

do

USERNAME=`sed -n “${LINE_NUM}p” $USERFILE` //截取userfile文件第一行內容

PASSWORD=`sed -n “${LINE_NUM}p” $PASSFILE` //截取passfile文件第一行內容

useradd $USERNAME //建立用戶

echo $PASSWORD | passwd –stdin $USERNAME

done

;;

delete) //第二種情況:delete

read -p “Please input the userfile : ” USERFILE

[ -e $USERFILE ] || {

echo “$USERFILE is not exist “

exit 1

}

USERLINE=`awk ‘BEGIN{N=0}{N++}END{print N}’ $USERFILE`

for LINE_NUM in `seq 1 $USERLINE`

do

USERNAME=`sed -n “${LINE_NUM}p” $USERFILE`

userdel -r $USERNAME

done

;;

*) //第三種情況:其餘各種情況

echo Eorror!

;;

esac

一篇文章教會你Linux編寫Shell腳本

2.執行:

[root@localhost mnt]# cat userfile

user1

user2

user3

[root@localhost mnt]# cat passfile

123

456

789

[root@localhost mnt]# sh user_ctrl.sh user

Please input the operation (create or delete ): hello //輸入錯誤動作

Eorror!

[root@localhost mnt]# sh user_ctrl.sh user

Please input the operation (create or delete ): create

Please input the userfile : user //輸入錯誤文件

user is not exist

[root@localhost mnt]# sh user_ctrl.sh user

Please input the operation (create or delete ): create

Please input the userfile : userfile

Please input the passwdfile : passfile //建立用戶

Changing password for user user1.

passwd: all authentication tokens updated successfully.

Changing password for user user2.

passwd: all authentication tokens updated successfully.

Changing password for user user3.

passwd: all authentication tokens updated successfully.

[root@localhost mnt]# sh user_ctrl.sh user

Please input the operation (create or delete ): delete //刪除用戶

Please input the userfile : userfile

[root@localhost mnt]# id user1

id: user1: no such user

一篇文章教會你Linux編寫Shell腳本
一篇文章教會你Linux編寫Shell腳本

實驗二

循環

循環執行介紹

將某代碼段重複運行多次,通常有進入循環的條件和退出循環的條件

重複運行次數

  • 循環次數事先已知
  • 循環次數事先未知

常見的循環的命令:for, while, until

一篇文章教會你Linux編寫Shell腳本

for循環

[root@centos7 ~]#help for

for: for NAME [in WORDS … ] ; do COMMANDS; done

Execute commands for each member in a list.

The `for’ loop executes a sequence of commands for each member in a

list of items. If `in WORDS …;’ is not present, then `in “$@”‘ is

assumed. For each element in WORDS, NAME is set to that element, and

the COMMANDS are executed.

Exit Status:

Returns the status of the last command executed.

for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done

Arithmetic for loop.

Equivalent to

(( EXP1 ))

while (( EXP2 )); do

COMMANDS

(( EXP3 ))

done

EXP1, EXP2, and EXP3 are arithmetic expressions. If any expression is

omitted, it behaves as if it evaluates to 1.

Exit Status:

Returns the status of the last command executed.

格式1:

for NAME [in WORDS … ] ; do COMMANDS; done for 變數名 in 列表;do 循環體 done for 變數名 in 列表 do 循環體 done

執行機制:
依次將列表中的元素賦值給「變數名」; 每次賦值後即執行一次循環體; 直到列表中的元素耗盡,循環結束

for循環列表生成方式:

直接給出列表

整數列表:

{start..end}

$(seq [start [step]] end)

返回列表的命令:

$(COMMAND)

使用glob,如:*.sh

變數引用,如:$@每個參數為獨立字元串,$#傳遞給腳本的參數的個數,$*全部參數合為一個字元串

範例:面試題,計算1+2+3+…+100的結果

[root@centos8 ~]#sum=0;for i in {1..100};do let sum+=i;done ;echo sum=$sum

sum=5050

[root@centos8 ~]#seq -s+ 100|bc5050

5050

1

2

3

4

範例:

[root@centos8 ~]#cat /data/scripts/for_sum.sh

#!/bin/bash

sum=0

for i in $* ; do

let sum+=i

done

echo sum=$sum

[root@centos8 ~]#bash /data/scripts/for_sum.sh 1 2 3 4 5 6

sum=21

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/226541.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:50
下一篇 2024-12-09 14:50

相關推薦

發表回復

登錄後才能評論