欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Shell腳本之while循環(huán)應(yīng)用具體案例

 更新時(shí)間:2025年04月28日 09:02:27   作者:難釋?xiě)? 
這篇文章主要介紹了Shell腳本之while循環(huán)應(yīng)用的相關(guān)資料,通過(guò)四個(gè)案例展示了如何利用while循環(huán)來(lái)處理不同場(chǎng)景下的編程問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

在Shell腳本編程中,while循環(huán)是一種非常有用的控制結(jié)構(gòu),適用于需要基于條件進(jìn)行重復(fù)操作的場(chǎng)景。與for循環(huán)不同,while循環(huán)通常用于處理不確定次數(shù)的迭代或持續(xù)監(jiān)控某些狀態(tài)直到滿足特定條件為止的任務(wù)。本文將通過(guò)幾個(gè)實(shí)際的應(yīng)用案例來(lái)展示如何使用while循環(huán)解決具體的編程問(wèn)題。

案例一:監(jiān)控服務(wù)器資源使用情況

假設(shè)我們需要編寫(xiě)一個(gè)腳本來(lái)實(shí)時(shí)監(jiān)控服務(wù)器的CPU和內(nèi)存使用率,并在任一項(xiàng)超過(guò)設(shè)定閾值時(shí)發(fā)送警告信息。

腳本示例:

#!/bin/bash

cpu_threshold=80
mem_threshold=75

echo "Monitoring CPU and Memory usage..."

while true; do
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}') # 獲取CPU使用率
    mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}') # 獲取內(nèi)存使用率
    
    if (( $(echo "$cpu_usage > $cpu_threshold" | bc -l) )); then
        echo "Warning: CPU usage is above threshold at $cpu_usage%"
    fi

    if (( $(echo "$mem_usage > $mem_threshold" | bc -l) )); then
        echo "Warning: Memory usage is above threshold at $mem_usage%"
    fi
    
    sleep 5 # 每隔5秒檢查一次
done

說(shuō)明:

  • 使用top命令獲取CPU使用率,free命令獲取內(nèi)存使用率。
  • bc -l用于執(zhí)行浮點(diǎn)數(shù)比較。
  • 通過(guò)sleep 5讓腳本每隔5秒檢查一次系統(tǒng)狀態(tài)。

案例二:讀取文件并處理每一行

假設(shè)我們有一個(gè)包含多個(gè)URL的文本文件,需要對(duì)每個(gè)URL發(fā)起HTTP請(qǐng)求以檢查其可訪問(wèn)性。

腳本示例:

#!/bin/bash

input_file="urls.txt"

while IFS= read -r url
do
    if curl --output /dev/null --silent --head --fail "$url"; then
        echo "$url is up"
    else
        echo "$url is down"
    fi
done < "$input_file"

說(shuō)明:

  • 使用IFS=防止行首尾的空白被忽略。
  • curl --output /dev/null --silent --head --fail用于檢測(cè)URL是否可訪問(wèn)。
  • < "$input_file"將文件內(nèi)容作為輸入傳遞給read命令。

案例三:用戶交互式菜單

創(chuàng)建一個(gè)簡(jiǎn)單的用戶交互式菜單,允許用戶選擇不同的操作直到他們選擇退出。

腳本示例:

#!/bin/bash

while true; do
    echo "Menu:"
    echo "1) Display current date and time"
    echo "2) List files in current directory"
    echo "3) Exit"
    read -p "Please enter your choice [1-3]:" choice

    case $choice in
        1)
            date
            ;;
        2)
            ls
            ;;
        3)
            echo "Exiting..."
            break
            ;;
        *)
            echo "Invalid option, please try again."
            ;;
    esac
done

說(shuō)明:

  • read -p提示用戶輸入選項(xiàng)。
  • 使用case語(yǔ)句根據(jù)用戶的選擇執(zhí)行相應(yīng)的操作。
  • break用于退出無(wú)限循環(huán)。

案例四:批量重命名文件

假設(shè)我們有一組文件名不符合規(guī)范,需要對(duì)其進(jìn)行批量重命名。

腳本示例:

#!/bin/bash

prefix="new_"

ls | while read -r file; do
    if [[ $file != ${prefix}* ]]; then
        mv "$file" "${prefix}${file}"
        echo "Renamed '$file' to '${prefix}${file}'"
    fi
done

說(shuō)明:

  • 使用ls列出當(dāng)前目錄下的所有文件。
  • if [[ $file != ${prefix}* ]]確保只重命名不帶前綴的文件。
  • mv "$file" "${prefix}${file}"添加指定前綴并重命名文件。

結(jié)語(yǔ)

到此這篇關(guān)于Shell腳本之while循環(huán)應(yīng)用的文章就介紹到這了,更多相關(guān)Shell腳本while循環(huán)案例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論