Wildcard variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcard variables
# 1  
Old 06-13-2010
Wildcard variables

Hello everyone,
I'm new to UNIX and thought I'd give writing my own script a try, but the * is giving me some trouble. What I'm trying to do is write a script that does the same thing as rm, but instead of actually deleting the file, it merely moves it to the trash. This is what I have so far:

Code:
#!/bin/sh

garbage="$1"

for file in "$garbage"
do
     mv "$file"/ ~/.Trash/
done

The script works as long as the user inputs a single file name as a variable, but I wanted to make it work for * also (ie 'command *' would move everything to the trash). It seems that if I use * as a variable the script just takes the first file that meets the criteria and uses that file as $1. Surely there's a way to get $garbage to literally mean * and not the name of the first file found?

Last edited by thedoobieman5; 06-14-2010 at 02:31 PM.. Reason: Code tags
# 2  
Old 06-13-2010
Hi.

You have set garbage to $1 - the first file.

You can change that line to:
Code:
garbage="$@"

to mean "all files", but that only works so long as your filenames don't have spaces in them.

The below will work for files with spaces too:
Code:
#!/bin/sh

for file in "$@"
do
  mv "$file" ~/.Trash
done

This User Gave Thanks to Scott For This Post:
# 3  
Old 06-13-2010
Thanks! Worked perfectly. I knew there had to be some built in way to do this.
Thanks again.

---------- Post updated at 08:50 PM ---------- Previous update was at 05:19 PM ----------

OK now something else is happening I don't get. When I run the command it only works for directories, otherwise I get an error message. I thought the "$@" was supposed to include all possible arguments? Smilie
# 4  
Old 06-14-2010
Quote:
Originally Posted by thedoobieman5
OK now something else is happening I don't get. When I run the command it only works for directories, otherwise I get an error message. I thought the "$@" was supposed to include all possible arguments? Smilie

"$@" represents all the arguments.

If you called the script with * as the argument, the shell will expand it and the script will see all the files (including subdirectories) in the current directory (not including dot files), each stored as a separate argument in "$@".
This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 06-14-2010
Ah, I see what's going on. Thanks for the replies. Apparently


Code:
#!/bin/sh

for file in "$@"
do
	mv "$file" ~/.Trash/
done

works fine, but


Code:
#!/bin/sh

for file in "$@"
do
	mv "$file"/ ~/.Trash/
done

only works if you're trashing directories. I guess I have much to learn...

Last edited by thedoobieman5; 06-14-2010 at 02:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Help with wildcard

CD_numb is AM017 this code: set the_Firstcom_CD to (do shell script "ls -d '/volumes/audioNAS/Firstcom/Access Music/' ") & CD_numb gives me this: "/volumes/audioNAS/Firstcom/Access Music/AM017" the item I am looking for is AM017Q. I can get the "*" syntax right so it never finder... (7 Replies)
Discussion started by: sbrady
7 Replies

2. Shell Programming and Scripting

sed using wildcard

Hi Folks, I had a requirement to replace a pattern a.*a with 'a' alone. I'm writing a sed command to do that. But I'm not able to work this out. Pls help me. echo 'a123a456a789' | sed 's/a.*a/a/' Expected o/p : a456a789 But actual o/p is a789. :confused: how can write that... (6 Replies)
Discussion started by: poova
6 Replies

3. Shell Programming and Scripting

Wildcard in ls

Hi Experts, I want to use ls in the below form: ls -l *.{txt,TXT} (working fine) but when i am declaring a variable, VAR="*.{txt,TXT}" ls -l $VAR is not working. Please help. Thanks. (4 Replies)
Discussion started by: sugarcane
4 Replies

4. Shell Programming and Scripting

wildcard help!!

i have got heaps of files (.pdf, .txt and .doc) files in one folder, i am making a program in PERL that helps me find the files i want easier using shell wildcard, something like this!! print "Enter a pattern: (must be in )"; $input = <STDIN>; if (The input is in and valid wildcard... (3 Replies)
Discussion started by: bshell_1214
3 Replies

5. Shell Programming and Scripting

How to use wildcard * in if?

Hi, Can anyone help me how to use * in if statement. File contains below line1:a|b|c|Apple-RED| line2:c|d|e|Apple-Green| line3:f|g|h|Orange| I need to find line by line 4th field contains 'Apple' or not. Please help me at the earliest. (6 Replies)
Discussion started by: jam_prasanna
6 Replies

6. UNIX for Advanced & Expert Users

wildcard help

Can someone please explain the wildcards in this. How is this recursive? When I put this in my terminal it recursively displayed everything. ls .* * (6 Replies)
Discussion started by: cokedude
6 Replies

7. Shell Programming and Scripting

if/else comparison with wildcard

This might be a simple answer but I can't for my life figure it out. If I input "bat", how do I get the condition to pick it up with the wild card *at*. #!/bin/bash read -p "enter" BOO if ; then echo "you win" else echo "you lose" fi (3 Replies)
Discussion started by: zerofire123
3 Replies

8. Shell Programming and Scripting

wildcard

Hi, I have this code to search all "cif" files using wildcard for file in *.cif do grep "Uiso" $file | awk '{ print $3, $4, $5 }' > tet done I get this error "grep: *.cif: No such file or directory" Please where am I going wrong!!! Thank you in advance (6 Replies)
Discussion started by: princessotes
6 Replies

9. UNIX for Dummies Questions & Answers

wildcard

what will the cmd below do? ls *.3 1 members mentions that to seek all permutations and combinations of the mp3 extension ill have to use curly braces, {} and not, . what then will do? (13 Replies)
Discussion started by: abhi
13 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question