如何在 shell 中将用户输入作为程序执行的一部分? [复制]

Posted

技术标签:

【中文标题】如何在 shell 中将用户输入作为程序执行的一部分? [复制]【英文标题】:How to take user input as part of program execution in shell? [duplicate] 【发布时间】:2020-09-24 23:13:12 【问题描述】:

我编写了一个简单的脚本,它从文件中读取文本并将其写入新文件。它目前的功能是提示用户输入文件名,但我想节省时间并将其作为程序执行的一部分。

lattice = []
coords = []
file = input("Enter name of output file: ")

with open(file) as openfile:
    for line in openfile:
            if ("acell" in line) and ("Bohr" in line):
                Sort = line.split()
                for value in Sort:
                    lattice.append(value)

with open(file, 'r+') as f:
    for line in f:
        if ("xred" in line) and ("E" in line):
            coords.append(line)
            for i in range(5):
                line = f.readline()
                coords.append(line)

Out = file.replace('.out','.coords')
g = open(Out,"w+")

g.write("\n*********************************************************\n\n")

g.write("Initial xred \n\n")
for i in range(len(coords)+1):

    if i <= 5:
        g.write(str(coords[i]))

    if i == 6:
        g.write("Final xred\n\n")

    if i >= 7:
        g.write(str(coords[i-1]))

g.write("Initial acell \n\n")
g.write(str(lattice[1]+' '+str(lattice[2]+' '+str(lattice[3])+'\n')))

g.write("\nFinal acell \n\n")
g.write(str(lattice[6]+' '+str(lattice[7]+' '+str(lattice[8])+'\n\n')))

g.write("\n*********************************************************\n\n")

g.close()

用户是否可以在一个命令中输入“python program.py file.out”以生成file.coords?

【问题讨论】:

【参考方案1】:

是的,只需使用sys.argv[1](0 是文件本身的名称)

from sys import argv
lattice = []
coords = []
try:
    file = argv[1]
except IndexError as e:
     # some error handling in case the file is not called with an argument

with open(file) as openfile:
    for line in openfile:
            if ("acell" in line) and ("Bohr" in line):
                Sort = line.split()
                for value in Sort:
                    lattice.append(value)

with open(file, 'r+') as f:
    for line in f:
        if ("xred" in line) and ("E" in line):
            coords.append(line)
            for i in range(5):
                line = f.readline()
                coords.append(line)

Out = file.replace('.out','.coords')
g = open(Out,"w+")

g.write("\n*********************************************************\n\n")

g.write("Initial xred \n\n")
for i in range(len(coords)+1):

    if i <= 5:
        g.write(str(coords[i]))

    if i == 6:
        g.write("Final xred\n\n")

    if i >= 7:
        g.write(str(coords[i-1]))

g.write("Initial acell \n\n")
g.write(str(lattice[1]+' '+str(lattice[2]+' '+str(lattice[3])+'\n')))

g.write("\nFinal acell \n\n")
g.write(str(lattice[6]+' '+str(lattice[7]+' '+str(lattice[8])+'\n\n')))

g.write("\n*********************************************************\n\n")

g.close()

【讨论】:

以上是关于如何在 shell 中将用户输入作为程序执行的一部分? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何编写一个简单的shell脚本.

什么是shell

Shell简介:什么是Shell,Shell命令的两种执行方式

Shell简介:什么是Shell,Shell命令的两种执行方式

Shell简介:什么是Shell,Shell命令的两种执行方式

初识shell