简体   繁体   English

如果 for 命令的每个循环都成功匹配,则更改变量的 output

[英]Change a variable's output if a match is successful with each loop of for command

Startup action:

I am trying to make a function that I will call with.bash_functions which is sourced in my.bashrc file.我正在尝试制作一个 function,我将调用 with.bash_functions,它来自 my.bashrc 文件。

Goal:

I am trying to reduce the amount of code required.我正在尝试减少所需的代码量。 If I make a function with repeated commands that have (mostly minor) differences for each command line I end up with a HUGE function...如果我用重复的命令创建一个 function,每个命令行都有(主要是微小的)差异,我最终会得到一个巨大的 function...

Purpose of function:

The function will search the current working directory for files that match an array of predefined extensions. function 将在当前工作目录中搜索与一组预定义扩展名匹配的文件。 If a file with a matching extension is found it will execute a certain customized command line.如果找到具有匹配扩展名的文件,它将执行特定的自定义命令行。 If another match is found with a different extension then another type of command must be used instead of the first command that was mentioned and so forth.如果找到另一个具有不同扩展名的匹配项,则必须使用另一种类型的命令而不是提到的第一个命令等等。

If the following files are in the current working directory:

file1.7z
file2.bz2
file3.gz
file4.tgz
file5.xz

Then running the function in a terminal will output the following lines:

7z x -o/root/file1 /root/file1.7z
tar -xvfj /root/file2.bz2 -C /root/file2
tar -xvf /root/file3.gz -C /root/file3
tar -xvf /root/file4.tgz -C /root/file4
tar -xvf /root/file5.xz -C /root/file5

Where I am at so far:

I don't yet have the array set up because I am stuck figuring out the flow to loop the commands correctly.我还没有设置数组,因为我无法确定正确循环命令的流程。 I have the below script which does what I want however like I mentioned I want to slim it down if possible (learn some new tricks from you guys)!我有下面的脚本,它可以做我想做的事情,但是就像我提到的那样,我想尽可能地精简它(向你们学习一些新技巧)!

untar()
{
    clear
    for i in *.*
    do
            local EXT="$(echo "${i}" | sed 's/.*\.//')"
            if [ -n "${EXT}" ]; then
            if [[ "${EXT}" == '7z' ]]; then
                if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi
                echo 7z x -o"${PWD}"/"${i%%.*}" "${PWD}"/"${i}"
            elif [[ "${EXT}" == 'bz2' ]]; then
                if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi
                echo tar -xvfj "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
            elif [[ "${EXT}" == 'gz' ]]; then
                if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi
                echo tar -xvf "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
            elif [[ "${EXT}" == 'tgz' ]]; then
                if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi
                echo tar -xvf "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
            elif [[ "${EXT}" == 'xz' ]]; then
                if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi
                echo tar -xvf "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
            fi
        fi
    done;
}

Required to function:

Essentially, two different tar commands and a single 7z command are needed (as far as i can come up with anyways)本质上,需要两个不同的 tar 命令和一个 7z 命令(据我所知)

Command 1: tar -xvf <target.{gz,tgz,xz}> -C <output folder> 
Command 2: tar -xvfj <target.bz2> -C <output folder> 
Command 3: 7z x -o<output folder> <target.7z>

Any ideas you can throw my way?你有什么想法可以告诉我吗?

Since you're familiar with parameter substitution I'd start with eliminating the overhead of the dual subprocess invocations for finding EXT ;由于您熟悉参数替换,因此我将从消除用于查找EXT的双子进程调用的开销开始; shorter code but also a performance improvement as this doesn't require spawning/cleaning-up any subprocesses (for each pass through the loop):更短的代码,但也提高了性能,因为这不需要生成/清理任何子进程(对于每次通过循环):

local EXT="${i##*.}"

