Linux shell指令碼程式設計基礎之練習篇

2021-09-22 17:07:15 字數 4917 閱讀 8311

shell指令碼程式設計基礎之練習篇。

#!/bin/bash

if [ $# -ne 1

]then

echo

"請輸入乙個引數

"exit

else

echo

"引數正確

"newfile=$1fi#

echo `grep

"^#\!"$`

if ! grep

"^#\!

" $ &>/dev/null

then

cat >>$<#!/bin/bash

# author: inert your name here.

#date & time: `date +"

%f %t"`

#description: please edit here.

eoffi

vi +5 $

將指令碼改個名字例如:newshfile,將其放置在/bin/目錄下,那麼你的系統就多了乙個新的newshfile命令了

#!/bin/bash

# author: inert your name here.

#date & time: 2015-06-02

20:37:07

#description: please edit here.

let

sum=0

for index in

doif [ $[ $%2 ] == 0 ]; then

#let

sum+=$

sum=`expr $ +$`

fidone

echo

"sum=$

"let

sum=0

for num in $(seq

1100); do

if [ $[ $num % 2 ] == 0 ]; then

sum=`expr $sum +$num`

fidone

echo

"sum=$sum

"

#!/bin/bash

if [ $# -eq 2

]then

echo

"引數個數 $#\n

"echo

"引數相加 $1 + $2 = `expr $1 + $2`

"else

echo

"引數個為 $#,本指令碼需要兩個引數

"fi

#!/bin/sh

num=5

while test $num != 0

doecho

"$num

"num=`expr $num - 1

`done

echo

"*****************************

"num=5

while (($num != 0

))do

echo

"$num

"num=`expr $num - 1

`done

echo

"*****************************

"for num in

doecho

"$num

"done

echo

"*****************************

"for ((num=5;$num>0;num=`expr $num - 1

`))do

echo

"$num

"done

#!/bin/bash

# author: inert your name here.

#date & time: 2015-06-02

21:32:51

#description: please edit here.

if test $# == 3

then

echo

"引數個數$#,引數:$@

"case $1

in +)

num=`expr $2 + $3

` ;;

-)num=`expr $2 - $3

` ;;

x)num=`expr $2 \* $3

` ;;

/)num=`expr $2 \/ $3

` ;;

*)echo

"只允許+ - x /這幾個運算子";;

esac

echo

"num=$num

"else

echo

"引數個數為3個,分別為\"運算子 引數1 引數2\"

"fi

echo 5.12 + 2.5 | bc

#!/bin/bash

# author: inert your name here.

#date & time: 2015-06-02

22:07:28

#description: please edit here.

a=5.66

b=8.67

c=`echo $a + $b |bc`

echo

"$a + $b = $c

"

1

22333

4444

55555

****************************112

1231234

12345

****************************

|_||_

|||_

||||_

|||||_

****************************

* * *

* * *

* * * *

* * * * *

* * * * *

* * * *

* * *

* **************lengxing****************

** *

* * *

* * * *

* * * * *

* * * * * *

* * * * *

* * * *

* * *

* **

#!/bin/bash

# author: inert your name here.

#date & time: 2015-06-02

22:24:41

#description: please edit here.

for ((num=1;$num<=5;num=`expr $num + 1

`))do

for((index=$num;$index>0;index=`expr $index - 1

`))

doecho -n "

$num

"done

echo

""done

echo

"****************************

"for ((num=1;$num<=5;num=`expr $num + 1

`))do

for((index=1;$index<=$num;index=`expr $index + 1

`))

doecho -n "

$index

"done

echo

""done

echo

"****************************

"for ((num=1;$num<=5;num=`expr $num + 1

`))do

for((index=1;$index<=$num;index=`expr $index + 1

`))

doif [ $index%2 == 0 ]; then

echo -n "

"else

echo -n "|"

fidone

echo -n "_"

echo

""done

echo

"****************************

"for (( i=1; i<=5; i++))

dofor (( j=1; j<=i; j++))

doecho -n "*"

done

echo

""done

for (( i=5; i>=1; i--))

dofor (( j=1; j<=i; j++))

doecho -n "*"

done

echo

""done

echo

"************lengxing****************

"max=6

for ((i=1; i<=$max; i++))

dofor ((j=$max-i; j>0; j--))

doecho -n "

"done

for ((k=1; k<=i; k++))

doecho -n "*"

done

echo

""done

for ((i=1; i<=$max; i++))

dofor ((k=1; k<=i; k++))

doecho -n "

"done

for ((j=$max-i; j>0; j--))

doecho -n "*"

done

echo

""done

《Linux shell指令碼程式設計入門》

linux shell指令碼程式設計入門 linuxos在伺服器以及近來引發熱議的嵌入式系統中都得到廣泛應用,linuxshell指令碼程式設計入門 介紹的就是該程式必備的shell程式設計要素。尤其還收錄了現在門戶 企業等使用的主流套件centos communityenterpriseopera...

Linux shell指令碼程式設計基礎

把指令碼路徑寫入 path變數。帶上路徑執行,需要執行許可權。bash bash後跟上指令碼,該方法指令碼不需要執行許可權也能執行。bash 讀入指令碼內容,通過管道符交給bash執行。當前主機遠端呼叫其他主機的指令碼。1.宣告shell型別 bin bash 2.變數 3.函式 4.主程式 在編寫...

Linux Shell指令碼程式設計入門

shell 指令碼是包含一系列命令的文字本當 讀取執行該文字文件就可以執行文件裡面的命令 常用於執行批量事務處理 重複性操作 自動化運維 定時執行任務等 編寫乙個簡單 shell 指令碼 1 建立 shell 指令碼文件 vi hellow.sh2 編寫 shell 指令碼 注意第一行是標記這個指令...