如何使用变量作为查找命令的路径 [英] How to use variable as a path for find command

查看:228
本文介绍了如何使用变量作为查找命令的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以有人给我一个提示,我可以如何使下面的bash脚本正常运行:

  path =/ path /
excludepath =!-path \/ path / to / exclude / * \

找到$ path$ excludepath-name *。txt -type f

当我尝试运行它时,我得到:

  find:`! -path/ path / to / exclude / *':没有这样的文件或目录

谢谢您的问题是事实上,$ excludepath >是一个参数,即使它包含空格。要放置几个参数,请使用字符串数组而不是字符串:

  excludepath =(!-path* webshow * )
找到$ path$ {excludepath [@]}-name* .txt-type f

这是一个bashism,但是(绊倒这个的人不要使用 bash 将需要做其他事情)


Can someone give me a hint, how I can make the following bash script run properly:

path="/path/"
excludepath="! -path \"/path/to/exclude/*\" "

find "$path" "$excludepath" -name *."txt" -type f

When I try to run it I get:

find: `! -path "/path/to/exclude/*"': No such file or directory

Thank you in advance.

解决方案

Your problem was the fact that "$excludepath" is a single argument, even if it contains spaces. To put several arguments, use an array of strings instead of a string:

excludepath=(! -path "*webshow*")
find "$path" "${excludepath[@]}" -name "*.txt" -type f

This is a bashism, though (people that stumble onto this and don't use bash will need to do something else).

这篇关于如何使用变量作为查找命令的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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