shell小指令碼

2021-07-11 02:46:15 字數 1086 閱讀 3424

前言:可能這些指令碼看起來很簡單,很多人都不屑於去寫,但是一切複雜的東西都是由乙個個簡單的東西組合起來的。

shell的語法比較怪異,和一般的程式語言有很大的區別,真的要多動手去寫才能學會的

1. 要求:

1. 設定變數file的值為/etc/passwd

2. 依次向/etc/passwd中的每個使用者問好,並顯示對方的shell,形如: hello, root, your shell: /bin/bash

3. 統計一共有多少使用者

#!/bin/bash

file="/etc/passwd"

context=`cat /etc/passwd`

for i in $context; do

echo "hello, `echo $i |cut -d: -f1`, your shell: `echo $i |cut -d: -f7`"

done

echo "a total of `cat /etc/passwd |wc -l` user"

2. 求100以內能被3整除的數相加的和

#!/bin/bash

sum=0

for i in ; do

mod=$[$i%3]

if [ $mod -eq 0 ]; then

sum=$[$sum+$i]

fidone

echo "sum=$sum"

3. 隨機生成10個數,並給這10個數排序

#!/bin/bash

#declare -a a

for x in ;do

a[$x]=$[$random]

done

for (( i=0; i<=9; i++));do

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

if [[ $ -gt $ ]];then

t = $[$]

a[i] = $[$]

a[j] = $[$t]

fidone

done

for y in ;do

echo $

done

shell小指令碼4

1 編寫指令碼selinux.sh,實現開啟或禁用selinux功能 selinux有三種模式 enforcing 強制模式。代表selinux在執行,且已經開始限制domain type之間的驗證關係 permissive 寬容模式。不會限制domain type之間的驗證關係,即使驗證不正確,程...

Linux 常用shell小指令碼

1.計算指令碼運算時間 bin bash st date y m d h m s 運算過程開始 sleep 2 運算過程結束 en date y m d h m s st c date d st s en c date d en s interval expr en c st c echo star...

shell小指令碼 網速監控

在windows中,我們可以在360等管家軟體中顯示網速,在linux下想要檢視實時的網速怎麼辦呢?當然在linux下也有很多 思路 在執行ifconfig檢視網絡卡資訊的時候,可以看到以下內容 ifconfig wlp2s0 wlp2s0 flags 4163 mtu 1500 inet 192....