【问题标题】:Bash script returns different results for different usersBash 脚本为不同的用户返回不同的结果
【发布时间】:2016-07-13 01:11:19
【问题描述】:

我正在尝试检查特定文件是否打开。我为此使用 lsof 并且我正在使用 bash shell。我写了一个 bash 脚本来检查文件是否打开。但是,我面临一个奇怪的问题。当我使用user a 从命令行运行脚本时,我得到了预期的结果。但是,当我使用user b 运行脚本时,我得到了不同的结果。

我需要将 Talend Studio 中的脚本调用为 user b,因此我无法切换到 user a 并执行该脚本。该脚本拥有user b 的777 权限,而user b 也是该脚本的所有者。下面是脚本。请帮忙。

#! /bin/bash
# declare STRING variable

FILENAME=$1
if [ "$FILENAME" == "" ]; then
echo "No file found..exiting"
exit
fi
#While loop to check if the file has been completely copied
while true; do
#$(lsof /mnt/disk1/$FILENAME | wc -l)
SZ=$(/usr/sbin/lsof <Path to file>/$FILENAME | wc -l)
if [ $SZ -gt 1 ]; then
echo "File $FILENAME  is being copied in the Landing Zone"
sleep 10
else
echo "Copied $FILENAME................"
break
fi
done

我需要在 sudoers 中添加 user b 吗?

【问题讨论】:

  • @Bohemian / @ fedorqui 请帮忙解决上述问题
  • 预期结果是什么,作为用户 b 得到的结果是什么?
  • @chepner 在以“用户 a”身份运行时,我看到“正在复制的文件”作为输出,直到文件完成复制然后打印“已复制 ”。当我以“用户 b”身份运行它时,我得到“已复制 ”,甚至当文件仍在被复制时
  • userb对文件登陆的目录和文件有权限吗
  • @garpitmzn 是的,它确实有权限

标签: linux bash shell file-handling talend


【解决方案1】:

您使用“lsof”的目的只是为了验证 $FILENAME 是否被打开,对吗? 所以我认为更好的方法是在打开 $FILENAME 时创建一个标志文件,但不要使用“lsof”来验证,因为“lsof”是一种昂贵的方法。 例如:

在你的“着陆区”:

#!/bin/bash

FILENAME=/foo/foo1
if [ -f /tmp/filename.flag ] ; then
    echo "the file is opened now ..."
else
    touch /tmp/filename.flag
    cp -arf $FILENAME /tmp/. # or use rsync to copy it
    echo "file copy is done ..."
    rm -rf /tmp/filename.flag
fi

而且,有时您只需“触摸/tmp/filename.flag”即可停止此任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多