Ejercicios 12f-h
This commit is contained in:
parent
49f131b18d
commit
3787db0fe3
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# Exercise 12h
|
||||||
|
# false command
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.exit(1)
|
|
@ -0,0 +1,45 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# Exercise 12f
|
||||||
|
# tail command
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("stream", default=sys.stdin, nargs="*")
|
||||||
|
parser.add_argument("-v", "--verbose", action="store_true")
|
||||||
|
parser.add_argument("-n", "--lines", default="10", type=str)
|
||||||
|
args = parser.parse_args()
|
||||||
|
res = []
|
||||||
|
|
||||||
|
|
||||||
|
def intonum(n):
|
||||||
|
try:
|
||||||
|
return abs(int(n))
|
||||||
|
except ValueError:
|
||||||
|
print(f"tail: el número de líneas no es válido: «{n}»")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def getlines(lines, n):
|
||||||
|
lines = list(map(lambda l: l.rstrip(), lines))
|
||||||
|
num = intonum(n)
|
||||||
|
if n[0] == "+":
|
||||||
|
return lines[num-1:]
|
||||||
|
return lines[-num:]
|
||||||
|
|
||||||
|
|
||||||
|
if isinstance(args.stream, list):
|
||||||
|
for path in args.stream:
|
||||||
|
lines = Path(path).open().readlines()
|
||||||
|
header = [""] if len(res) != 0 else []
|
||||||
|
if args.verbose:
|
||||||
|
header += [f"==> {path} <=="]
|
||||||
|
res += header + getlines(lines, args.lines)
|
||||||
|
else:
|
||||||
|
lines = args.stream.readlines()
|
||||||
|
header = ["==> entrada estándar <=="] if args.verbose else []
|
||||||
|
res += header + getlines(lines, args.lines)
|
||||||
|
|
||||||
|
print("\n".join(res))
|
|
@ -0,0 +1,12 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# Exercise 12g
|
||||||
|
# yes command
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("words", nargs="*", type=str, default=["y"])
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print(" ".join(args.words))
|
Loading…
Reference in New Issue