LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2011, 05:38 AM   #1
MrNatewood
LQ Newbie
 
Registered: Apr 2007
Distribution: Ubuntu
Posts: 7

Rep: Reputation: 0
Begginer's bash script question


Hello,

I am administrating a lab in a university and every semester we need to delete all the home folders of the accounts for the next semester.

I would like to make a bash script that does this automatically and having trouble with it.

Note that I am writing my very first bash script.

What I need to do is make a script to delete the following:
Delete everything in /home/$exp$num/$dir
when "exp" could be either "rt", "ic" or "sp".
"num" could run from 1(single digit) to 45 and dir is "profile" and "work".

This is what I tried to write:
Code:
#!/bin/sh

cd /home

for exp in "rt ic sp"
do
	for num in "1 2 3 4 5 6 7 8 9"
	do
		for dir in "profile work"
		do
			rm -r $exp$num*/$dir/*
done
done
done
exit 0
What seems to be the problem is the reading of "$exp$num" as a joint expression.

Can anyone tell me what exactly am I doing wrong?
 
Old 02-15-2011, 05:47 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Code:
#!/bin/bash
rm -rf /home/rt*
rm -rf /home/ic*
rm -rf /home/sp*
Would that be a possible solution? Dirty, but easy.
 
Old 02-15-2011, 05:53 AM   #3
MrNatewood
LQ Newbie
 
Registered: Apr 2007
Distribution: Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
Thank for suggestion but the problem with that is that there is a directory that needs to saved inside the tree.

For example in /home/rt1
There are three directories
/home/rt1/profile
/home/rt1/work
/home/rt1/RT-DSP

The last of which needs to be saved. I need to only delete "work" and "profile".

Moreover, there are some other users that start with "ic" or one of the others and does not continue with a number. you can imagine a user named "iceweasel" being deleted.

Last edited by MrNatewood; 02-15-2011 at 05:55 AM.
 
Old 02-15-2011, 06:07 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
1. I don't follow you when you say "joint expression." What exactly do you mean by that?

2. I also don't understand why your rm command uses two asterisks. In your original post, you say you want to "Delete everything in /home/$exp$num/$dir" but then you use "rm -r $exp$num*/$dir/*" to do the deletion. If that were to work as I think you expect, when num=1, your rm command would delete numbered directories 1, 10, 100, 1000, 11, 12, 13, 1a, 1b, 1c, 1A, 1B, 1C, etc. And of course, similar behavior when num=2 and so on. If you want num to go from 1 to 45, might I suggest:
Code:
for num in $( seq 1 45 )
Now, I don't know if this will fix your problem (see question #1), but it's good practice to always wrap your variables with curly braces. If you form the habit, you'll never have to worry about special cases when you must use them. In my opinion, they also make the script easier to read. For instance:
Code:
#!/bin/sh

cd /home

for exp in "rt ic sp"
do
	for num in $( seq 1 45 )
	do
		for dir in "profile work"
		do
			rm -r ${exp}${num}/${dir}/*
                done
        done
done
exit 0
 
Old 02-15-2011, 06:20 AM   #5
MrNatewood
LQ Newbie
 
Registered: Apr 2007
Distribution: Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for all the tips.

The problem was with the quotes in the variable condition(should be rt sp ic and not "rt sp ic").
It is now working well without the quotes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script CD question olan Linux - Newbie 6 11-06-2009 03:57 AM
Bash Script Question nade Programming 2 05-22-2009 10:36 PM
Bash script question deiussum Programming 6 08-14-2004 12:43 PM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 04:39 PM
A bash script question J_Szucs Programming 2 05-13-2003 03:13 AM


All times are GMT -5. The time now is 06:05 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration