Quick shell script question

mrab54

Member

Reaction score: 1
Messages: 21

I found a nice tip to search for a given port:
# echo /usr/ports/*/*portname*

If the string is found, it echos where and if not it says "echo: No match". I tried putting that into a script as follows:
//fport.sh
Code:
#!/bin/sh

echo /usr/ports/*/*$1*
If I run the script and the string is found it outputs it but if not I get the following:
# sh ./fport blabla
/usr/ports/*/*blabla*

So my question is what is the difference here? Why am I getting different output?
 

Mormegil

Member

Reaction score: 7
Messages: 65

Why not just use

Code:
cd /usr/ports
make search name=blabla
Edit: the script works for me..
 

vermaden

Son of Beastie

Reaction score: 1,212
Messages: 2,792

@mrab54

Try that one:

Code:
#! /bin/sh

ls -d /usr/ports/*/*${1}* 2> /dev/null || echo "not found: ${1}"
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,906
Messages: 31,208

mrab54 said:
I found a nice tip to search for a given port:
# echo /usr/ports/*/*portname*

If the string is found, it echos where and if not it says "echo: No match". I tried putting that into a script as follows:
//fport.sh
Code:
#!/bin/sh

echo /usr/ports/*/*$1*
If I run the script and the string is found it outputs it but if not I get the following:
# sh ./fport blabla
/usr/ports/*/*blabla*

So my question is what is the difference here? Why am I getting different output?
The first one on the CLI makes use of the shell's globbing behavior. The second inside the script doesn't and just prints the *.

http://en.wikipedia.org/wiki/Glob_(programming)
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,906
Messages: 31,208

Different shells treat globbing slightly different. The script uses /bin/sh, the cli probably tcsh or bash.
 

Mormegil

Member

Reaction score: 7
Messages: 65

I meant that it worked for me in an sh script.

Code:
> cat test.sh && echo && sh test.sh angband
#!/bin/sh

echo /usr/ports/*/*$1*

/usr/ports/games/angband /usr/ports/games/mangband /usr/ports/games/zangband /usr/ports/japanese/zangband
 

bigearsbilly

Active Member

Reaction score: 5
Messages: 143

yes well i see you are logged in as root?
that's not a good idea for general messing about.
root uses /bin/csh usually default, i think.
your script is /bin/sh

hence it works differently.
 

Mormegil

Member

Reaction score: 7
Messages: 65

No, regular user using tcsh. I tried it in several shells, not that I think it matters being that the script itself is a bourne shell script. It works in any case, I'm just not 100% sure the shell's handling of globbing is the issue.
 
OP
OP
M

mrab54

Member

Reaction score: 1
Messages: 21

SirDice said:
Different shells treat globbing slightly different. The script uses /bin/sh, the cli probably tcsh or bash.
Thanks for the replies. Yes, it does looks like it is just a difference in how tcsh and sh handle globbing.
Code:
> tcsh ./findport.sh blabla
echo: No match.
> sh ./findport.sh blabla
/usr/ports/*/*blabla*
>
 
OP
OP
M

mrab54

Member

Reaction score: 1
Messages: 21

vermaden said:
@mrab54

Try that one:

Code:
#! /bin/sh

ls -d /usr/ports/*/*${1}* 2> /dev/null || echo "not found: ${1}"
I like this :)

Can somebody explain this? I've seen '... 2> /dev/null' on more than one occasion but I don't understand what it does.
 

vermaden

Son of Beastie

Reaction score: 1,212
Messages: 2,792

Code:
1 --> stdout
2 --> stderr
1> /path/to/file means redirect standart output to file.
2> /path/to/file means redirect standart errors to file.
 

Ruler2112

Well-Known Member

Reaction score: 25
Messages: 422

whereis also reports locations in the /usr/ports tree.

Ex:
Code:
whereis libxml2
libxml2: /usr/ports/textproc/libxml2
Not 100% because of ports named with the version as part of the port name (libltdl22, libtool22, etc), though close.
 
Top