bash脚本-忽略脚本参数中的空格 [英] bash script - ignoring whitespaces in script parameters

查看:54
本文介绍了bash脚本-忽略脚本参数中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对bash脚本还很陌生,而且我在家庭作业脚本中用尽了所有的想法.

I'm quite new to bash scripting and I've ran out of ideas in my homework script.

该脚本带有4个参数-pathToDirectory,[-c/-m/-r] ==复制/移动/删除,ext1和ext2(运行脚本的示例:script.sh/home/user/somefolder -ca.txt b.sh).

The script takes 4 arguments - pathToDirectory, [-c/-m/-r] == copy/move/remove, ext1 and ext2 (example of running a script: script.sh /home/user/somefolder -c a.txt b.sh ).

该脚本应该在/home/user/someFolder(及其所有子文件夹)中找到所有名称中包含"a.txt"的文件,并(在-c和-m情况下)重命名该"a.txt"部分到'b.sh'并根据-c/-m参数创建一个新文件或仅重命名一个现有文件(在-r情况下,它只是删除该文件),然后在stdout中写类似'旧名称=>新名称".

The script should find all files in /home/user/someFolder (and its all subfolders) that contain 'a.txt' in their names and (in -c and -m case) rename that 'a.txt' part to 'b.sh' and depending on -c/-m argument either create a new file or just rename an existing file (in -r case it just removes the file) and then write in stdout something like 'old name => new name'.

上述脚本的示例输出:
/home/user/someFolder/bbb.txt =>/home/user/someFolder/bba.txt

example output of a script mentioned above:
/home/user/someFolder/bbb.txt => /home/user/someFolder/bba.txt

嗯,这并不是实现问题,直到我将代码发布到我们的上载系统(评估脚本)之前,一切都可以正常工作.

Well, that was not a problem to implement, everything worked until I posted my code to our upload system (evaluates our script).

第一个上载系统尝试运行我的脚本的过程看起来像是"script.sh/something/graph 1 -c .jpg .jpeg".

The very first Upload System's try to run my script looked like "script.sh /something/graph 1 -c .jpg .jpeg".

现在的问题是,整个'/something/graph 1'是一条路径,而'1'之前的空白会破坏所有路径.

The problem now is, that the whole '/something/graph 1' is a path and that whitespace before '1' ruins it all.

预期输出: ./projekty/graph 1.jpg =>./projekty/graph 1.jpeg
我的脚本输出: ./projekty/graph =>./projekty/graph.jpeg1.jpg =>1.jpeg

到目前为止我所拥有的:

What I have so far:

if [ "$2" = "-r" ]; then
    for file in  $(find $1 -name "*$3"); do
        echo $file
        rm -f $file
    done    

elif [ "$2" = "-c" ]; then
    for file in  $(find "$1" -name "*$3") ; do      
        cp "$file" "${file//$3/$4}"
        echo $file "=>" ${file%$3}$4            
    done

elif [ "$2" = "-m" ]; then
    for file in  $(find $1 -name "*$3"); do
        mv "$file" "${file//$3/$4}"
        echo $file "=>" ${file%$3}$4        
    done    

else    
    echo Unknown parameter >&2
fi

我尝试过的& notworking&可能是愚蠢的主意:由于-r/-c/-m参数应为$ 2,因此我能够检测到$ 2是其他东西(假设某些东西仍属于该路径)并附加$ 2变成$ 1,所以我有了一个可变的DIR,这就是整个路径.使用shift将所有参数移至左侧(由于空格,-r/-m/-c参数不在$ 2上,而是在$ 3上,所以我再次将其设为$ 2),然后代码如下:-c部分)

My tried&notworking&probablystupid idea: as the -r/-c/-m parameter should be at $2, I was able to detect that $2 is something else (assumpting something that still belongs to the path) and append that $2 thing to $1, so then I had a variable DIR which was the whole path. Using shift I moved all parameters to the left (because of the whitespace, the -r/-m/-c parameter was not on $2 but on $3, so I made it $2 again) and then the code looked like: (just the -c part)

DIR=$1
if [ "$2" != "-r" ] && [ "$2" != "-c" ] && [ "$2" != "-m" ]; then
     DIR+=" $2"
     shift      
fi

if [ "$2" = "-c" ]; then
    for file in  $(find "$DIR" -name "*$3") ; do        
        cp "$file" "${file//$3/$4}"
        echo $file "=>" ${file%$3}$4            
    done    
fi

当我回显"$ DIR"时,它显示了整个路径(正确),但是仍然无法正常工作.请问有其他/更好/任何方式可以解决此问题吗?:/

when i echoed "$DIR", it showed the whole path (correctly), but it still didn't work.. Is there any other/better/any way how to fix this please ? :/

提前谢谢!

推荐答案

  1. 由于仅需要在文件名的末尾替换目标字符串,因此"$ {file//$ 3/$ 4}" 是个坏主意.

示例: ./projekty/graph.jpg.jpg.jpg.graph.jpg

将易于引号扩展的字符串传递给循环也不是什么好主意.

Passing a string prone to unquoted expansion to a loop is a no better idea either.

事实是 find 可以按预期工作,并且其输出如下所示:

The fact is that find works as expected and its output looks like:

./projekty/graph 1.jpg

但是在循环中它不能正确扩展:

But inside a loop it is expanded incorrectly:

./projekty/graph
1.jpg

为避免这种情况,可以将 find 的输出保存到变量中,然后对其进行标记化,直到没有文本为止:

To avoid this, you can save the output of find to a variable and then tokenize it until no text is left:

list="$(find $1 -name "*$3")"

while [ -n "$list" ]; do
    file="${list%%$'\n'*}"
    list="${list#$file}"
    list="${list#$'\n'}"
    # your commands here
    # ...
done

这篇关于bash脚本-忽略脚本参数中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