2017-04-27 50 views
2

我試圖傳入參數並將其打印出來。我不能。如何在處理|時通過+讀取參數(管)?

嘗試#1

curl www.site.com/bash/bashrc.sh | bash 

    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 3391 100 3391 0  0 66809  0 --:--:-- --:--:-- --:--:-- 67820 
There 0 arguments 

嘗試#2

curl www.site.com/bash/bashrc.sh | bash yellow red green 

bash: yellow: No such file or directory 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 3391 100 3391 0  0 130k  0 --:--:-- --:--:-- --:--:-- 132k 
(23) Failed writing body 

我的目標是能夠訪問我的論點,我在我的腳本中傳遞:yellowred,​​

[0]:的bash

[1]:黃色

[2]:紅色

[3]:綠色


bashrc.sh

#!/bin/bash 

for i in [email protected] 
do echo $i 
done 

echo "There $# arguments" 

.... 

我該如何達到目標?

回答

1

使用-s開關:

curl www.site.com/bash/bashrc.sh | bash -s yellow red green 

yellow 
red 
green 
There 3 arguments 

man bash

-s If the -s option is present, or if no arguments remain after option processing, 
    then commands are read from the standard input. This option allows the positional 
    parameters to be set when invoking an interactive shell.