# Quick shell script question



## mrab54 (Oct 2, 2009)

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

```
#!/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 (Oct 2, 2009)

Why not just use


```
cd /usr/ports
make search name=blabla
```

Edit: the script works for me..


----------



## vermaden (Oct 2, 2009)

@mrab54

Try that one:


```
#! /bin/sh

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


----------



## SirDice (Oct 2, 2009)

ports-mgmt/psearch


----------



## SirDice (Oct 2, 2009)

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:
> ...



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)


----------



## Mormegil (Oct 2, 2009)

SirDice said:
			
		

> 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)



I'm curious why it worked for me


----------



## SirDice (Oct 2, 2009)

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


----------



## Mormegil (Oct 2, 2009)

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


```
> 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 (Oct 2, 2009)

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 (Oct 2, 2009)

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.


----------



## mrab54 (Oct 3, 2009)

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.  

```
> tcsh ./findport.sh blabla
echo: No match.
> sh ./findport.sh blabla
/usr/ports/*/*blabla*
>
```


----------



## mrab54 (Oct 3, 2009)

vermaden said:
			
		

> @mrab54
> 
> Try that one:
> 
> ...



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 (Oct 3, 2009)

```
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 (Oct 6, 2009)

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

Ex:

```
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.


----------

