小编Ale*_*hin的帖子

用实际名称替换 ./ 和 ../ 目录

我有一个脚本,它在用户指定的目录中查找文件。

#!/bin/bash

# make sure about the correct input
if [ -z $1 ]
then
    echo "Usage: ./script_name.sh    path/to/directory"
else
    DIR=$1
    if [ $DIR = '.' ]
    then 
        echo "Find files in the directory $PWD"
    else  
        echo "Find files in the directory $DIR"
    fi

    find $DIR -type f -exec basename {} \; 
fi
Run Code Online (Sandbox Code Playgroud)

如果我输入

$ ./script_name.sh .
Run Code Online (Sandbox Code Playgroud)

脚本给了我正确的替换 ./ 到 $PWD 并显示(例如)

$ Find files in the directory /root/scripts  
Run Code Online (Sandbox Code Playgroud)

但是我无法决定如何将 ../ 替换 为层次结构中紧邻上方的目录的名称。如果我输入

$ ./script_name.sh ..  
Run Code Online (Sandbox Code Playgroud)

脚本给了我输出 …

shell-script

6
推荐指数
1
解决办法
797
查看次数

标签 统计

shell-script ×1