#!/bin/bash
if rpm -q sudo > /dev/null; then
echo "sudo is installed and version is"
else
echo "sudo is not installed"
fi
您好想知道是否可以在檢查安裝后列印出我的 sudo 版本。如果有辦法,我該怎么做
uj5u.com熱心網友回復:
sudo --version必須決議輸出并使用回傳狀態來確定它是否已安裝。
首先,無論活動系統區域設定如何,您都希望版本輸出保持一致。
所以你用LANG=C sudo --version.
這會列印多行,只有第一行最后一個元素是 sudo 版本:
Sudo version 1.9.5p2
Sudoers policy plugin version 1.9.5p2
Sudoers file grammar version 48
Sudoers I/O plugin version 1.9.5p2
Sudoers audit plugin version 1.9.5p2
使用 POSIX-shell 語法,行和欄位決議必須使用外部工具完成。這里awk能夠一次決議所有內容:
#!/usr/bin/env sh
# Captures version string parsed with awk:
# so only last entry of first line is captured
if sudo_version=$(LANG=C sudo --version 2>/dev/null | awk 'NR==1{print $NF}')
then printf 'sudo is installed and version is %s\n' "$sudo_version"
else printf 'sudo is not installed\n' >&2
fi
如果使用 Bash,將第一行捕獲到陣列中,以保存使用外部工具和管道分叉程序:
#!/usr/bin/env bash
# Capture version string first line into a Bash array
if read -ra version_array < <(LANG=C sudo --version 2>/dev/null)
then
# Version is the last element of array
printf 'sudo is installed and version is %s\n' "${version_array[-1]}"
else
printf 'sudo is not installed\n' >&2
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420375.html
標籤:
