简体   繁体   English

Linux脚本不起作用

[英]Linux script doesn't work

I have the following code: 我有以下代码:

#!/bin/bash
# script.sh

awk -F: '{if ($3 > 500) {print $3}}' /etc/passwd
awk -F: '{if ($3 > 500) {
if (size = "$(du -s /home/$1)" > 10000) {
mkdir /home/copy/R$1
cp /home/$1 /home/copy/R$1
}
}}' /etc/passwd

The first awk prints all the users with the UID above 500. The second awk should create the directory /home/copy/Ruser, and copy all the files of the user directory in it, when the size of the user directory is above 10000, but it doesn't do anything, and there's no error when I run the script. 第一个awk打印所有UID大于500的用户。第二个awk应创建目录/ home / copy / Ruser,并在用户目录的大小大于10000时复制该用户目录中的所有文件,但是它什么也没做,而且运行脚本时也没有错误。

$( du -s ) is a shell syntax, not awk syntax. $( du -s )是shell语法,而不是awk语法。 The following assigns the size of the home directory to size in gawk: 以下将主目录的大小分配给gawk中的size

"du -s "$6" | cut -f1"  | getline size
if (size > 10000) {
    ...

Using awk for this is probably more trouble than it's worth: 为此,使用awk可能会带来更大的麻烦:

#!/bin/bash

while IFS=: read user passwd uid gid info dir shell; do
  test $uid -gt 500 2> /dev/null || continue  # Skip if uid too low
  size="$(du -s $dir)"
  if test "$size" -gt 10000; then
    mkdir /home/copy/R$user
    cp /home/$1 /home/copy/R$user  # This probably won't work!
  fi
done < /etc/passwd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM