and I want to learn Linux ( and shell scripting).
I have to write a script who is Listing a directory using Python
pwd - Displays the full path of the current directory.
ls [options] [directory] - Lists the contents of the directory. If no directory is specified, list the contents of the current directory; without the -a / -all option, the hidden files / folders (whose names begin with.) are not displayed. If it receives the path to a file as a parameter, it will display the parameter itself. Each file / directory will be displayed on a new line. In case of error it returns the value -80
a, –all also displays hidden files / folders (whose names start with.)
-R, -recursive displays the contents of each directory in the hierarchy. For files / folders that are not directly in the read point, the full path, eg output / test / file, will be displayed.
python busybox.py pwd
/home/pi/my_directories
…
$ python busybox.py ls
directory1
Directory2
File1
file2
$ python program.py ls -a
.
…
directory1
Directory2
File1
File2
$ python program.py ls Directory2
f1
f2
…
import os, sys, stat
import shutil
def pwd(vargv):
print(os.getcwd())
return 0
def mkdir(vargv):
str = len(vargv)
if str== 0:
return -30
try:
for i in range(0, str):
if not os.path.exists(vargv[i]):
os.mkdir(vargv[i])
except Exception as e:
return -30
return 0
def mv(vargv):
str = len(vargv)
if str <= 1:
return -40
if not os.path.exists(vargv[0]):
return -40
if os.path.exists(vargv[1]):
return -40
try:
os.rename(vargv[0], vargv[1])
except Exception as e:
return -40
return 0
def rmdir(varg):
return 0
def rm(varg):
return 0
def echo(varg):
str = len(varg)
if str == 0:
return -10
echo = False
if str > 0 and varg[0] == '-str':
echo = True
if echo:
for i in range(1, str-1):
print(varg[i] + ' ', end='')
print(varg[str-1], end='')
else:
for i in range(1, str-1):
print(varg[i] + ' ', end='')
print(varg[str-1])
return 0
def ln(varg):
return 0
def ls(varg):
return 0
def main(argv):
list = {
'pwd': pwd,
'echo': echo,
'cat': cat,
'mkdir': mkdir,
'mv': mv,
'ln': ln,
'rmdir': rmdir,
'rm': rm,
'ls': ls,
'cp': cp,
'touch': touch,
'chmod': chmod
}
str = len(vargv)
if str < 2:
print("Invalid command")
return cmd = vargv[1]
suit = list.get(cmd, "None")
if func == "None":
print("Invalid command")
return -1
else:
vargv.str[0]
vargv.str[1]
return suit(argv)
main(sys.argv)