Quick shell script question

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?
 
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)
 
Different shells treat globbing slightly different. The script uses /bin/sh, the cli probably tcsh or bash.
 
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
 
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.
 
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.
 
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*
>
 
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.
 
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.
 
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.
 
Back
Top