【问题标题】:Search for user files in relative and absolute path在相对和绝对路径中搜索用户文件
【发布时间】:2011-05-06 18:27:22
【问题描述】:

我有在绝对路径中搜索用户文件的脚本,但现在我还需要添加相对路径。我该怎么做?

#!/usr/local/bin/bash
if [ $# -eq 2 ]; then
        DIR=$1
        USERNAME=$2
        while [ ! $(id -g $USERNAME) ]; do
                echo "non-existing username, please add again "
                read USERNAME
        done
        while [ ! -d "/home/$DIR" ]; do

                echo "non-existing '$DIR' directory, please add again "
                read DIR
        done
        echo "variable username is now $USERNAME"
        echo "variable DIR is now $DIR"
        echo -e "username group   file"
        ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${USERNAME} '$3==user{print}{}' | tr -s ' ' | cut -d ' ' -f3 -f4 -f9
else
        echo "bla"
fi

【问题讨论】:

  • 您没有为此使用find 的任何特殊原因?
  • 对于您的相对路径,您希望它们相对于什么,当前工作目录或其他什么?
  • 如果我输入:目录用户名,它应该作为相对,如果我输入:/dir/dir1 用户名,它应该作为绝对用户。
  • 请不要解析lsHere's why。至于你的问题,这里有一个提示:如果目录不以斜杠开头,它是相对的。

标签: bash search shell directory


【解决方案1】:

我不需要该死的绝对和相对路径与该死的 ls :/ 所以

while [ ! -d "$DIR" ]; do

ls -1Apl $DIR | grep -v /\$

它正在工作

【讨论】:

    猜你喜欢
    • 2010-09-15
    • 2013-07-14
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2014-02-06
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多