Register
It is currently Thu Apr 24, 2014 7:59 am

A simple semantic problem it seems


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Aug 10, 2011 4:49 pm   

Joined: Wed Aug 10, 2011 4:39 pm
Posts: 1
Greets, using this simple lines:

Code:
#!/bin/bash
var="/home/lazar/workspace/Tail"
ls f* > $var/myfile

i get the following output to the file:

ff
file
flash2mp3
fold

but if i make the following change:

Code:
#!/bin/bash
var="/home/lazar/workspace/Tail"
ls $1 >> $var/myfile

and send f* through the console:
Code:
$ ./script.sh f*
i get only "ff" inside myfile

What is the reason?
10x


Top
 Profile  
 PostPosted: Wed Aug 10, 2011 6:41 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Globbing happens before your script is run.
After the expansion is done, in your case, your command line looks like:
Code:
./script.sh ff file flash2mp3 fold

As you can see, « ff » becomes the first parameter of your script ( i.e., $1 ).

If you want to avoid this behavior, you have two solutions:
1. Quote « f* » ( i.e., « 'f*' » or « "f*" » ) and not $1 inside your script ;
2. Use the @ parameter ( i.e. « "$@" » ) instead of $1 inside your script. (preferred)

Also, using ls(1) in a script is bad.
Unless your intention is only to retrieve filenames in a file then do nothing with the data, tell us what you're actually doing.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP