I have no idea what is wrong with my simple script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I have no idea what is wrong with my simple script.
# 1  
Old 12-07-2012
I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as it should.

What am I missing?


Code:
#!/bin/bash
if [ -d "~/.TRASH" ]
then
        mkdir ~/.TRASH
fi

DATE=$(date +'_%Y%m%d_%H%M')
mv $PWD/"$1" "~/.TRASH/${1}.${DT}"


Last edited by iamdeman; 12-07-2012 at 06:26 PM..
# 2  
Old 12-07-2012
First, omit double quotes in the if condition. Second, you need to put negation in there to make it work. Try it like this:
Code:
if [ ! -d ~/.TRASH ]

# 3  
Old 12-07-2012
Quote:
Originally Posted by bartus11
First, omit double quotes in the if condition. Second, you need to put negation in there to make it work. Try it like this:
Code:
if [ ! -d ~/.TRASH ]

Perfect. I have no idea why I quoted and could not see it. I am just getting my feet wet. I appreciate the help, it worked beautifully. Now I have a folder .TRASH and files are moved in with an appended date/time. Just need to alias it to RM now.

Thanks again!

EDIT: I spoke too soon. Upon looking in the directory created, it is not appending the date/time to the files uggh. I was doing this to avoid file collision, so this will not work.

---------- Post updated at 05:22 PM ---------- Previous update was at 05:18 PM ----------

Uggh, spoke to soon, dates not appending.
# 4  
Old 12-07-2012
Try:
Code:
mv $PWD/"$1" "$HOME/.TRASH/${1}.${DATE}"

# 5  
Old 12-07-2012
Wow, I have so much to learn and understand. That did do it.

---------- Post updated at 05:35 PM ---------- Previous update was at 05:34 PM ----------

Makes sense too, now that I see it.

Highly appreciate it.
# 6  
Old 12-08-2012
Just a note (and my personal opinion) while this is a good exercise, YOU SHOULD NOT ALIAS RM. The last thing you want to do is forget how powerful it is, and more importantly how permanent it is. Seems like no big deal, but then that one day you carelessly delete something...

One thing you could do though if you still want to proceed is make it so that before it rm's whatever, it performs an ls, prints the output and asks you to confirm that you want to delete.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. Shell Programming and Scripting

need a new idea for a script

Hi all, I now have project in UNIX Solaris and I want to have some new ideas to execute it, so I hope you help me finding new ideas in scripting or some infrastructure .bye (1 Reply)
Discussion started by: hard_revenge
1 Replies

3. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

4. Shell Programming and Scripting

Help with the idea of a script to modify time

Hello friends, I need an idea or a ready solution for a problem i have the following lines in text file: 1 20100920140122 object4 MOVE IN 2 20100920150012 object4 MOVE OUT -- cut -- the second column is the date and time: 20100920140122 = 2010 09 20 14:01.22 what I need to do is to add 40... (8 Replies)
Discussion started by: peetteerr
8 Replies

5. UNIX for Dummies Questions & Answers

What's wrong with this simple program in APUE?

I start wetting my toes in Linux programming. I tried the first program myls.c in Advanced Programming in the Unix Environment. #include <sys/types.h> #include <dirent.h> #include "apue.h" int main(int argc, char *argv) { DIR *dp; struct... (1 Reply)
Discussion started by: cqlouis
1 Replies

6. Shell Programming and Scripting

Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following. I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely... (12 Replies)
Discussion started by: mrpugster
12 Replies

7. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies

8. UNIX for Dummies Questions & Answers

something simple, but i have no idea: a login issue

Hi all, I have a problem not really dramatic but realyl annoying: i've got a groups of users who logon a sunos 2.3 box via a windows telnet client (KEAI, but this is not the problem). they login as perso1 and password. It takes a very very very long time to get in. If i log on the same box... (2 Replies)
Discussion started by: penguin-friend
2 Replies

9. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies

10. UNIX for Dummies Questions & Answers

What is wrong with this simple script?

The script below is supposed to list file and folder names and list the type besides each - It has no practical value - I am just trying to learn KSH (so far sounds like a bad idea!) Expected output should look like this: folder 1 *** dir *** file1 * file * The code runs but produces... (9 Replies)
Discussion started by: GMMike
9 Replies
Login or Register to Ask a Question