开发者

Source From Standard In (Bash on OSX)

开发者 https://www.devze.com 2022-12-25 08:57 出处:网络
I am trying to do something like this ruby test.rb | source /dev/stdin where test.rb just prints out cd /. There开发者_运维知识库 are no errors, but it doesn\'t do anything either. If I use this:

I am trying to do something like this

 ruby test.rb | source /dev/stdin

where test.rb just prints out cd /. There开发者_运维知识库 are no errors, but it doesn't do anything either. If I use this:

 ruby test.rb > /tmp/eraseme2352; source /tmp/eraseme2352

it works fine, but I want to avoid the intermediate file.

Edit: The whole point of this is that the changes need to persist when the command is done. Sorry I didn't make that clearer earlier.


You can try:

$(ruby test.rb)

$(...) tells bash to execute whatever output is produced by command inside ().


eval `ruby test.rb`


The following code works for me on Mac OS X 10.6.7:

/bin/bash

sw_vers   # Mac OS X 10.6.7

bash --version   # GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

source /dev/stdin <<<'echo a b c'

source /dev/stdin  <<< "$(ruby -e 'puts "echo a b c"')"

source /dev/stdin <<<'testvar=TestVal'; echo $testvar


source /dev/stdin <<-'EOF'
echo a b c
EOF

source /dev/stdin <<-'EOF'
$(ruby -e 'puts "echo a b c"') 
EOF

(
source /dev/stdin <<-'EOF'
testvar=TestVal
EOF
echo $testvar
)


Until a more experienced bash hacker comes along to correct me, you could do this:

for c in `ruby test.rb` ; do $c ; done

Caution: This doesn't do what you want. Read the comments!


bash (not sh):

while read -a line
do
  "${line[@]}"
done < <(somescript)

Spaces in arguments to commands will need to be backslash-escaped in order to work.


How about just

ruby test.rb | bash
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号