0

我必须将文件复制(如果已经存在,则覆盖)到同一组(“学生”)的用户成员的所有主目录。

我找到了一个我试图适应我的上下文的脚本(我有 LDAP 用户而不是 /etc/passwd,所以我使用 getent passwd 来获取用户名)。

这是脚本(cp2stud.sh):

#!/bin/bash

# subdirectory of /home/uid
DIR=".eclipse/org.eclipse.platform_3.8_155965261/configuration"

# the file to copy
FILE="/home/admin/tmp/config.ini"

# location of home dirs
UHOME="/home" 

# GID of "students" group
USERS_GID=10004

# get list of users having same GID
_USERS="$(getent passwd | awk -F ':' '{if ( $4 == $USERS_GID ) print $1 }')"

for u in $_USERS
do
   _dir="${UHOME}/${u}/${DIR}"
   if [ -d "$_dir" ]
   then
     yes | /bin/cp -v "$FILE" "$_dir"
     chown $(id -un $u):students "$_dir/${FILE}"
   fi
done

当我尝试启动它时:

$ sudo cp2stud.sh

我什么都得不到。

我在哪里弄错了?

提前致谢

4

3 回答 3

1
_USERS="$(getent passwd | awk -v X="$USERS_GID" -F ':' '{if ( $4 == X ) print $1 }')"
于 2014-08-11T20:37:34.657 回答
0

以下代码有效:

DIR=".eclipse/org.eclipse.platform_3.8_155965261/configuration"
FILE="/home/admin/tmp/config.ini"

UHOME="/home"
USERS_GID=10004
GRP_NAME=students
FILENAME=$(basename $FILE)


_USERS="$(getent passwd | awk -v X="$USERS_GID" -F ':' '{if ( $4 == X ) print $1 }')"

for u in $_USERS
do
  _dir="${UHOME}/${u}/${DIR}"
  if [ -d "$_dir" ]
  then
      yes | /bin/cp -v "$FILE" "$_dir"
      chown -v $(id -un $u):$GRP_NAME "${UHOME}/${u}/${DIR}${FILENAME}"
  fi
done
于 2014-08-12T06:09:53.417 回答
0

试试这个方法:

...
export USERS_GID=10004

_USERS=$(getent passwd | awk -F ':' '{if ( $4 == ENVIRON["USERS_GID"] ) print $1 }')
...

你也可以这样做:

...

USERS_GID=10004

_USERS=$(getent passwd | awk -F ':' -v gid=$USERS_GID '{if ( $4 == gid ) print $1 }')
...

要不就:

...

_USERS=$(getent passwd | awk -F ':' -v gid=10004 '{if ( $4 == gid ) print $1 }')
...
于 2014-08-11T20:35:25.553 回答