學習shell程式設計的兩個例子

2021-07-29 18:29:38 字數 1942 閱讀 7990



例子一:繪製特殊圖形

**如下:

#!/bin/bash

max_no=0

echo -n "enter number between (5 to 9) : "

read max_no

if ! [ $max_no -ge 5 -a $max_no -le 9 ] ; then

echo "wtf... i ask to enter number between 5 and 9, try again"

exit 1

ficlear

for (( i=1; i=i; s-- ))

doecho -n " "

done

for (( j=1; j=1; i-- ))

dofor (( s=i; s<=max_no; s++ ))

doecho -n " "

done

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

doecho -n " ."

done

echo ""

done

你應該不會被上述例子中的「關鍵字」困擾了,很多都是你熟悉的,或者從它們的名字可以猜出它們的意思,如「max」設定某個變數的最大值,「for」是乙個迴圈

輸出結果

**如下:

[root@tecmint ~]# chmod 755 special_pattern.sh

[root@tecmint ~]# ./special_pattern.sh

enter number between (5 to 9) : 6

.. .

. . .

. . . .

. . . . .

. . . . . .

. . . . . .

. . . . .

. . . .

. . .

. ..

例子二:五顏六色的指令碼

linux終端也是支援五顏六色的,請看下面的指令碼:

**如下:

#!/bin/bash

clear

echo -e "\033[1m hello world"

# bold effect

echo -e "\033[5m blink"

# blink effect

echo -e "\033[0m hello world"

# back to noraml

echo -e "\033[31m hello world"

# red color

echo -e "\033[32m hello world"

# green color

echo -e "\033[33m hello world"

# see remaing on screen

echo -e "\033[34m hello world"

echo -e "\033[35m hello world"

echo -e "\033[36m hello world"

echo -e -n "\033[0m"

# back to noraml

echo -e "\033[41m hello world"

echo -e "\033[42m hello world"

echo -e "\033[43m hello world"

echo -e "\033[44m hello world"

echo -e "\033[45m hello world"

echo -e "\033[46m hello world"

echo -e "\033[0m hello world"

你可以對上面的列子舉一反三,把它用到你自己的指令碼中去。

關於演算法的兩個例子

def brute force li,target n len li for i in range 0,n for j in range i 1,n if li i li j target return i,jdef bin search li,val low 0 high len li 1 whi...

C 中Override的兩個例子

main.cc created on 2008 8 5 author administrator includeclass base class derived public base int main int arg,char args 結果 derived clone derived clone...

Python 關於演算法的兩個例子

演算法例子一 給定乙個列表和乙個整數,找到兩個數的下標,使得這兩個數的各為給定的整數,保證肯定僅有乙個結果 窮舉法 def brute force li,target n len li for i in range 0 n for j in range i 1 n if li i li j targ...