简体   繁体   English

Bash CD到目录

[英]Bash CD to directory

I am trying to change the extraction_sudir to a completely unrelated path to the file location (/home/user/downloads/tcomplete/foldername/file.mp4 我正在尝试将extract_sudir更改为文件位置的完全不相关的路径(/home/user/downloads/tcomplete/foldername/file.mp4

#!/bin/bash
formats=(zip rar)
commands=([zip]="unzip -u" [rar]="unrar -o- e")
extraction_subdir='extracted'

downloadid=$1
downloadname=$2
downloadpath=$3

log()
{
    logger -t deluge-extractarchives "$@"
}

log "Download complete: $@"
cd "${downloadpath}"
for format in "${formats[@]}"; do
    while read file; do 
        log "Extracting \"$file\""
        cd "$(dirname "$file")"
        file=$(basename "$file")
        # if extraction_subdir is not empty, extract to subdirectory
        if [[ ! -z "$extraction_subdir" ]] ; then
            mkdir "$extraction_subdir"
            cd "$extraction_subdir"
            file="../$file"
        fi
        ${commands[$format]} "$file"
    done < <(find "$downloadpath/$downloadname" -iname "*.${format}" )
done

I thought it would be as simple as: 我认为这很简单:

extraction_subdir='/home/user/extracted'

However that seems to just place the unrar'd file in the directory: /home/user/downloads/tcomplete/foldername/ 但是,这似乎只是将未压缩的文件放在目录中:/ home / user / downloads / tcomplete / foldername /

I am looking at: 我在看:

file=$(basename "$file")
...
file="../$file"

Being the cultprit but unsure how to solve this. 作为罪魁祸首,但不确定如何解决。

Edit: 编辑:

Solved this my specifying the output in the unrar command eg: 解决了我在unrar命令中指定输出的问题,例如:

${commands[$format]} "$file" "$extractedpath"

became: 成为:

${commands[$format]} "$file" "/home/user/extract"

You were correct in your analysis. 您的分析是正确的。 The issue is that the script assumes that extraction_subdir is always within downloadpath . 问题是该脚本假定extraction_subdir始终在downloadpath

I can't personally test it, but I believe the following will fix the issue: 我无法亲自对其进行测试,但是我相信可以解决以下问题:

file="${downloadpath}/${file}"

It will force the file path to be absolute instead of relative. 这将强制文件路径是绝对路径,而不是相对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM