首页 > 解决方案 > 理解 bash 代码中的输出的问题

问题描述

我在 bash 中有以下代码:

#!/bin/sh
w=`who | grep $1`
if [ -z "$w" ]; then
echo "$1 ... ";
fi

你能帮我理解这段代码的输出吗?我可以看到它将显示命令行中的给定参数($1,$2...)

但我真的不明白这两行:

w=`who | grep $1`
if [ -z "$w" ]; then

你能给我一些帮助吗,因为我找不到太多信息

标签: bashsh

解决方案


Looks to me a script to check if the username you pass as parameter to the script is logged on the system.

For example if you call it with ./check.sh nonexistinguser it will print nonexistinguser ....

Explanation:

w=`who | grep $1`

Execute who, which displays who is logged in, then grep the result for whatever parameter you passed to the script ($1), store the result in a variable called w; have a look here for the docs on -z

if [ -z "$w" ]; then

If "$w" has length zero, execute the body of the if.


推荐阅读