美文网首页
2020-08-05 输出文件或目录的绝对路径脚本abspath

2020-08-05 输出文件或目录的绝对路径脚本abspath

作者: YPCHEN1992 | 来源:发表于2020-04-01 13:08 被阅读0次

脚本:abspath
Python版本

#!/usr/bin/env python3
import os
import sys
for entry in sys.argv[1:]:
     print(os.path.abspath(entry))

使用

abspath fastq.gz
# output
 /data/fasta/fastq.gz

bash 版本

#!/usr/bin/env bash
function usage() {
    echo "NAME:"
    echo "    print the absolte path of target File(s) or Folder(s)."
    echo "SYNOPSIS:"
    echo "    abspath <target> ..."
    echo "AUTHOR:"
    echo "    YP CHEN, 2020-08-05"
    exit 1
}

if [[ '--help' =~ $1 ]]; then
    if [[ $# -ne 0 ]]; then
        usage
    fi
fi

# If no argument, the current working directory will be print.
if [[ $# -eq 0 ]]; then
    pwd
else
    # Multiple arguments
    for fd in $@
    do
        if [[ -f $fd ]]; then
            # If relative path of a file was supplied.
            echo $fd | grep '/' > /dev/null
            if [[  $? -eq 0 ]]; then
                cd $(dirname $fd) && echo $PWD/$(basename $fd) && cd - > /dev/null
            else
                echo $PWD/$fd
            fi
        elif [[ -d $fd ]]; then
            # If realative path of a folder was supplied.
            echo $fd | grep '/' > /dev/null
            if [[ $? -eq 0 ]]; then
                cd $fd && echo $PWD && cd - > /dev/null
            else
                cd $fd && echo $PWD && cd - > /dev/null
            fi
        else
            # Unknown arguments
            echo "[ERROR] Invalid input: $fd"
        fi
    done
fi

相关文章

网友评论

      本文标题:2020-08-05 输出文件或目录的绝对路径脚本abspath

      本文链接:https://www.haomeiwen.com/subject/vswouhtx.html