简体   繁体   English

在bash中循环浏览一系列文件名

[英]looping through a series filenames in bash

I am trying to process some files in a loop. 我正在尝试循环处理一些文件。 This version is kind of hard coded for testing but my problem is to get the full filename with variables and it looks good to me but the script hangs so obviously I did something silly. 这个版本经过硬编码测试,但是我的问题是获取带有变量的完整文件名,对我来说看起来不错,但是脚本挂起了,显然我做了一些愚蠢的事情。 Can anyone help? 有人可以帮忙吗? Here is my script 这是我的剧本

File_numbers={1003,1004}
File_name=$Domain + "__" + $Domain + "_" + $i "__" + * +  ".ext"
Full_path=$Path + "/" + $Domain + "/" + $File_name

for i in $File_numbers; 
do echo $i; cat $Full_path; 
done

If someone has a more elegant solution that would be fine as well. 如果有人有一个更优雅的解决方案,那也很好。

Update: I have changed my script as such (based on the issues pointed out by users) 更新:我已经更改了自己的脚本(基于用户指出的问题)

#!/bin/bash

Path="aPath";
Domain="aDomain";
File_numbers="{1003,1004}"
Full_path=$Path"/"$Domain"/"

for i in $File_numbers; do find $Full_path -name "*"$i"*" | xargs cat; done

it seems that the variable is not recognized in this case 在这种情况下似乎无法识别该变量

I would do (fixing a bunch of minor issues): 我会做的(解决了一些小问题):

#!/bin/bash
Path="aPath"
Domain="aDomain"
File_numbers="[1003|1004]"
Full_path="$Path/$Domain/"

find $Full_path -name "*$File_numbers*" | xargs cat

So what I changed: 所以我改变了:

  • no ; 不; in bash ! 扑朔迷离!
  • no need to loop on File_numbers. 无需循环File_numbers。 [1003|1004] means anything including 1003, or 1004. [1003 | 1004]表示包括1003或1004在内的任何内容。
  • notice how -name " $File_numbers " contains everything within the "" 请注意-name“ $ File_numbers ”如何包含“”中的所有内容
  • Full_path: variables are usable within double quotes ("). Full_path:变量在双引号(“)中可用。

Finally, I strongly suggest you do a quick google search and find a beginners guide to bash, it will save you a LOT of time! 最后,我强烈建议您进行快速的Google搜索,并找到针对bash的初学者指南,它将为您节省大量时间! Invest now or waist time later! 立即投资,或稍后再投资!

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

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