LinuxQuestions.org
Help answer threads with 0 replies.
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 10-06-2011, 05:53 PM   #1
LoveUnix
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Removing all files not in an array


Hello,

I created a simple script to try and learn arrays

#!/bin/sh
# name - .start

#-- Set Some Arrays ...
create=(one two three four five six seven GHGHG JHJsHJH HJHJHJkl)
allowed=(one two three .start)


touch ${create[*]}
rm -rf ${!allowed[*]}


I'm trying to remove all files not in the allowed array.
 
Old 10-06-2011, 07:29 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
${!array[*]} is a reference to the indexes of the array and is commonly used for associative arrays.

I think you will need to loop through all files in the directory and test that it does not exist in allowed and then remove
said file.
 
Old 10-07-2011, 12:29 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Arrays called with the "${array[*]}" form prints the entire array as a single string. Calling them with "${array[@]}" prints the array as a series of separate elements, and if you quote the parameter, each value acts as if it were quoted (thus avoiding word-splitting problems). You should thus always use the latter when running it as part of a command string, and like all parameters, they should nearly always be quoted.

Here's one solution that uses both forms. It requires the regex matching ability of bash's [[ extended test, however (It might also work in ksh).
Code:
for dir in "${create[@]}"; do

	if [[ "${allowed[*]}" =~ $dir ]]; then
		echo "$dir is permitted"
	else
		echo "$dir isn't allowed"
	fi

done
It tests each directory in turn to see if it exists inside the "${allowed[*]}" string, and does the appropriate action. Notice though that only the [@] form is safe to use in for loops.

If you need a more portable version that doesn't depend on [[ or other advanced shell features, then you'll have to use a second nested loop.
Code:
for dir in "${create[@]}"; do

	for dir2 in "${allowed[@]}"; do

		if [ "$dir" == "$dir2" ]; then
			ok=1
			break
		fi
	done

	if [ "$ok" -eq 1 ]; then
		echo "$dir is permitted"
	else
		echo "$dir isn't allowed"
	fi

	ok=0
done
http://mywiki.wooledge.org/BashGuide/Arrays
http://www.tldp.org/LDP/abs/html/arrays.html
 
  


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
manually removing the .vdi files left over after removing VirtualBox adityavpratap Linux - General 2 10-24-2010 01:04 PM
removing files bloodyscript Linux - Newbie 4 07-15-2008 06:20 PM
removing item from array niteshadw Programming 1 08-01-2005 07:06 PM
removing files kjs Linux - Software 1 02-17-2005 03:18 PM
Removing files wihtout removing containing Direcotry caps_phisto Linux - General 2 10-07-2004 09:16 AM


All times are GMT -5. The time now is 04:52 PM.

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