Small
shell
* ReadMe *
Basic
scheme:
This
project is to simulate the environment of small shell. I do it in Bourne shell
programming language. Basically, there are two scripts. One is smshell, which
is a login script to guarantee the correct login for users, once the user has
logged in correctly, it will call the other script named shellloop to enter the
small shell environment to do the file manipulations.
Specification:
There
is a passfile in the same directory with the smshell and shellloop scripts, so
that normal users can’t access it through their own directory, since all of the
user directories are the subdirectories of the directory passfile belongs to.
And for the convenience, I assume all of the password should be 4 digits. If
the username or password is not correct, the system will prevent the users from
entering their accounts. Once typing “logout” in such stage, you will leave the
smshell login environment and resume to the standard Unix shell environment.
Once
the user has logged in correctly, the prompt will change to “smsh>”, and the
script will call the other script shellloop to enter that user’s account. Its
directory is: /csc/s01/lfu/SmShell/username.
If this directory exists, the user can execute file manipulation
directly, otherwise, the system will set up a new directory for this login
user. Once the system has found the user’s directory, the user can execute the
following manipulations:
.create
a new project: the command is create. The system will ask you to enter the name
of the project you want to create, can once the project has been created, the
corresponding bin, doc and src subdirectories for this project are created
automatically.
.execute
any .class files in the top bin folders: the command is execute. The system
will ask you to enter the name of the file you want to execute. If the file you
specified doesn’t exist, the system will tell you about that.
.list all of the files or
directories in the user’s top directory: the command is ls
.logout of the file manipulation
system and go back to the log in environment.
.open
an existing file: the command is open. The system will ask you type in the
project name you want to open, if it exists, the prompt will change to
“$projectname>”. There is a while loop to keep track of the manipulations of
the projects. Inside this while loop, we have the following manipulations:
.close this project: the
command is close.
.create
a new file: the command is new. The system will ask you to type in the file
file you want to create, if it already exists, the system will tell you.
.delete
a file from this project: the command is delete. The system will ask you to
type in the file you want to delete, if this file doesn’t exist, the system
will tell you about it. If the system has found this file, it will ask you
again to double check, if the answer is yes, the system will delete the file
and its relative executable files.
.compile
the source files: the command is compile. The system will ask you type in the
file name. If it doesn’t find this file, the system will tell you about it.
During compilation, if you get some errors, the system will tell you, all of
the errors are saved in the error file.
.execute
the .class files in the bin directory: The command is run.
.view
the source files in the src directory: the command is view. The system will ask
you type in the file name. If it doesn’t find this file, the system will tell
you about it.
.list
all of the files in this project: the command is lsp.
.edit
the existing files: the command is editp. The system will ask you type in the
file name. If it doesn’t find this file, the system will tell you about it.
.move
the executable files into the top bin folder.
.error the
errors you get during compilation: the command is error.
In my
project, the compiler I use is javac, so the source files in the src folder are
supposed to be *.java, and the executable files in bin folder are supposed to
be *.class. In my shell, if the command typed in does not correspond to what I
have specified, a help manu will come out to clarify all kinds of commands in
my small shell. It’s a friendly interactive interface in my scripts, so it’s
easy for user to know how to do or what to do.
Construction:
Topdir:
/csc/s01/lfu/SmShell
/ |…………… \
usrname1
usrname2 usrnamen
/ / |..\ / /..\
/ /..\
bin p1 p2 pn bin p1 pn bin p1 pn
//\ //\//\ //\//\
//\//\
bin src doc……………bin src doc bin src doc
Shortcoming:
There
is one flaw in my program due to the technique difficulty and the limit of my
knowledge. While typing the password from the keyboard, I can’t encrypt it from
the screen.
* Scripts *
-------------------------------------smshell------------------------------------
# This
is a small shell which simulates the shell functions, including
#
login, directory management, and file management.
#
First, providing Login for the user, the requirements are the
#
following: password protection; all user files are in my file system;
# each
user has their own directory structure; User can’t touch other users'
#
files. We assume no two user names and no two passwords are the same. And
# we
assume the password can only be four digits.
# for
the smshell, I consult Dr. Shragger’s basic construction of the program.
count=1
ok=0
while [
"$count" -eq 1 ] #outer loop
to check the correct login
do
echo
Please login:
read
name
if [
"$name" = logout ] #jump out
of the loop
then
count=0
fi
if [
"$count" = 1 ]
then
echo password:
read password
if [ "$password" = "" ]
then
echo need a password!
else
#extract the lines match user's name to
another file called ntest
grep ^$name passfile > ntest
#count the number of lines in the
matching name file
lines=`wc -l ntest`
#if there is a match of the name, then
go on processing password
if
[ "$lines" != 0 ]
then
cut -c10-13 ntest > ptest
#extract password portion
if
grep ^$password ptest > /dev/null
then
ok=1
echo match!
else
echo incorrect password!
fi
fi
fi
fi
#name
and password match, enter the user's account
if [
"$ok" = 1 ]
then
shellloop $name #execute
the loop to manipulate the files
fi
#do
something reset work, delete the intermediate files
if [ -f
ntest ]
then
rm
?test #delete the unnecessary
intermediate files
fi
ok=0
name=0
password=0
done
echo
you are logging out...
-----------------------------------shellloop-----------------------------------
# this
script takes the user's name as the argument to enter that user's
#
account. And do the file manipulation.
usrname=$1
topdir="$HOME/SmShell/$usrname"
binfiledir="$topdir/bin"
if [ -d
$topdir ] #check the existence of $topdir, if now, create a new one.
then
echo find $topdir
else
echo creating $topdir
mkdir $topdir
mkdir $binfiledir
fi
echo Hi
$usrname, welcome to the small shell development
cont=1
while [
"$cont" -eq 1 ]
do
PS1="smsh>" #change the prompt
echo $PS1
read choice
case "$choice"
in
logout) cont=0
echo goodbye, $usrname;;
#logout
ls)
echo Executable Files #get a list of
files
ls $binfiledir #get
the whole executable files
echo project files
ls $topdir #get projects and top bin file
cont=1;;
#create the new project.
create) echo type in the project you want to create:
read project
if [ -d $topdir/$project ]
# check if it exists
then
echo already exists!
else
mkdir
$topdir/$project #create a new
project
mkdir
$topdir/$project/bin
mkdir
$topdir/$project/doc
mkdir
$topdir/$project/src
fi
cont=1;;
# execute the executable java files in the top bin.
execute) cd
$binfiledir
echo "type in the file you want to execute(no
extension):"
read filename
if [ -f ${filename}.class ]#check its existence
then
java $filename
else
echo No such file!
fi
cd ..
cont=1;;
#open a existing project to
manipulate the files in it
open)
count=1
echo type in the project you want to open:
read oproject
if [ -d $topdir/$oproject ] #check the
existence
then
cd $topdir/$oproject
#while loop to control the manipulation within the project
while [ $count -eq 1 ]
do
PS1="$oproject>"
#change the prompt
echo $PS1
read ones
case "$ones"
in
close) count=0;; #close the project and jump out of the loop
#create new files in this
project
new) echo "type in the file you want to
create(no extension):"
read file0
if [ -f src/${file0}.java -a -f doc/${file0}.txt ]
then
echo already exits!
else
echo "which file do
you want to create(j or t)"
read ans
if [ "$ans" = j ]
then
vi src/${file0}.java
else
vi doc/${file0}.txt
fi
fi
count=1 ;;
#delete the source file and
the relative object files
delete)
echo "type in the file you want to delete(no extension)"
read file1
if [ -f src/${file1}.java ]
then
echo "do you really want to delete
it(y/n)?" #double check
read answer
if [ "$answer" = y ]
then
rm
src/${file1}.java
if [ -f
bin/${file1}.class ]
then
rm bin/${file1}.class
fi
if [ -f
../bin/${file1}.class ]
then
rm ../bin/${file1}.class
fi
fi
else
echo No such
file
fi
count=1;;
compile) echo "type in the source
file you want to compile(no extension):"
read file2
if [ -f src/${file2}.java ]
then
javac src/${file2}.java > error 2>&1 #catch the errors
if [ -s error ]
then
echo You
got errors!
else
chmod u+x src/${file2}.class
cp src/${file2}.class
bin/${file2}.class
fi
else
echo No such file!
fi
count=1;;
run)
echo "type in the executable file you want to run(no
extension):"
read file3
if [ -f bin/${file3}.class ]
then
cd bin
java $file3
cd ..
else
echo No such file!
fi
count=1;;
view) echo "type in the source file
you want to view(no extension):"
read file4
if [ -f src/${file4}.java ]
then
cat src/${file4}.java
else
echo No such file!
fi
count=1;;
lsp)
echo source files #list all kinds of files in this project
ls src
echo executable files
ls bin
echo text files
ls doc
count=1;;
editp) echo type in the source or text files you want to
echo "edit(with extension):"
read file5
if [ -f src/$file5 ]
then
vi src/$file5
elif [ -f doc/$file5 ]
then
vi doc/$file5
else
echo No such file!
fi
count=1;;
move) cd src #move the executable files into the top bin
for files in `ls -1`
do
if [ -x $files ]
then
mv
$files ../../bin/$files
echo
move $files
fi
done
cd ..
count=1;;
error) if [ -s error ] #see the
errors
then
cat error
fi
count=1;;
* )
echo bad command. You are manipulating in your $oproject
echo directory. The valid commands are the following:
echo delete-delete an existing source file and relative
echo object file, text file.
echo editp-edit an existing source or text file
echo compile-compile a source file into an object file
echo " and put those executable
files into bin directory."
echo view-display an existing source file
echo lsp-get a list of all kinds of files in this
echo " $oproject directory."
echo run-run the executable file
echo move-move all the executable
files to the top_level
echo " bin
file"
echo close-close an existing project
echo new-create new source or text files
echo error-view the errors from the
latest compilation;;
esac
done
else
echo No such directory
fi;;
* ) echo bad command.
echo ls-get a list of project and the outer bin directory
echo logout-logout the shell loop
echo execute-execute any .class program in the top bin
echo create-create a new project and the relative
echo " lower_level directories"
echo open-open an existing project, and manipulate within
echo " that project. Look up in the open command in detail";;
esac
done
* Output *
Please
login:
lixifu
password:
blue
match!
find
/csc/s01/lfu/SmShell/lixifu
Hi
lixifu, welcome to the small shell development
smsh>
execute
type in
the file you want to execute(no extension):
world
world!smsh>
open
type in
the project you want to open:
project1
project1>
lsp
source
files
hello.java world.java
executable
files
hello.class world.class
text
files
hello.txt
project1>
dir
bad
command. You are manipulating in your project1
directory.
The valid commands are the following:
delete-delete
an existing source file and relative
object
file, text file.
editp-edit
an existing source or text file
compile-compile
a source file into an object file
and put those executable files into bin
directory.
view-display
an existing source file
lsp-get
a list of all kinds of files in this
project1 directory.
run-run
the executable file
move-move
all the executable files to the top_level
bin file
close-close
an existing project
new-create
new source or text files
error-view
the errors from the latest compilation
project1>
delete
type in
the file you want to delete(no extension)
hello
do you
really want to delete it(y/n)?
n
project1>
lsp
source
files
hello.java world.java
executable
files
hello.class world.class
text
files
hello.txt
project1>
view
type in
the source file you want to view(no extension):
world
class
world{
public
static void main(String[] args){
System.out.print("world!");
}
}
project1>
compile
type in
the source file you want to compile(no
extension):
hello
You got
errors!
project1>
error
src/hello.java:4:
'}' expected.
}
^
1 error
project1>
move
project1>
lsp
source
files
hello.java world.java #executable hello.class has been
moved to the top bin.
executable
files
hello.class world.class
text
files
hello.txt
project1>
close
smsh>
ls
Executable
Files
hello.class world.class
project
files
bin project1 project2 project3
oh
bad
command.
ls-get
a list of project and the outer bin directory
logout-logout
the shell loop
execute-execute
any .class program in the top bin
create-create
a new project and the relative
lower_level directories
open-open
an existing project, and manipulate within
that project. Look up in the open command in
detail
smsh>
logout
goodbye,
lixifu
Please
login:
logout
you are
logging out...