Skip to content

Commit f3b6b52

Browse files
committed
shell commands written by python
1 parent 407b010 commit f3b6b52

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog = "cat-command",
5+
description = "cat shell command in python "
6+
)
7+
8+
parser.add_argument("-n", action="store_true", help="Display all lines numbers")
9+
parser.add_argument("-b", action="store_true", help="Display numbers non-empty lines")
10+
parser.add_argument("path", nargs="+", help="The file to search")
11+
12+
args = parser.parse_args()
13+
14+
for file_path in args.path:
15+
with open(file_path, "r") as f:
16+
content = f.readlines()
17+
18+
if args.n:
19+
number = 1
20+
for line in content:
21+
print(f"{number}\t{line.strip()}")
22+
number +=1
23+
elif args.b:
24+
number = 1
25+
for line in content:
26+
if line.strip() !="":
27+
print(f"{number}\t{line.strip()}")
28+
number +=1
29+
else:
30+
print("")
31+
else:
32+
print("".join(content))
33+
34+
35+
36+
37+
# if args.b:
38+
# number=1
39+
# for line in content:
40+
# print
41+
42+
# print(content)

implement-shell-tools/ls/ls.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import argparse
2+
import os
3+
import stat
4+
import pwd
5+
import grp
6+
import time
7+
8+
parser = argparse.ArgumentParser(
9+
prog = "ls-command",
10+
description= "ls shell command on python"
11+
)
12+
13+
parser.add_argument("-l", action="store_true", help="Display long format description files")
14+
parser.add_argument("-a", action="store_true", help="Display hidden files along with visible")
15+
parser.add_argument("path", nargs="*", default=["."], help="The file to search")
16+
17+
args=parser.parse_args()
18+
19+
def long_format(path, file):
20+
info = os.stat(path)
21+
permissions = stat.filemode(info.st_mode)
22+
size_file = info.st_size
23+
owner = pwd.getpwuid(info.st_uid).pw_name
24+
group = grp.getgrgid(info.st_gid).gr_name
25+
mtime = time.strftime("%b %d %H:%M", time.localtime(info.st_mtime))
26+
print (permissions, size_file, owner, group, mtime, file)
27+
28+
29+
30+
for path in args.path:
31+
if os.path.isfile(path):
32+
file = os.path.basename(path)
33+
if args.l:
34+
long_format(path, file)
35+
else:
36+
print(file)
37+
elif os.path.isdir(path):
38+
files = os.listdir(path)
39+
40+
if not args.a:
41+
visible_files=[]
42+
for file in files:
43+
if not file.startswith("."):
44+
visible_files.append(file)
45+
files=visible_files
46+
47+
for file in files:
48+
full_file_path = os.path.join(path, file)
49+
50+
if args.l:
51+
long_format(full_file_path, file)
52+
else:
53+
print(file)
54+
55+
56+
57+
58+
59+

implement-shell-tools/wc/wc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog = "wc-count words, lines, characters",
5+
description = "wc shell command on python"
6+
)
7+
8+
parser.add_argument("-l", action="store_true", help="Count lines")
9+
parser.add_argument("-w", action="store_true", help="Count words")
10+
parser.add_argument("-c", action="store_true", help="Counts bytes")
11+
parser.add_argument("path", nargs="+", help="The file to search")
12+
13+
args = parser.parse_args()
14+
15+
for file_path in args.path:
16+
with open(file_path, "r") as f:
17+
content = f.readlines()
18+
19+
text = "".join(content)
20+
count_lines = len(content)
21+
count_words = len(text.split())
22+
count_bytes=len(text.encode("utf-8"))
23+
24+
if not (args.l or args.w or args.c):
25+
print (count_lines, count_words, count_bytes, file_path)
26+
if args.l:
27+
print (count_lines, file_path)
28+
if args.w:
29+
print (count_words, file_path)
30+
if args.c:
31+
print (count_bytes, file_path)
32+
33+
34+
35+
36+
37+
38+

0 commit comments

Comments
 (0)