简体   繁体   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

The commands that feed on stdin for some input to process, are usually fed by echo command. 以stdin为输入进行处理的命令通常由echo命令提供。 Echo dumps the string provided to it on stdout, which in turn is duplicated on stdin using the pipe "|". Echo将提供给它的字符串转储到stdout上,然后使用管道“ |”将其复制到stdin上。 So for the commands which does not require input from stdin or uses some other method of input to process can be written without echo command. 因此,对于不需要stdin输入或使用其他输入方法进行处理的命令,无需编写echo命令即可。

aptitude upgrade : upgrade is an argument to the aptitude program. aptitude upgradeupgradeaptitude程序的一个参数。 If you see output on your screen, it means inside aptitude, its doing some "echoing" to stdout. 如果您在屏幕上看到输出,则表示内部能力,这对stdout产生了一些“回响”。

programs can also be written to take in stdin through a pipe "|", as in your second case. 与第二种情况一样,也可以编写程序以通过管道“ |”接收stdin。 for example, a program in Python that takes in stdin , 例如,Python中的程序采用stdin,

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

and to take in arguments 并接受争论

import sys
file = sys.argv[1]

A combination of these 2 will make the program able to take in stdin or an argument. 这两个的组合将使程序能够接受stdin或参数。 This will be how aptitute or debconf-set-selections is implemented, depending on what language its built on. 这将是实现aptitutedebconf-set-selections方式,具体取决于其所使用的语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM