【问题标题】:Why can't this script execute the other script为什么这个脚本不能执行另一个脚本
【发布时间】:2015-04-06 16:18:57
【问题描述】:

此脚本会查找其中包含字符串 RECHERCHE 的所有用户。我尝试在 sudo 中运行它并且它有效,但随后在第 8 行停止(权限被拒绝)。即使从脚本中删除 sudo,仍然会发生此问题。

#!/bin/bash
#challenge : user search and permission rewriting
echo -n "Enter string to search : "
read RECHERCHE
echo $(cat /etc/passwd | grep "/home" | cut -d: -f5 | grep -i "$RECHERCHE" | sed s/,//g)
echo "Changing permissions"
export RECHERCHE
sudo ./challenge2 $(/etc/passwd) &

然后,第二个脚本在后台更改属于 RECHERCHE 找到的每个用户的每个文件的权限。如果您能帮助我弄清楚这做错了什么,那将是非常有用的。我

#!/bin/bash
while read line
do

        if [-z  "$(grep "/home" | cut -d: -f5 | grep -i "$RECHERCHE")" ]
              then
        user=$(cut -f: -f1)
        file=$(find / -user $(user))
        if [$(stat -c %a file) >= 700]
                then
        chmod 700 file 2>> /home/$(user)/challenge.log
        fi
        if [$(stat -c %a file) < 600]
                then
        chmod 600 file 2>> /home/$(user)/challenge.log
        fi
        umask 177 2>> /home/$(user)/challenge.log
        fi
done

我必须知道我在做什么。

【问题讨论】:

  • /etc/passwd 不是命令。删除 $()。使用shellcheck.net 作为您的第二个脚本。
  • 在第二个 sn-p 中,[] 周围缺少空格。
  • echo $(...) 通常没用,你可以不用echo 和包装$()
  • @cyrus 感谢您的链接

标签: linux bash debian


【解决方案1】:

$(...) 语法表示命令替换,即:将替换为括号内命令的输出。

因为/etc/passwd 不是命令而只是一个文本文件,所以你不能执行它。

所以如果你想将/etc/passwd 的内容传递给你的脚本,你只需调用它:

./challenge2 < /etc/passwd

或者,如果您需要特殊权限来读取文件,例如

sudo cat /etc/passwd | ./challenge2

同样在您的challenge2 脚​​本中,您使用的是$(user),这是错误的,因为您真的只想扩展user 变量:为此使用花括号,例如${user}

/etc/passwd?

不是你要问的,但无论如何你可能不应该直接阅读/etc/passwd。 如果要获取用户列表,请使用以下命令

$ getent passwd

这可能会为您提供比/etc/passwd 中存储的用户更多的用户,因为您的系统可能使用其他 PAM 后端(ldap、...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2017-01-10
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多