首页 \ 问答 \ 如何将Bash命令的输出分配给变量?(How to assign the output of a Bash command to a variable? [duplicate])

如何将Bash命令的输出分配给变量?(How to assign the output of a Bash command to a variable? [duplicate])

这个问题已经在这里有一个答案:

  • 如何从Bash中的命令设置一个变量到输出? 12答案

pwd命令的内容放入我将在稍后使用的shell变量中。

这是我的shell代码(循环不停止):

#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
    do
        echo $pwd
        ls && cd .. && ls 
        $pwd= `pwd` 
    done

你能发现我的错吗?


This question already has an answer here:

I have a problem putting the content of pwd command into a shell variable that I'll use later.

Here is my shell code (the loop doesn't stop):

#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
    do
        echo $pwd
        ls && cd .. && ls 
        $pwd= `pwd` 
    done

Could you spot my mistake, please?


原文:https://stackoverflow.com/questions/2314750
更新时间:2022-09-27 11:09

最满意答案

我喜欢返回一个新的集合实例。 这使得这些过滤方法可以链接(例如boxes.byColor("red").bySize("L") )。

var Boxes = Backbone.Collection.extend({
    model: Box,

    byColor: function (color) {
        filtered = this.filter(function (box) {
            return box.get("color") === color;
        });
        return new Boxes(filtered);
    }
});

var red_boxes = boxes.byColor("red")

I like returning a new instance of the collection. This makes these filtering methods chainable (boxes.byColor("red").bySize("L"), for example).

var Boxes = Backbone.Collection.extend({
    model: Box,

    byColor: function (color) {
        filtered = this.filter(function (box) {
            return box.get("color") === color;
        });
        return new Boxes(filtered);
    }
});

var red_boxes = boxes.byColor("red")

相关问答

更多
  • 回答你的问题: 可以扩展视图或集合以具有触发更改事件的属性,答案是否定的,是的。 不,你不能做view.set({attr: value})或collection.set({attr: value}) 但是,您可以通过扩展Backbone.Events来获取视图或集合触发事件。 YourView = Backbone.View.extend({}); _.extend(YourView, Backbone.Events); var view = new YourView(); view.bind('Sel ...
  • 我喜欢返回一个新的集合实例。 这使得这些过滤方法可以链接(例如boxes.byColor("red").bySize("L") )。 var Boxes = Backbone.Collection.extend({ model: Box, byColor: function (color) { filtered = this.filter(function (box) { return box.get("color") === color; ...
  • 您可以通过将集合中的Playlist转换为模型来解决问题。 如果您考虑这个问题, Playlist可能会有其他属性(例如名称),不能在集合中设置。 Playlists将是Playlist模型(而不是集合)的集合,它应该无错误地工作。 var Track = Backbone.Model.extend({ //trackdata }); var Playlist = Backbone.Model.extend({ model : Track }); var Playlists = Back ...
  • 我总是喜欢做的一件事是避免这样的问题,即创建“列表视图”和“项目视图”,并在列表视图上呈现多个项目视图。 这允许我有两个单独的事件哈希:一个在项目视图级别,一个在列表视图级别。 这样做也很明显Backbone事件处理应该去哪里(项目视图如果我们专注于一个模型,列表视图,如果我们专注于集合)。 还要记住,Backbone集合将所有模型事件代理到它们自己的事件触发器,因此可以进行collection.on("change:someModelAttr") 。 所有这些回调都将获得受影响的模型。 The probl ...
  • 你也可以使用Underscore some (aka any ),它几乎与你的search函数相同,除了它使用函数参数作为它的谓词,而不是键/值: 如果列表中的任何值通过迭代器真相测试,则返回true。 如果找到了真实元素,则短路并停止遍历列表。 这个实现使用更直接一些: search: function (attribute, value) { return this.some(function(x) { return x.get(attribute) === value; ...
  • 在Backbone中没有一种真正的方法来处理嵌套集合。 引用Backbone常见问题解答 : Backbone不包括对嵌套模型和集合的直接支持,或者“有很多”关联,因为在客户端有许多用于建模结构化数据的良好模式,Backbone应该为实现其中任何一个提供基础。 FAQ还提供了一种模式,并提供了Extensions,Plugins,Resources页面的链接,该页面链接到许多可用于处理嵌套模型和模型关系的库。 也就是说,我有时会遇到这样的问题: var Author = Backbone.Model.ext ...
  • 您的服务器似乎正在返回嵌套在JSON响应中的tasks Array。 为了让Backbone知道如何正确解析JSON,您需要覆盖parse()方法并告诉它使用tasks数组作为模型的源。 var MyModel = Backbone.Model.extend({}); var MyCollection = Backbone.Collection.extend({ model: MyModel, parse: function(response) { ...
  • 我想说最好的办法就是使用同一个集合的2个实例。 其中包含所有模型的模型,以及其中包含可见模型的模型。 您可以将视图绑定到“可见”集合,当您单击加载更多时,只需将模型从完整集合复制到可见集合。 我不会为此编写代码,因为您的StartView需要一些重构,我会尝试使用您的视图执行以下操作: 从视图中删除你的collection.fetch和render调用,并将它们移动到一个控制你的应用程序的对象,设置你的视图等。这样的视图是一个非常愚蠢的对象,它知道它的DOM元素,它需要的集合渲染,这就是它。 不要使用fet ...
  • 您可以筛选出包含在数组中的状态的所有模型,然后获取此结果数组的长度: var numOfModels = collection.filter(function(model) { return array.indexOf(model.get('status')) !== -1; }).length; You could filter out all the models which has a status contained in the array, and then take the lengt ...
  • 嗨,这里是可以过滤多种类型的代码,存储在数组中 var typesTofilter=[5,1,2];//array of type tobe search. var f2=friends.filter(function(o){ var accept=false; $(typesTofilter).each(function(i,val){ ...

相关文章

更多

最新问答

更多
  • 您如何使用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)