小编Den*_*nis的帖子

powershell:如何从[ref]变量写入主机值

我是Powershell的新手,我正在尝试研究如何在函数中打印[ref]变量的值.

这是我的测试代码:

function testref([ref]$obj1) {
  $obj1.value = $obj1.value + 5
  write-host "the new value is $obj1"
  $obj1 | get-member
}


$foo = 0
"foo starts with $foo"
testref([ref]$foo)
"foo ends with $foo"
Run Code Online (Sandbox Code Playgroud)

我从这个测试得到的输出如下.你会注意到我没有像我希望的那样得到$ obj1的值.我也试过在写入主机的调用中传入$ obj1.value但是生成了相同的响应.

PS > .\testref.ps1
foo starts with 0
the new value is System.Management.Automation.PSReference


   TypeName: System.Management.Automation.PSReference

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()
Value       Property   System.Object Value {get;set;}
foo ends with …
Run Code Online (Sandbox Code Playgroud)

powershell function ref

17
推荐指数
1
解决办法
5万
查看次数

Powershell陷阱[Exception]没有捕获我的错误

出于某种原因,当我针对不存在的文件运行以下脚本时,我的脚本没有捕获异常.我基于我在网络上找到的示例来创建此代码,但它似乎对我不起作用.

我将不胜感激如何解决这个问题的任何提示或指示.

注意:在下面的例子中我也试过了