NOTE: this assumes all files have a single extension (eg, .tgz instead of tar.gz ) otherwise OP may need to add more logic to determine how to process files with multiple extensions (eg, abc.tar.gz vs abc.tar.gz )注意:这假设所有文件都有一个扩展名(例如, .tgz而不是tar.gz ),否则 OP 可能需要添加更多逻辑来确定如何处理具有多个扩展名的文件(例如, abc.tar.gzabc.tar.gz )

Next idea would be to pull the mkdir logic out by itself;下一个想法是自己拉出mkdir逻辑; this eliminates 4 mkdir lines:这消除了 4 mkdir行:

if [ ! -d "${PWD}"/"${i%%.*}" ]; then mkdir -p "${PWD}"/"${i%%.*}"; fi

# or

[[ ! -d "${PWD}"/"${i%%.*}" ]] && mkdir -p "${PWD}"/"${i%%.*}"

Small detour...小弯路...

OP has mentioned wanting to use an array to manage a list of extensions and while such an approach is doable it would also require some thought on how to store and reference the associated commands. OP 提到想要使用数组来管理扩展列表,虽然这种方法是可行的,但还需要考虑如何存储和引用相关命令。

Assuming this will be the only piece of code that needs to process a list of extensions I'd probably opt for 'hardcoding' the logic for each extension (as opposed to storing in a resource/config file and then loading into an array).假设这将是唯一需要处理扩展列表的代码,我可能会选择“硬编码”每个扩展的逻辑(而不是存储在资源/配置文件中,然后加载到数组中)。 Net result, stick with current approach but with a few improvements.最终结果,坚持当前的方法,但有一些改进。

Back to code (re)design...返回代码(重新)设计...


Next idea would be to collapse the tar calls into a single call with a test for bz2 ;下一个想法是将tar调用折叠为单个调用,并测试bz2 this eliminates 3 tests and 3 tar lines:这消除了 3 个测试和 3 个tar行:

if [[ "${EXT}" == '7z' ]]; then
     echo 7z x -o"${PWD}"/"${i%%.*}" "${PWD}"/"${i}"
else
     jflag=""
     [[ "${EXT}" == 'bz2' ]] && jflag="j"
     echo tar -xvf${jflag} "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
fi

Personally, I'd probably opt for a case statement:就个人而言,我可能会选择case陈述:

case "${EXT}" in
    7z)
         echo 7z x -o"${PWD}"/"${i%%.*}" "${PWD}"/"${i}"
         ;;
    *)
         jflag=""
         [[ "${EXT}" == 'bz2' ]] && jflag="j"
         echo tar -xvf${jflag} "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
         ;;
esac

Pulling this all together:把这一切放在一起:

untar()
{
    clear
    local EXT

    for i in *.*
    do
        EXT="${i##*.}"

        [[ ! -d "${PWD}"/"${i%%.*}" ]] && mkdir -p "${PWD}"/"${i%%.*}"

        case "${EXT}" in
            7z)
                 echo 7z x -o"${PWD}"/"${i%%.*}" "${PWD}"/"${i}"
                 ;;
            *)
                 jflag=""
                 [[ "${EXT}" == 'bz2' ]] && jflag="j"
                 echo tar -xvf${jflag} "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
                 ;;
        esac
    done
}

Posting this as another example for others to see that I came up with after seeing the great example by markp-fuso.将此作为另一个示例发布,让其他人看到我在看到 markp-fuso 的优秀示例后想出的。

I added zip files to the list this time.这次我在列表中添加了 zip 个文件。

untar()
{
    clear
    local EXT

    for i in *.*
    do
        EXT="${i##*.}"

        [[ ! -d "${PWD}"/"${i%%.*}" ]] && mkdir -p "${PWD}"/"${i%%.*}"

        case "${EXT}" in
            7z|zip)
                7z x -o"${PWD}"/"${i%%.*}" "${PWD}"/"${i}"
                 ;;
            bz2|gz|xz)
                jflag=""
                [[ "${EXT}" == 'bz2' ]] && jflag="j"
                tar -xvf${jflag} "${PWD}"/"${i}" -C "${PWD}"/"${i%%.*}"
                 ;;
        esac
    done
}

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

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