What's new

Programming Help...Please :-) (1 Viewer)

Alan Erceg

Stunt Coordinator
Joined
Jul 16, 1999
Messages
154
ok here's the deal.....im gettin really frustrated. I have a script to write for a UNIX class......and it is getting the best of me....


here is the basic outline of what it involves...

you are designing a menu driven program for a company(text).. which when the user logs on will tell where the user logged on from...who the user is...and what files are in that users current directory. Now so far i have all of that working fine and dandy. No problems there.

Here is where my problems start...

The menu gives the user a list of options to do...

The User can either:

Delete a file
backup file(s) to a predetermined directory
print file(s) to any printer
move or rename files to any directory where they have permissions
create directories in any directory they are allowed
send files to any user on the system
change file permissions
edit files withtin the VI editor
an option to drop to the shell level
an option to exit out of UNIX completley

I just dont know where to start.....i was thinking makin all of those mini scripts and calling them from my main script would be easier.

so for ex. the user hits 1 to delete a file it would call up a del_script that would look something like this...

#! /bin/sh
#alan erceg
#

echo "Please Enter the file you wish to delete c "
read file

then check to make sure that file exsists but im not sure of the syntax for that

give them confirmation like
echo "File $file has been deleted..Thank You! "

then have it exit back to the main script.....

something like that. Like i said i just dont know where to begin. I have to present this in front of my class tomorrow morning...and its just stressing the hell outta me. this doesnt come to me like C++ or VB or JAVA or anything like that does.

So any help at all would be greatly appreciated..

Thank You
Alan
 

Greg Rowe

Stunt Coordinator
Joined
Nov 29, 2001
Messages
159
Real Name
Greg
I am familiar with bash scripting, and the syntax might be a little differnt but should mostly be the same. The man page will be your friend! It will list all of the tests. Here is a list of *some* of the conditional tests available in bash (again, might be slightly different for a plain old sh).

-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and is set-group-id.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its ``sticky'' bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
file1 -nt file2
True if file1 is newer (according to modification date) than file2.
file1 -ot file2
True if file1 is older than file2.
file1 -ef file2
True if file1 and file2 have the same device and inode numbers.


So I would do

if [ -f $file ]; then
if rm $file; then
echo "$file deleted"
fi
fi

Also, since you have so many little tasks I'd write functions for each. The syntax is:

functionName()
{
}

Then you can call that function with arguments. So if I did:

functionName hello there

$1 would be "hello" in the function, $2 would be there. Also it is really cool to note that you can redirect the output of a function just as though it were a program.

If you have any more questions feel free to email me.

Greg
 

MikeAlletto

Senior HTF Member
Joined
Mar 11, 2000
Messages
2,369
Does it have to be written in a shell script? It would be extremely easy to write this as a simple perl script. I only write shell scripts for really really basic stuff that I have to do otherwise I write it in perl. Then if you write it in perl it is easier to take the next step and make it a web based thing...can you say extra credit? :)
 

Alan Erceg

Stunt Coordinator
Joined
Jul 16, 1999
Messages
154
gregg,

i sent you an email to the address you listed on your webpage since that is the only one i could find...


mike,

not sure about perl script. we run sco unix on nt at my school that i go to....would that work....if so....then im all for it....im curious. I'm gettin ready to leave for work....but if you could give me any info on that....or help with it....it would be greatly appreciated..i figure im gonna be up all night with this bad boy anyways. i should be home around 10ish or so Eastern Time. Let me know what you think mike.

thanks
alan
 

MikeAlletto

Senior HTF Member
Joined
Mar 11, 2000
Messages
2,369
If they have perl installed it will. When you login do a perl -v and see if it comes back. If not then your out of luck. You could install perl on your windows machine also, but might as well go with what you know.
 

MikeAlletto

Senior HTF Member
Joined
Mar 11, 2000
Messages
2,369
Well if you've never written anything in perl you're going to have to learn it. You could check out perl.com or do searches for learning perl on google. But I don't know how much time you have to learn it. I picked it up in about 3 days about 5 years ago just out of school without even knowing what it is, so its not hard :)
Basically your going to have a loop with your menu items in it that. When they select an item a new function will handle the processing then will send them back to the menu.
Something like:
$exit=0;
while (! $exit) {
if ($option eq "a") { delete(); }
elsif ($option eq "b") { backup(); }
.
.
.
.
elseif ($option eq "q") { $exit=1; }
}
exit 0;
Thats your basic frame for it all. An example of the delete function would be something like:
sub delete {
print "Enter full path for the file to delete: ";
$file=STDIN;
chomp ($file);
if (-e $file) { unlink($file);
}
basically that should work (STDIN should have greater than and less than signs around it, but I couldn't get it to show up), but obviously you'll want to catch errors, file exist, permissions, error unlinking the file, etc...
 

Kevin P

Screenwriter
Joined
Jan 18, 1999
Messages
1,439
If you have to do a shell script, you can use the case statement to make your life easier. For example:
Code:
while true
 do
 echo "Please choose from the following options:"
 echo "1. List directory"
 echo "2. Display a file"
 echo "3. Delete a file"
 echo -n "Choose an option: "
 read x
 case $x in
 1) ls
 ;;
 2) echo -n "Enter file name to display: "
 read f
 more $f
 ;;
 3) echo -n "Enter file name to delete: "
 read f
 if [ -f "$f" ]
 then
 rm "$f"
 else
 echo "$f not found or is not a file"
 fi
 ;;
 4) exit
 ;;
 *) echo "You chose an invalid option. Please try again."
 esac
 done
Hopefully this will help you get started.
KJP
 

Users who are viewing this thread

Sign up for our newsletter

and receive essential news, curated deals, and much more







You will only receive emails from us. We will never sell or distribute your email address to third party companies at any time.

Forum statistics

Threads
357,016
Messages
5,128,521
Members
144,245
Latest member
thinksinc
Recent bookmarks
0
Top