如何使用变量而不是文件来写入任何命令? [英] How to use a variable instead of file to let any command write to?

查看:96
本文介绍了如何使用变量而不是文件来写入任何命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 curl --cookie-jar< filename> 保存cookie Templey并稍后将其加载到脚本中。

I'm using curl --cookie-jar <filename> to save cookie temperley and load it later in a script.

在OS X中没有/ dev / shm,我不希望将太多临时文件写入SSD。

In OS X, there's no /dev/shm and I don't want too many temp files write to SSD.

是否可以使用

变量可以像具有 curl --cookie<的文件一样被读取;(echo $ variable),它涉及磁盘访问吗?

A variable can be read like a file with curl --cookie <(echo "$variable"), is it involved with disk access?

推荐答案

文字问题



在完全通用的意义上:您不能。与Python变量或C程序的变量相比,Shell变量在文件系统上没有独立存在。 ( Environment 变量公开给进程读取,但是更改不会传播回父进程,因此,即使操作系统具有扩展名,该扩展名提供了可通过文件系统接口访问的环境变量,类似于Linux上的 / proc / self / environ -在需要双向通讯的情况下这无济于事。

Literal Question

In the completely generic sense: You can't. Shell variables don't have independent existence on the filesystem any more than Python variables or C programs' variables do. (Environment variables are exposed to be read by processes, but changes aren't propagated back to parent processes, so even if an operating system had extensions that provided environment variables to be accessible via a filesystem interface -- akin to /proc/self/environ on Linux -- that's not helpful here, where two-way communication is needed).

您可以使用 hdiutil diskutil 创建一个带有文件系统的ramdisk,其作用与 / dev / shm 相同。参见ie。 https://gist.github.com/rxin/5085564

You can use hdiutil and diskutil to create a ramdisk with a filesystem on it to serve the same purpose as /dev/shm. See ie. https://gist.github.com/rxin/5085564

一种不可行但可行的方法是使用后台进程在一对FIFO之间泵送数据:

One fugly-but-feasible approach is to use a background process pumping data between a pair of FIFOs:

#!/usr/bin/env bash

# These are the only two operations that touch disk
mkfifo cookie-write.fifo || exit
mkfifo cookie-read.fifo  || exit

# Start a background process that pumps data from the read FIFO to the write FIFO
datapump() {
  while IFS= read -r -d '' content <cookie-write.fifo || [[ $content ]]; do
    printf '%s\0' "$content" >cookie-read.fifo
  done
}
datapump & datapump_pid=$!

# run an initial curl with cookies written to cookie-write.fifo
curl -c cookie-write.fifo http://example.com/login
cookies=$(<cookie-read.fifo)   # read cookies set by login process

# write back from the shell variable to the FIFO to allow read by another curl
printf '%s\0' "$cookies" >cookie-write.fifo
# read in in that new curl process, write back to the FIFO again
curl -b cookie-read.fifo -c cookie-write.fifo http://example.com/do-something
cookies=$(<cookie-read.fifo)  # read cookies as updated by do-something process

这种方法需要格外小心,以免发生死锁:请注意,协同过程首先读取然后写入;如果这些操作都没有发生,那么它将无限期地挂起。 (因此,如果您的 curl 操作不写入任何cookie,则泵送过程不会将模式从读取切换为写入,并且随后尝试读取Cookie状态的尝试可能会挂起)。

This kind of approach requires a great deal of care to avoid deadlocks: Note that the coprocess first reads and then writes; if either of those operations doesn't take place, then it's going to hang indefinitely. (Thus, if your curl operation doesn't write any cookies, the pump process won't switch modes over from reading to writing, and a subsequent attempt to read the cookie state may hang).

这篇关于如何使用变量而不是文件来写入任何命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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