script similar to rm utility

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions script similar to rm utility
# 1  
Old 11-29-2010
script similar to rm utility

1. The problem statement, all variables and given/known data:

saferm is a replacement for the rm utility. Rather than removing files, it move files in a sub directoy called".saferm" in the user's home directory. If "~/.saferm" doesn't exist, it is automatically created. The -l options lists the current contents of the "~/.saferm" directory.

Only the -l option or a valid existing filename can be specified at one time. Print a suitable error message and terminate the script otherwise.


2. Relevant commands, code, scripts, algorithms:
  1. check if one argument is given, if anything other than one argument was passed an error message should be printed.
  2. check if the "~/.saferm" directory exists using an "if" statement. If it doesn't, create it.
  3. In the third part, check if "-l" was passed using an"if" statement, If it was, then display the files in the "~/.saferm" directory. If not, then move the file to the "~/.saferm" directory.



3. The attempts at a solution (include all code and scripts):
Code:
if [ $# != 1 ]
   then
      echo "Usage: saferm  -l  (or a filename)" >&2
      exit 1
fi



if[! -d ~/.saferm]
   then
       mkdir ~/.saferm
fi

if["$1-l"]
then
    ls -l ~/.saferm
fi



4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

George Brown,Toronto,Canada,George Sayegh,COMP1156
# 2  
Old 11-29-2010
Code:
if["$1-l"]

Try
Code:
if [ "$1" == "-l" ]

...spaces and all, you need a space between "if" and "[" for it to work. The same goes for your "-d" statement up there, the spaces are important.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-29-2010
thanks for the quick response. So should the code look something like this:

Code:
if [ $# != 1 ]
   then
      echo "Usage: saferm  -l  (or a filename)" >&2
      exit 1
fi



if [! -d ~/.saferm]
   then
       mkdir ~/.saferm
       
fi

if [ "$1" == "-l" ]
   then
    ls -l ~/.saferm

else

 mv $1 ~/.saferm

fi

and would that be the correct way to move the file to the saferm directory?
# 4  
Old 11-30-2010
Code:
 if [ ! -d ~/.saferm ]  # dont forget the space after [ , and before ]

Your >&2 although correct ( where do you usually display the result of an echo?) is superflu IMHO (makes sence if you were to log errors somewhere...)

Be more respectfull in your indentation (2 or 3 char? once decided, stck to it!)
e.g.
Code:
if [ $# != 1 ]
then
   echo "Usage: saferm  -l  (or a filename)" # >&2
   exit 1
fi

if [ ! -d ~/.saferm ]
then
   mkdir ~/.saferm
fi

if [ "$1" = "-l" ]
then
   ls -l ~/.saferm
else
   mv $1 ~/.saferm
fi

Notice the third test, I removed a = sign, it will not work like that in ksh and you didnt mention what shell you were using...

---------- Post updated at 15:15 ---------- Previous update was at 15:01 ----------

And you didnt test if $1 existed, you risk to see you shell complain...

So your final script should look like: (I changed test syntax so you see there are other ways possible...)
Code:
if [ $# != 1 ]
then
   echo "Usage: saferm  -l  (or a filename)" # >&2
   exit 1
fi

if [ ! -d ~/.saferm ]
then
   mkdir ~/.saferm
fi

if [ "$1" = "-l" ]
then
   ls -l ~/.saferm
else
   if test -f "$1"; then
      mv $1 ~/.saferm
   else
     echo "$1 inexistant file "
   fi
fi

All the best
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Isolate text with sed or similar utility

All, I'm getting a list like the following and I'd like to kill each PID in turn. pid (17797) pid (21748) pid (21754) pid (21704) pid (2199) pid (2159) pid (17809) pid (21769) pid (21778) pid (21715) ... (3 Replies)
Discussion started by: ejianu
3 Replies

2. Shell Programming and Scripting

Shell script matching similar records

hello all, I have requirement to identify similar records matching about 80% to 90%.I have to black list customers with multiple accounts. The data is in the Oracle Database, but is there any way I can get the data into flat file and compare the strings and fetch similar matching records? ... (2 Replies)
Discussion started by: kashik786
2 Replies

3. Shell Programming and Scripting

shell script similar to the command HEAD

hello please if anyone can help me make a shell script similar to the command HEAD and TAIL THANKS (3 Replies)
Discussion started by: BELLA86
3 Replies

4. Shell Programming and Scripting

I need help to run this utility from a shell script

I need some help to run this executable from within a shell script. The Script is run the following way at the command prompt. $rateupd Main Menu --Standard Output ----------- --Standard Output 1. - Update Rate --Standard Output 2. - Exit. --Standard Output Enter Selection: 1 --User... (5 Replies)
Discussion started by: rajeeb_d
5 Replies

5. Shell Programming and Scripting

Writing a Utility Script

Hi All ,, I have couple of shell scripts .. I am trying to build a Utility script which would call each script example :: ======== 1) uni.sh 2) uni2.sh 3)uni3.sh when i run the Util script it will come as a menu ,, once i press 1 it will call the first shell script and runs it ..... (10 Replies)
Discussion started by: raghav1982
10 Replies

6. Shell Programming and Scripting

Help on how to call a utility from script

Hi, I am new to shell scripting (sh) I need a script which will call a utility & once u call it.It will ask for inputs on the screen.These inputs it needs to get or read from a txt file. for e.g #! /bin/sh while read line do echo $line done txt file will have test 01/01/2008 ... (3 Replies)
Discussion started by: innocent
3 Replies

7. Shell Programming and Scripting

copy similar files only both at different locations using script.

Hello, Here is the situation.............. # pwd /opt/123 # cat index.txt abc-monitor/homedir/public_html/index.php abc-monitor/homedir/public_html/test/index.php abc-monitor/homedir/public_html/test1/index.php # cp index.txt index.home # cat /root/x (1 Reply)
Discussion started by: fed.linuxgossip
1 Replies

8. UNIX for Dummies Questions & Answers

Need help on SCRIPT(1M) utility

Hi All, I need to do a lot of manual entries at shell prompt. So to collect the logs(each command fired in that session, i use "SCRIPT(1) : make typescript of terminal session" this is kool,but the problem here is that it saves the linefeed, and backspaces along with the commands in the log... (1 Reply)
Discussion started by: amit4g
1 Replies

9. Shell Programming and Scripting

sort utility in script ?

Hi friends, I want to use sort command in script. I used the following syntax in my scipt, sort -t '|' +3 tempcdrext4.cdr > temp.mocdr It give me a error " Input file specified two times." but this command work fine in the prompt without any problem. Can sombody please tell me who... (2 Replies)
Discussion started by: maheshsri
2 Replies

10. Shell Programming and Scripting

cdrdao utility script

friends, i love ya... i wrote a script to automate using cdrdao to burn an audio cd from mp3 files, using the great tutorial at http://tldp.org/HOWTO/MP3-CD-Burning/index.html (check out the site, i believe that it's very well-written). i messed around with it and got to a place where i felt... (2 Replies)
Discussion started by: snwright
2 Replies
Login or Register to Ask a Question