Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux中向组发送消息_Linux_Shell - Fatal编程技术网

在Linux中向组发送消息

在Linux中向组发送消息,linux,shell,Linux,Shell,我找到了通过用户名向当前登录的用户发送消息的方法: who | grep username | cut -c1-20 | while read line; do printf "Message Text" | write $line ; done 但是,我是否可以按组向当前登录的用户发送消息 感谢您的帮助。如果您将变量group设置为您感兴趣的组,您可以尝试以下操作: for i in $(who -u | cut -d " " -f1 | sort | uniq); do if ech

我找到了通过用户名向当前登录的用户发送消息的方法:

who | grep username | cut -c1-20 | while read line; do  printf  "Message Text" | write $line ; done
但是,我是否可以按组向当前登录的用户发送消息


感谢您的帮助。

如果您将变量
group
设置为您感兴趣的组,您可以尝试以下操作:

for i in $(who -u | cut -d " " -f1 | sort | uniq); do if  echo $(groups $i | cut -d " " -f3-) | grep $group >/dev/null; then echo "Message Text" | write $i; fi; done
$(who-u | cut-d”“-f1 | sort | uniq)
给出登录用户的列表

$(组$i | cut-d”“-f3-)
提供用户所属的所有组


grep
语句将减少属于
$group

的用户的列表。如果将变量
group
设置为感兴趣的组,则可以尝试以下操作:

for i in $(who -u | cut -d " " -f1 | sort | uniq); do if  echo $(groups $i | cut -d " " -f3-) | grep $group >/dev/null; then echo "Message Text" | write $i; fi; done
$(who-u | cut-d”“-f1 | sort | uniq)
给出登录用户的列表

$(组$i | cut-d”“-f3-)
提供用户所属的所有组


grep
语句将减少属于
$group
的用户的列表,因此。。。要向特定组中的每个登录用户发送消息,请在他们登录到的每个tty上

#!/bin/sh

usage() {
  cat <<-EOT
        Usage: writegroup groupname [message]

        where [message] will be taken from stdin if not provided on the command line.
EOT
}

if [ $# -eq 0 ]; then
  usage
  exit 1
fi

groupname="$1"
shift
message="$*"

if [ -z "$message" ]; then
  read message
fi

who | while read user tty junk; do
  if groups "$user" | grep -wq "$groupname"; then
    echo "$message" | write "$user" "$tty"
  fi
done
#/垃圾箱/垃圾箱
用法(){

cat所以…要在每个登录到的tty上向特定组中的每个登录用户发送消息

#!/bin/sh

usage() {
  cat <<-EOT
        Usage: writegroup groupname [message]

        where [message] will be taken from stdin if not provided on the command line.
EOT
}

if [ $# -eq 0 ]; then
  usage
  exit 1
fi

groupname="$1"
shift
message="$*"

if [ -z "$message" ]; then
  read message
fi

who | while read user tty junk; do
  if groups "$user" | grep -wq "$groupname"; then
    echo "$message" | write "$user" "$tty"
  fi
done
!/bin/sh
用法(){

cat在/etc/group中,内容类似于group:x:111。在etc/group-中,内容类似于username:x:111。找不到group.username,因此没有使用您的脚本发送消息。请帮助。在我的ubuntu上,
/etc/group
的格式是
sudo:x:27:user1,user2
。在您的系统上是否相同?如果不是,请给出一个例子e至少包含该文件的2个用户。此外,最好使用
groups
命令,而不是解析不同操作系统甚至不同发行版的系统文件。感谢您的提示,我使用
groups
命令编辑了我的答案。使用groups命令可以工作!但是,此脚本不会在他们登录的每个tty上发送。in/etc/group,内容类似于group:x:111。在etc/group-中,内容类似于username:x:111。找不到group.username,因此没有使用您的脚本发送消息。请提供帮助。在我的ubuntu上,
/etc/group
的格式是
sudo:x:27:user1,user2
。在您的系统上是否相同?如果不是,请给出一个示例行至少有两个用户使用该文件。此外,最好使用
命令,而不是解析系统文件,因为系统文件可能因操作系统而异,甚至因发行版而异。感谢您的提示,我使用
命令编辑了我的答案。使用组命令可以工作!但是,此脚本不会在登录的每个tty上发送。命令
groups$line
将返回用户所属组的列表。您可以检查所需组是否在列表中。命令
groups$line
将返回用户所属组的列表。您可以检查所需组是否在列表中。