Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'.
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!
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!
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.