Global Pattern - exclude directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Global Pattern - exclude directories
# 1  
Old 08-12-2013
Global Pattern - exclude directories

All,

I am trying delete folder by adding pattern not to delete certain folders. But i struck with error.

When i use below command from command line, it works fine.

shopt -s extglob
rm -rf !(test1|test2|test3)


But when i use the same in shell script, i get the below error.

syntax error near unexpected token `('

Any help?
# 2  
Old 08-12-2013
Did you set the extglob option in the script as well?
# 3  
Old 08-12-2013
Yes, i did.

---------- Post updated at 09:51 PM ---------- Previous update was at 09:40 PM ----------

And one more information, the script works perfectly in my Mac bash terminal but fails in solaris sun bash terminal.
# 4  
Old 08-12-2013
The default shell on Mac OS is bash; the default shell on SunOS is a Bourne shell. What shell do you specify in the 1st line of you script?
# 5  
Old 08-12-2013
bash for the both.

Just now i got the additional info even it fails in Mac OS. And the below code fails particularly when inside the user function.

Code:
#!/bin/bash
SRC_HOME=/u/home/test/src

pack()
{
cd "$SRC_HOME"
	echo "-----Little House keeping-----"
	shopt -s extglob
	rm -rf !(test1|test2|test3)

}
pack


Last edited by Don Cragun; 08-12-2013 at 02:20 PM.. Reason: Use CODE (not ICODE) tags for multi-line code segment.
# 6  
Old 08-12-2013
Show us the bash versions on both computers.
# 7  
Old 08-12-2013
I found just now the issue not due to OS shell. But the syntax error arises when we use the below code in user function

shopt -s extglob

rm -rf !(test1|test2|test3)


---------- Post updated at 10:21 PM ---------- Previous update was at 10:14 PM ----------

Moved the line shopt -s extglob out of the user function and Issue resolved.

Thanks for all responses.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude directories in FIND command

Can you please help tweak the below command to exclude all directories with the name "logs" and "tmp" find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. AIX

Exclude Directories in my tar command

Hi, im having some issues after i execute the next command: tar -cvf /varios/restore/test.tar -X /jfma/test1/excludefile /jfma | gzip -c > /varios/restore/test.tar.gz this creates the desired "test.tar.gz" file, but whe i try to open it it says "tar: 0511-164 There is a media read or write... (6 Replies)
Discussion started by: blacksteel1988
6 Replies

3. Programming

Using sql like to choose a pattern and exclude a pattern

Hi, I am writing an sql query in which i have to choose only that url that are from particular host but are not zip files. something like: select * from mytable where url like '%myhost%' and url not like '%zip%' Is there a simple way to combine these two condition into one? (3 Replies)
Discussion started by: vickylife
3 Replies

4. Shell Programming and Scripting

Perl exclude directories in command line

Hi, I use find command to list all the files in a directory and its sub-directories, but the problem is to exclude certain directories during search. Can i give the directory names in command line to skip them and search rest of the directories? For example i have directories: test ../test1... (1 Reply)
Discussion started by: nmattam
1 Replies

5. Shell Programming and Scripting

How to exclude the empty directories

Hi., I have a script, in which I am processing a files present in the directory types. ls -lrt | grep ^d | grep Dir_type | awk -f '{print $9}' |\ while read dir_name; do #operations done where Dir_type is the pattern in which directories get created. How to filter out empty... (2 Replies)
Discussion started by: IND123
2 Replies

6. Shell Programming and Scripting

Help - Find command to exclude sub-directories

Hi Forum. I'm trying to write a script that finds and deletes files that are older than 300 days. The script will read a table that contains the following 3 columns: 1st col: “Y” means sub-directory scan; "N" means no subdirectory scan 2nd col: sub-directory location 3rd col: File prefix... (7 Replies)
Discussion started by: pchang
7 Replies

7. Shell Programming and Scripting

Find but exclude directories

Hello, I have a line in my script to find the files changed in the last 24 hours. It is as below: find /home/hary -type f -mtime -1 I now want to exclude a directory named "/home/hary/temp/cache" from the above find command. How do I add it to my script? Any help is appreciated. ... (9 Replies)
Discussion started by: tadi18
9 Replies

8. UNIX for Dummies Questions & Answers

find command to exclude directories

Howdy I have this directory structure ... eep eepaptest eepfatest eepgltest eep.old eeppoptest ehf ehfaptest ehfgltest ehp ehpgltest I want to find files in these directories, but I want to exclude eep, ehf & ehp. Cany anyone help with the correct command ?? (1 Reply)
Discussion started by: SmurfGGM
1 Replies

9. UNIX for Advanced & Expert Users

GNUTAR exclude directories

Hi, Can anyone help me with the following: I want to make a backup from my (AIX) system. But I don't want to backup a couple of temp-directories. I tried it with the X-option, but that didn't work. I hope that someone can help me. Thanks in advance. Corine (6 Replies)
Discussion started by: TheBlueLady
6 Replies
Login or Register to Ask a Question