shell程式設計示例

2021-09-23 01:49:17 字數 2101 閱讀 5037

is_root.sh

#!/bin/bash

# 通過環境變數判斷

test=$(env | grep 'user' | cut -d "=" -f 2)

if [ "$test" == "root" ]

then

echo "current user is root."

else

echo "not root."

fi

注意:給指令碼賦予執行許可權,如 chmod 755 is_root.sh

judge_apache.sh

#!/bin/bash

# 通過程序檢視apache

# 獲取輸入數

read -p "input n1: " n1

read -p "input ope: " ope

read -p "input n2: " n2

# 三個輸入數都不為空

if [ -n "$n1" -a -n "$n2" -a -n "$ope" ]

then

# 判斷兩個數是否都是數字

shell指令碼示例

shell基礎 一 迴圈1 100,並計算1 2 3 4 5 6 7 8 9 10的和 bin bash sum 0 for i 1 i 100 i dowhile i le 10 dosum sum i break done done echo sum me 注意幾點 1.sum 0要進行變數的初...

shell高亮示例

bin bash define log color readonly fg grey 30 灰色readonly fg red 31 readonly fg green 32 readonly fg yellow 33 readonly fg blue 34 前景色藍色 readonly fg vi...

shell 多執行緒示例

shell指令碼本身不支援多執行緒語法,這裡所說的多執行緒本質上是使部分 在後台執行 多程序 使cpu自動的呼叫各個程序,最大限度的利用cpu的效能。這裡所說的能使用多執行緒的場景值得是部分 塊可以並行執行,相互之間沒有影響的情況。bin bash echo begin for num in seq...