Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions implement-shell-tools-python-mansoor/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world, Hi, ETC
Binary file not shown.
47 changes: 47 additions & 0 deletions implement-shell-tools-python-mansoor/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import argparse

parser = argparse.ArgumentParser(
prog="cat",
description="Concatenate and display files",
)

parser.add_argument(
"-n",
action="store_true",
help="number all lines",
)

parser.add_argument(
"-b",
action="store_true",
help="number non-empty lines",
)

parser.add_argument(
"files",
nargs="+",
help="the files to read",
)

args = parser.parse_args()

line_number = 1

for filename in args.files:
try:
with open(filename) as file:
for line in file:
content = line.rstrip("\n")

if args.b and content == "":
print()
continue

if args.n or args.b:
print(f"{line_number}\t{content}")
line_number +=1
else:
print(content)

except OSError as error:
print(f"{error.strerror}")
52 changes: 52 additions & 0 deletions implement-shell-tools-python-mansoor/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import argparse
import os

parser = argparse.ArgumentParser(
prog = "ls",
description = "List directory content"
)

parser.add_argument(
"paths",
nargs = "*",
default=["."],
help= "the directory path to process",
)

parser.add_argument(
"-1",
"--one",
action="store_true",
help="list one file per line",

)

parser.add_argument(
"-a",
"--all",
action="store_true",
help="List all files including hidden files such as '.' and '..' "
)

args = parser.parse_args()

for target_dir in args.paths:
try:
files = os.listdir(target_dir)

if args.all:
file_list = sorted([".", ".."] + files)
else:
file_list = []
for name in files:
if not name.startswith("."):
file_list.append(name)
file_list.sort()

if args.one:
for name in file_list:
print(name)
else:
print(" ".join(file_list))
except OSError as error:
print(f"{error.strerror}")
86 changes: 86 additions & 0 deletions implement-shell-tools-python-mansoor/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import argparse

parser = argparse.ArgumentParser(
prog ="wc",
description ="my word, line and byte count",
)

parser.add_argument(
"-l",
"--lines",
action="store_true",
help="count lines"
)

parser.add_argument(
"-w",
"--words",
action="store_true",
help="count words",
)

parser.add_argument(
"-c",
"--bytes",
action ="store_true",
help="count bytes",
)

parser.add_argument(
"files",
nargs="+",
help=" the files to process",
)

args = parser.parse_args()

if not args.lines and not args.words and not args.bytes:
show_line = True
show_words = True
show_bytes = True
else:
show_line = args.lines
show_words = args.words
show_bytes = args.bytes

total_lines = 0
total_words = 0
total_bytes = 0


for filename in args.files:
try:
with open(filename, "rb") as f:
content = f.read()

lines = content.count(b"\n")
words = len(content.split())
bytes_count = len(content)

total_lines += lines
total_words += words
total_bytes += bytes_count

output = []
if show_line:
output.append(str(lines))
if show_words:
output.append(str(words))
if show_bytes:
output.append(str(bytes_count))
output.append(filename)
print("\t".join(output))

except OSError as error:
print(f"{error.strerror}")

if len(args.files) > 1:
total_output = []
if show_line:
total_output.append(str(total_lines))
if show_words:
total_output.append(str(total_words))
if show_bytes:
total_output.append(str(total_bytes))
total_output.append("total")
print("\t".join(total_output))
Binary file added implement-shell-tools/.DS_Store
Binary file not shown.
Binary file added implement-shell-tools/cat/.DS_Store
Binary file not shown.
Binary file added individual-shell-tools/.DS_Store
Binary file not shown.
Loading