簡體   English   中英

初學者的Bash腳本:“一元運算符預期”錯誤&&如何使用grep從命令輸出中搜索變量?

[英]Beginner's Bash Scripting: “Unary Operator Expected” error && how to use grep to search for a variable from the output of a command?

我正在嘗試編寫一個通過“./filename username”執行的bash腳本,並檢查該用戶是否已登錄,並打印結果。 我還不熟悉腳本,並且無法理解如何使這項工作成功。

我目前得到錯誤“第7行:[:ambonill:一元運算符預期”。 這意味着什么,我該如何解決這個錯誤呢?

另外,我如何讓grep工作而不是sort | uniq的? 我想從命令輸出中查找變量,但在手冊頁中找不到任何相關內容。

#! /bin/bash
# This script will take a username as an argument and determine whether they are logged on.

function loggedin {
   for u in `who | cut -f1 -d" " | sort | uniq`
   do
   if [ $u == $1 ]
   then
      echo "$1 is logged on"
   else
      echo "$1 is not logged on"
   fi
   exit 0
   done
}

loggedin $u
exit 1

嘗試找到一個更簡單的解決方案,例如:

#!/bin/bash
echo "$1 is $([ -z "$(w -h $1)" ]&&echo -n not\ )logged on"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM