LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04: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, 04:47 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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, 04: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 04:55 AM.
 
Old 02-15-2011, 05:07 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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, 05: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:54 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
Open Source Consulting | Domain Registration