trap [Exception] {
Run Code Online (Sandbox Code Playgroud)

但那也不起作用.

这是脚本:

function CheckFile($f) {

      trap {
        write-host "file not found, skipping".
        continue
      }

      $modtime = (Get-ItemProperty $f).LastWriteTime

      write-host "if file not found then shouldn't see this"
}


write-host "checking a file that does not exist"
CheckFile("C:\NotAFile")
write-host "done."
Run Code Online (Sandbox Code Playgroud)

输出:

PS > .\testexception.ps1
checking a file that does not exist
Get-ItemProperty : Cannot find path 'C:\NotAFile' because it does not exist.
At C:\Users\dleclair\Documents\Visual Studio 2010\lib\testexception.ps1:12 char:35
+       $modtime = (Get-ItemProperty <<<<  $f).LastWriteTime
    + CategoryInfo …
Run Code Online (Sandbox Code Playgroud)

powershell exception-handling exception powershell-trap

7
推荐指数
1
解决办法
1万
查看次数

从 Cygwin 用 C 语言运行 PostgreSQL 客户端

我正在尝试通过 Cygwin 用 C 语言构建一个非常简单的 PostgreSQL 客户端。

这是我到目前为止所做的:

  • 我已经下载了 PostgreSQL 源代码版本 9.1.2(以匹配在我的服务器上运行的相同版本)
  • 我已经从 Cygwin 配置并编译了源代码。编译似乎进行得很顺利。
  • 据我所知,头文件在:
    • /cygdrive/c/workspace/src/postgresql-9.1.2/src/interfaces/libpq,和
    • /cygdrive/c/workspace/src/postgresql-9.1.2/src/include
  • 图书馆位于:
    • /cygdrive/c/workspace/src/postgresql-9.1.2/src/interfaces/libpq

从这里开始,我使用下面的 makefile 编译并链接了客户端程序:

testlibpq: testlibpq.c
    gcc -o testlibpq -I /cygdrive/c/workspace/src/postgresql-9.1.2/src/interfaces/libpq -I /cygdrive/c/workspace/src/postgresql-9.1.2/src/include -L /cygdrive/c/workspace/src/postgresql-9.1.2/src/interfaces/libpq testlibpq.c -Bstatic -lpq
Run Code Online (Sandbox Code Playgroud)

编译和链接成功,没有错误或警告。

但是,当我尝试运行该程序时,出现以下错误:

$ ./testlibpq
/cygdrive/c/Users/dleclair/Dropbox/denis/src/testlibpq/testlibpq.exe: error while loading shared libraries: cygpq.dll: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我还没有想出如何解决这个问题。任何指针将不胜感激。哦,还有一件事,我找到了 cygpq.dll 所在的文件夹,并将我的 LD_LIBRARY_PATH 设置为指向它,但它仍然给了我相同的结果。

dleclair@dleclair-win7l ~/Dropbox/denis/src/testlibpq
$ ls /cygdrive/c/workspace/src/postgresql-9.1.2/src/interfaces/libpq
bcc32.mak      encnames.o    fe-connect.o  fe-misc.o       fe-protocol3.o   ip.o           libpq-events.c  md5.c                   pgstrcasecmp.c  pqsignal.c       thread.o
blibpqdll.def …
Run Code Online (Sandbox Code Playgroud)

postgresql cygwin libpq

5
推荐指数
1
解决办法
820
查看次数

ActiveRecord:将has_many列表视为一个简单数组

考虑一下这个简单的:has_many关系:

class Basket < ActiveRecord::Base
    has_many :apples
    ...
end

class Apple < ActiveRecord::Base
    belongs_to :basket
end
Run Code Online (Sandbox Code Playgroud)

现在,我在Basket类中有一个方法,其中我想创建'apples'数组的临时副本并操作临时副本.对于初学者,我想在临时副本中添加一个新元素,如下所示:

class Basket < ActiveRecord::Base
    has_many :apples

    def do_something
        #create a temporary working copy of the apples array
        temp_array = self.apples

        #create a new Apple object to insert in the temporary array
        temp_apple = Apple.new

        #add to my temporary array only
        temp_array << temp_apple

        #Problem! temp_apple.validate gets called but I don't want it to.
    end
end
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我发现当我尝试将它添加到临时数组时,会在临时Apple对象上调用validate例程.我创建临时数组的全部原因是为了避免主数组带来的所有行为,例如验证,数据库插入等......

也就是说,我确实找到了一种蛮力的方法来避免这个问题,在for循环中一次创建一个temp_array对象,如下所示.这有效,但很难看.我想知道是否有一种更优雅的方式来实现这一目标.

class Basket < ActiveRecord::Base
    has_many :apples …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails has-many

4
推荐指数
1
解决办法
3293
查看次数

COM 端口未出现在 Cygwin 中

我正在尝试编写一个脚本来自动通过串行连接将文件传输到远程设备。我的目的是在本地计算机上的 Cygwin 上运行的 Ruby 中编写脚本。

首先,我需要将串行通信连接到远程设备,为此,我假设我需要通过 /dev/tty 访问串行端口...

当我在 Cygwin shell 中查看 /dev/ 时,我只看到以下内容:

$ ls /dev
fd  mqueue  shm  stderr  stdin  stdout
Run Code Online (Sandbox Code Playgroud)

我没有看到像 tty 设备这样的东西。

当我在 Windows (Win7 Pro) 中检查设备管理器时,它显示了 3 个 com 端口。有一个本地通信端口 (COM1) 和两个虚拟通信端口 (COM5、COM6)。

现在我只是想获得任何类型的连接。

谢谢。

ruby cygwin serial-port

3
推荐指数
1
解决办法
2954
查看次数

Docker:具有 --volume 绑定挂载的文件权限

我正在遵循以下指南:https : //denibertovic.com/posts/handling-permissions-with-docker-volumes/ 在我的容器中设置 --volume 绑定安装并在来宾容器中创建一个用户作为我的主机用户的 UID - 理论是我的容器用户应该能够访问安装。它对我不起作用,我正在寻找一些指示以尝试下一步。

更多背景详情:

我的 Dockerfile 从一个 alpine 基地开始,并添加了 python 开发包。它按照来自 denibertovic 的指南在 entrypoint.sh 脚本中复制。然后跳转到 entrpoint.sh 脚本。

FROM alpine

RUN apk update
RUN apk add bash
RUN apk add python3
RUN apk add python3-dev
RUN apk add su-exec

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x  /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)

entrpoint.sh 脚本使用作为环境变量传入的 UID 将用户添加到容器中。

#!/bin/bash

# Add local user
# Either use the LOCAL_USER_ID if passed in at runtime or
# fallback

USER_ID=${LOCAL_USER_ID:-9001}

echo "Starting with …
Run Code Online (Sandbox Code Playgroud)

docker alpine-linux

1
推荐指数
1
解决办法
1万
查看次数