shell指令碼(一)

2021-09-27 13:26:32 字數 4819 閱讀 9582

1、列印九九乘法表

[root@centos7 scripts]

# vim 99chengfb.sh

#!/bin/bash

for i in;do

#for j in `seq 1 $i`;do

#for j in `eval echo `;do

for j in

$(eval

echo);

do result=$[

$j*$i

]echo -e "$x$=$result \t\c"

done

echo

done

執行結果

[root@centos7 scripts]

# ./99chengfb.sh

1x1=1

1x2=2 2x2=4

1x3=3 2x3=6 3x3=9

1x4=4 2x4=8 3x4=12 4x4=16

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

2、列印等腰三角形。(星個數:start=line*2-1;空格個數:space=總行數-line)

[root@centos7 scripts]

# cat ********.sh

#!/bin/bash

read -p "please input a num " num

for i in

`seq 1 $num`;do

for j in

`seq 1 $[$num-$i]`;

doecho -e " \c"

done

for k in

`seq 1 $[$i*2-1]`;

doecho -e "*\c"

done

echo

done

[root@centos7 scripts]

# ./********.sh

please input a num 5

****

*****

*******

*********

[root@centos7 scripts]

#

3、列印西洋棋棋盤

[root@centos7 scripts]

# cat color.sh

#!/bin/bash

greyel (

)yelgre (

)for i in;do

for((j=

1;j<=

4;j++

))do

if[ $[

$i%2] -eq 0 ]

;then

greyel

else

yelgre

fidone

echo

done

[root@centos7 scripts]

# ./color.sh

4、批量建立使用者和批量刪除使用者

[root@centos7 scripts]

# cat batch_creatuser.sh

#!/bin/bash

until

["$#" -le 0 ];do

useradd

$1echo

"$1 is created"

shift

done

echo

"done"

[root@centos7 scripts]

# cat batch_deleteuser.sh

#!/bin/bash

#while [ "$#" -gt 0 ];do

while

[! -z "$1"];

douserdel -r $1

&> /dev/null &&

echo

"$1 is deleted"

shift

done

echo

"done"

5、判斷系統的版本號

[root@centos7 scripts]

# cat check_version.sh

#!/bin/bash

version (

)version

[root@centos7 scripts]

# ./check_version.sh

this is 7 version!

!!

6、隨機生成10以內的數字,實現猜字遊戲,提示比較大或小,相等則退出

[root@centos7 scripts]

# cat guest.sh

#!/bin/bash

rand=$[

$random%11]

while

true;do

read -p "please input a number " num

if[[$num

=~ ^[

[:digit:]

]+$ ]];

thenif[

$rand -gt $num];

then

echo little

elif

[$rand -lt $num];

then

echo great

else

echo right

break

fielse

echo

"please input a number:1-10"

continue

fidone

執行結果

[root@centos7 scripts]

# ./guest.sh

please input a number 6

great

please input a number 5

great

please input a number 3

little

please input a number 4

right

7、實現階乘

[root@centos7 scripts]

# cat jiecheng.sh

#!/bin/bash

read -p "please input a number: " num

fact(

)fact $num

[root@centos7 scripts]

# ./jiecheng.sh

please input a number: 9

362880

8、實現100以內所有正奇數之和,實現1到100所有正數之和

[root@centos7 scripts]

# ./test_for.sh

sum=5050

sum1=2500

[root@centos7 scripts]

# cat test_for.sh

#!/bin/bash

declare -i sum=0

declare -i sum1=0

for i in;do

let sum=sum+i

done

echo sum=

$sum

for i in;do

let sum1+=i

done

echo sum1=

$sum1

[root@centos7 scripts]

# ./test_for.sh

sum=5050

sum1=2500

9、case測試指令碼

[root@centos7 scripts]

# cat test_case.sh

#!/bin/bash

read -p "do you argee? yes or no:" ans

if[ -z "$ans"];

then

echo

"please input yes or no"

exit

ficase

$ans

in[yy]

|[yy]

[ee]

[ss]

)echo your answer is yes ;;

[nn]

|[nn]

[oo]

)echo your answer is no ;;

*)echo your answer is false

esac

[root@centos7 scripts]

# ./test_case.sh

do you argee? yes or no:y

your answer is yes

shell指令碼一

shell 手工部署專案 自動化部署專案 1 shell簡介 什麼是shell shell是乙個命令直譯器,是乙個程式 bin bash,解釋linux的命令 shell互動式命令使用 開啟終端,一行行敲命令 shell指令碼 一系列的命令組成的檔案,結合shell語法 shell指令碼注釋 1 單...

Shell指令碼 一

1.特殊變數 1 2 表示指令碼後接的引數值 引數的數量 所有引數 0 指令碼名 程序號 退出碼2.條件判斷 2.1.if elif else語句 bin bash if condition1 condition2 then text1 elif condition3 then text2 else...

shell指令碼(一)

shell 1.概述 shell是乙個命令列直譯器,它接收應用程式 使用者命令,然後呼叫作業系統核心 還是乙個功能相當強大的程式語言,易編寫.易除錯.靈活性強 2.shell解析器 檢視linux提供的shell解析 cat etc shells sh和 bash是常用的 bash包含了其他的功能,...