簡體   English   中英

bash腳本編寫shell命令

[英]bash script writing shell commands

為什么有些命令需要echo語句,而其他命令卻可以簡單地編寫而無需:

#!/bin/bash

aptitude upgrade

echo "mysql-server-5.1 mysql-server/root_password password $DB_PASSWORD" | debconf-set-selections

以stdin為輸入進行處理的命令通常由echo命令提供。 Echo將提供給它的字符串轉儲到stdout上,然后使用管道“ |”將其復制到stdin上。 因此,對於不需要stdin輸入或使用其他輸入方法進行處理的命令,無需編寫echo命令即可。

aptitude upgradeupgradeaptitude程序的一個參數。 如果您在屏幕上看到輸出,則表示內部能力,這對stdout產生了一些“回響”。

與第二種情況一樣,也可以編寫程序以通過管道“ |”接收stdin。 例如,Python中的程序采用stdin,

import fileinput
for line in fileinput.input():
    print line

並接受爭論

import sys
file = sys.argv[1]

這兩個的組合將使程序能夠接受stdin或參數。 這將是實現aptitutedebconf-set-selections方式,具體取決於其所使用的語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM