【发布时间】: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 用户名,它应该作为绝对用户。
-
请不要解析
ls。 Here's why。至于你的问题,这里有一个提示:如果目录不以斜杠开头,它是相对的。
标签: bash search shell directory