首页 \ 问答 \ 所有文件在一个dir,linux中(All files in one dir, linux)

所有文件在一个dir,linux中(All files in one dir, linux)

今天,我在Linux中尝试了一个脚本来获取一个目录中的所有文件。 这很简单,但我发现了一些有趣的东西。

 #!/bin/bash
InputDir=/home/XXX/
for file in $InputDir'*'
do
  echo $file
done

输出是:

/home/XXX/fileA /home/XXX/fileB 

但是当我直接输入dir时,如:

 #!/bin/bash
InputDir=/home/XXX/
for file in /home/XXX/*
do
  echo $file
done

输出是:

/home/XXX/fileA
/home/XXX/fileB

看起来,在第一个脚本中,只有一个循环,所有文件名都存储在FIRST循环中的变量$ file中,并由空格分隔。 但是在第二个脚本中,只有一个循环中有一个文件名存储在$ file中,并且有多个循环。 这两个脚本之间的区别究竟是什么?

非常感谢,也许我的问题有点天真..


Today I tried a script in linux to get all files in one dir. It was pretty straightforward, but I found something interesting.

 #!/bin/bash
InputDir=/home/XXX/
for file in $InputDir'*'
do
  echo $file
done

The output is:

/home/XXX/fileA /home/XXX/fileB 

But when I just input the dir directly, like:

 #!/bin/bash
InputDir=/home/XXX/
for file in /home/XXX/*
do
  echo $file
done

The output is:

/home/XXX/fileA
/home/XXX/fileB

It seems, in the first script, there was only one loop and all the file names were stored in the variable $file in the FIRST loop, separated by space. But in the second script, one file name was stored in $file just in one loop, and there were more than one loop. What is exactly the difference between these two scripts?

Thanks very much, maybe my question is a little bit naive..


原文:https://stackoverflow.com/questions/16082205
更新时间:2021-12-05 14:12

最满意答案

这让人联想起经典的友谊模特问题。 多态联想不仅仅是重点。

Rails版本不可知解决方案:

class User < ActiveRecord::Base
  has_many :instigator_actions, :class_name => "Action", :as => :instigator
  has_many :victim_actions, :class_name => "Action", :as => :victim
  has_many :actions, :finder_sql => '
       SELECT a.* 
       FROM   actions a
       WHERE  (a.instigator_type = "User" AND instigator_id = #{id}) OR 
              (a.victim_type     = "User" AND victim_id     = #{id})'
end

创建Actions时,使用前两个关联之一创建它们。

u1.instigator_actions.create(:victim => u2)

要么

u1.victim_actions.create(:instigator => u2)

同时,您可以使用操作关联获取与用户关联的操作列表。

u1.actions

This reminds of classic Friendship model problem. Polymorphic association is besides the point.

Rails version agnostic solution:

class User < ActiveRecord::Base
  has_many :instigator_actions, :class_name => "Action", :as => :instigator
  has_many :victim_actions, :class_name => "Action", :as => :victim
  has_many :actions, :finder_sql => '
       SELECT a.* 
       FROM   actions a
       WHERE  (a.instigator_type = "User" AND instigator_id = #{id}) OR 
              (a.victim_type     = "User" AND victim_id     = #{id})'
end

While creating the Actions create them using one of the first two associations.

u1.instigator_actions.create(:victim => u2)

OR

u1.victim_actions.create(:instigator => u2)

At the same time you can get a list of actions associated with an user using the actions association.

u1.actions

相关问答

更多
  • 这是有争议的,但传统的“Rails方式”是管理与模型相关的东西,如默认值,外键和触发器是在ActiveRecord级别而不是在数据库中。 也就是说,您可以使用以下内容在迁移中为参照完整性添加外键: add_foreign_key :orders, :users Rails指南中提供了更多信息。 在销毁父对象时自动销毁子对象的'Rails方法'是在子集合上指定:dependent strategy。 这里有一个很棒的Stackoverflow讨论详细介绍了两个:dependent选项:destroy vs ...
  • 在第二种情况下,您需要在图片表中添加两个外键,即employee_id和product_id。 在第一种情况下t.references :imageable, polymorphic: true在您的图片迁移中将在图片表中添加两个字段,即 t.integer :imageable_id t.string :imageable_type imagable_type字段将是与此模型关联的类的名称,而imagable_id将保存该记录的id。 例如, 图片表的典型行看起来像 id | name | imaga ...
  • 要回答你的确切问题,答案是“有点是,有点不”。 内部设置关联的rails非常复杂。 是的,涉及getter / setter方法,是的,指向涉及表行的......但这并不是Active Record所做的,而且它不仅仅是Active Record所做的。 如果你真的想知道Active Record里面的内容:你可以去看看github上的源代码 。 如果您有更具体的问题...我建议您更新您的问题,要求:) To answer your exact question, the answer is "kinda ...
  • 这让人联想起经典的友谊模特问题。 多态联想不仅仅是重点。 Rails版本不可知解决方案: class User < ActiveRecord::Base has_many :instigator_actions, :class_name => "Action", :as => :instigator has_many :victim_actions, :class_name => "Action", :as => :victim has_many :actions, :finder_sql => ...
  • 您设置的方式,将用户与另一个用户关联,而不是将用户关联到PhotoAlbum,这就是您所描述的内容。 听起来好像你想要的 class Relationship < ActiveRecord::Base belongs_to :follower, :foreign_key => "follower_id", :class_name => "User" belongs_to :followed, :foreign_key => "photo_album_id", :class_name => "Phot ...
  • 瑞恩有一个关于这个很好的railscast 。 Belongs_to不是您添加到迁移中的东西,而是将其添加到模型中。 在迁移中,您必须添加外键列。 例如,如果您有一个属于用户的发布模型,您可以在迁移过程中将user_id列添加到发布主动记录中。 然后你添加 belongs_to :user 在后期模型中分开。 然后,rails将在后台发挥它的魔力,为您提供用户模型中的代理集合。 Ryan has a railscast about this that is pretty good. Belongs_to ...
  • 将集合传递到渲染与每个说法的关联几乎没有关系 - 您应该参考“ 布局和渲染”指南 。 当您使用集合(模型实例数组)调用render时,Rails会创建一个循环并使用模型名称和约定来为集合的每个成员查找正确的部分。 Rails并不关心该集合来自一个关联。 添加: 由于List和Item之间的关系是双向的,因此您始终可以通过item.list访问项目所属的item.list : # views/items/_item.html.erb
  • 您的问题的答案: 如果您的业务登录意味着多个模型将具有相同字段的相关模型,那么您应该使用多态关联。 (在你的情况下,你可以使用它)。 如果设置多态关联,Rails将根据关联的父模型自动处理设置*_id和*_type字段。 假设您在多态关联中有许多订单的产品,并且您想要定义哪个模型订单属于: order = Order.first order.orderable Answers to your questions: If your business login implies that multiple m ...
  • 1)如果将变量传递给partial,它将作为本地,而不是实例: <%= render :partial => 'comments/display', locals: {commentable: @article} %> 2)另请注意,从控制器渲染和从视图渲染是不同的事情。 在控制器中,您不必指定:partial=>因为Rails中的控制器应该呈现整个模板,但是如果您在视图文件中,则应该像Rails一样只能为每个请求呈现一个模板 1) If you pass variable to partial, it ...
  • Rails指南提供了解决您遇到问题的原因。 实质上,当您声明belongs_to关系时,外键会出现在声明它的类的表中。 声明has_one关系时,外键位于声明中类的表中。 例 在这种情况下, pictures表将需要page_id外键。 class Page < ActiveRecord::Base has_one :picture end class Picture < ActiveRecord::Base belongs_to :page end 在这种情况下, pages表将需要pict ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)