什么會轉到,翻譯成 bash 腳本
這是我的代碼:
#!/bin/bash
read distro </etc/issue
echo $distro
if [[ "$distro" = "Debian GNU/Linux bookworm/sid n l" ]];
then
goto Debian:
elif [[ "$distro" = "Ubuntu GNU/Linux bookworm/sid n l" ]];
then
goto Ubuntu:
elif [[ "$distro" = "kali GNU/Linux bookworm/sid n l" ]];
then
goto Kali:
elif [[ "$distro" = "Arch GNU/Linux bookworm/sid n l" ]];
then
goto Arch:
else
echo No suported OS detected some thing may not work
fi
Debian:
echo using Debian
sleep 5
exit
Ubuntu:
echo using Ubuntu
sleep 5
exit
Kali:
echo using kali
sleep 5
exit
Arch:
echo using Arch
sleep 5
exit
它是一個非常簡單的代碼,我什至不知道我檢查 Linux 發行版的方式是否可行
我已經嘗試使用 Windows 中的一批 Goto 功能,但它無法在 Linux 上運行,我將如何從一行跳到另一行
uj5u.com熱心網友回復:
我建議使用帶有 fall-through 的 Switch-Case 陳述句。
所有“處理程式”都可以在將從開關呼叫的函式中定義:
#!/bin/bash
handleDebian() {
echo "Using Debian"
}
handleUbuntu() {
echo "Using Ubuntu"
}
handleKali() {
echo "Using Kali"
}
handleArch() {
echo "Using Arch"
}
handleUnknown() {
echo "No suported OS detected some thing may not work"
}
read distro </etc/issue
case "${distro}" in
*Debian*) handleDebian() ;;
*Ubuntu*) handleUbuntu() ;;
*kali*) handleKali() ;;
*Arch*) handleArch() ;;
*) handleUnkown() ;;
esac
uj5u.com熱心網友回復:
在這種情況下,我會選擇更緊湊的case陳述句,例如:
read distro </etc/issue
echo $distro
case "${distro}" in
*Debian*) echo "Debian" ;;
*Ubuntu*) echo "Ubuntu" ;;
*kali*) echo "kali" ;;
*Arch*) echo "Arch" ;;
*) echo "No suported OS detected some thing may not work";;
esac
sleep 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520794.html
標籤:linux重击嘘
