LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-11-2003, 09:17 PM   #16
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled

Basically, I think the distinction is that if you post a homework question and somebody just gives you the answer, you're not necessarily *learning* anything and you're basically asking us to help you cheat, besides. And it means you have a 'school' problem that happens to be about Linux - not a Linux problem. But if you are asking questions about the principles of things and seeking to learn and figure out the specifics yourself, I'd figure that was great.

Quote:
First, I'm not sure what the ~ is for in the path
Like I can tell you to find the knowledge you seek by doing 'man bash' and checking out 'tilde-prefix' and 'expansion'.
 
Old 12-11-2003, 09:43 PM   #17
hopmedic
Member
 
Registered: Sep 2003
Location: in a small town west of Chicago
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
digiot - thanks. So, let's see here... Let's say my login is hopmedic (it isn't). The ~/bin would substitute the tilde with /home/hopmedic, making the path /home/hopmedic/bin. I think I got it!

Thanks!

Rich
 
Old 12-12-2003, 06:26 AM   #18
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Correct.
And if you want a really great resource for getting into bash scripting I suggest you grab the entire Advanced Bash Scripting Guide here. I have it installed locally on my laptop so I can refer to it even when I don't have a net connection available. Very neat.

Good luck with your Unix class, sounds like a fun class to me!

Håkan
 
Old 12-15-2003, 02:35 PM   #19
hopmedic
Member
 
Registered: Sep 2003
Location: in a small town west of Chicago
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
Håkan,

Thanks for the tips. I got the pathing working just fine - and got the script working almost exactly as I want it to.

Thanks,

Rich
 
Old 12-16-2003, 09:49 AM   #20
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
I would be interested in seeing what the script looks like if you don't mind.
 
Old 12-17-2003, 09:41 AM   #21
hopmedic
Member
 
Registered: Sep 2003
Location: in a small town west of Chicago
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
I'll post it some time today - there are actually 2 scripts - one to "delete" (called trash), and one to restore. It does output an error for some reason, but it does everything else that I wanted it to. The error doesn't have any effect on anything except the output to stdout.

Rich
 
Old 12-24-2003, 12:21 PM   #22
hopmedic
Member
 
Registered: Sep 2003
Location: in a small town west of Chicago
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
code

/bin/bash,

Sorry it took me so long to post... Been busy with Christmas stuff. Here is what I turned in... Got 100% on it. There are two scripts, and here is how they work. They both take arguments on the command line.

trash moves the file(s) specified in the command line to a hidden folder ~/.trash and appends its path to ~/.trash/.trash_history. You must use this script from the directory where the file exists, and you must have write permissions to the file being "trashed."

restore takes the filename(s) specified in the command line and checks to see if they exist in the ~/.trash directory. If so, it locates the prior path of the file, and moves the file back to that path. Again, you must have write permission, but when restoring a file, you do not have to be in any specific folder. Finally, the script removes the file from the ~/.trash/.trash_history file, so that there are no future references to the restored file in the history file.

Here is the code:

trash:
Code:
#!/bin/sh 
# Usage: trash [arg] [arg] [arg]...
# Check for proper usage
if [ "$#" -eq 0 ] ; then
        echo "Error: no filenames given." >&2        echo "Usage: trash filename1 filename2 ..." >&2
        exit 1
fi
# Set variables
DIR=~/.trash
echo 
echo 
# Is there a .trash directory?
if [ ! -d "$DIR" ] ; then
        mkdir $DIRfi
# File processing loop 
for arg 
do
path=`pwd`
# Check to see that the file exists as a regular file with write permissions
if [ -f "$arg" ] ; then
        if [ -w "$arg" ]; then
                echo "Processing file $path/$arg..."
                mv $arg $DIR/$arg
                 STATUS=$?
                if [ "$STATUS" -eq 0 ] ; then
                        echo "$path     XX$arg" >> $DIR/.trash_history
                        chmod 600 $DIR/.trash_history
                        echo "The file $arg has been removed to the $DIR directory."                else
                        echo "Error: the file $arg was not moved to the $DIR directory."                fi        
else
                echo "The file is not writable by the current user."
                echo "Proceeding to the next file."
        fi
else
         echo "The file $arg does not exist."
        echo "Proceeding to the next file."
fi
echo 
done
echo 
echo"No more files.  Process complete."
restore:
Code:
#!/bin/sh 
# This program will restore a file that was moved to the 
# ~/.trash folder by the trash script.
# Only files that were deleted by the user can be
# restored by that user.
# Usage: restore file1 file2 ...
# Check for proper usage
if [ "$#" -eq 0 ] ; then
        echo "Error: no filenames specified."
        echo "Usage: restore file1 file2 ..."
        exit 1
fi
# Assign global variables
DIR=~/.trash
HX=~/.trash/.trash_history
# Check that there is a history
if [ ! -e "$HX" ] ; then
        echo "Error: file $HX does not exist."
        echo "Unable to proceed."
        exit 2
fi
# File process loop begins here
for arg
do
# Check that the file exists and is writable by the user
if [ -e "$DIR/$arg" ] ; then
        if [ -w "$DIR/$arg" ] ; then
                # find the original path of the file
                path=`grep "    XX$arg$" $HX | cut -f1`
                mv "$DIR/$arg" "$path/$arg"
                STATUS=$?
                if [ ! "$STATUS" -eq 0 ] ; then
                        echo ; echo "An error occurred.  Process halted."
                        echo $STATUS
                        exit 3
                fi
                sed "s/.*XX$arg$//" "$HX" >> restoretmp
                mv restoretmp "$HX"
                     rm restoretmp
          else
                echo ; echo "Error: file $arg not writable by this user."
                echo "File not restored.  Proceeding to next file."
        fi
else
        echo ; echo "Error: file $arg does not exist in the $DIR folder."
        echo "Proceeding to next file."
fi
done
Please note that when I pasted the code, it all ended up on one line, and I had to manually edit it to put the carriage returns in the right places. Since I am only human, and cannot 'test' the code in this window, I can't guarantee that I didn't put a carriage return in the wrong place, or accidentally omit one. I can say, though, that the code did work when executed. There is one problem with it - I get an error output to the display, at the end of the trash script, but I don't know why, as the code does what it is supposed to do.

Rich
 
Old 12-27-2003, 06:45 PM   #23
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Nice work there, hopmedic!


Håkan
 
Old 12-28-2003, 02:37 AM   #24
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Just curious...in trash, line 26:
echo "$path XX$arg" >> $DIR/.trash_history

What is "XX"?
 
Old 12-28-2003, 09:43 PM   #25
hopmedic
Member
 
Registered: Sep 2003
Location: in a small town west of Chicago
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
my code

Quote:
Originally posted by mikshaw
Just curious...in trash, line 26:
echo "$path XX$arg" >> $DIR/.trash_history

What is "XX"?
XX is just two letters to put at the beginning of the string, to make the search easier when restoring the file. It's been a few weeks since I did it, so I don't remember the exact reason I did it, but it had something to do with doing the search, being able to parse the file's name from the path, and being able to later delete the line with sed.

Last edited by hopmedic; 12-28-2003 at 09:45 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
where is bashrc ? Kanaflloric Slackware 12 10-06-2007 02:47 PM
.bashrc ? Longinus Linux - Newbie 2 03-27-2004 11:50 AM
no .bashrc? mattman Slackware 6 06-10-2003 03:59 PM
bashrc nobu Slackware 6 03-21-2003 10:53 AM
.bashrc prasad Linux - General 1 04-14-2001 09:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration