PDA

View Full Version : Quick shell script question


mrab54
October 2nd, 2009, 05:16
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
October 2nd, 2009, 05:33
Why not just use


cd /usr/ports
make search name=blabla


Edit: the script works for me..

vermaden
October 2nd, 2009, 06:16
@mrab54

Try that one:

#! /bin/sh

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

SirDice
October 2nd, 2009, 07:11
ports-mgmt/psearch

SirDice
October 2nd, 2009, 07:14
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?

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_%28programming%29

Mormegil
October 2nd, 2009, 07:28
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_%28programming%29

I'm curious why it worked for me

SirDice
October 2nd, 2009, 07:32
Different shells treat globbing slightly different. The script uses /bin/sh, the cli probably tcsh or bash.

Mormegil
October 2nd, 2009, 09:04
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
October 2nd, 2009, 11:29
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
October 2nd, 2009, 15:52
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
October 3rd, 2009, 19:27
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
October 3rd, 2009, 19:33
@mrab54

Try that one:

#! /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
October 3rd, 2009, 23:32
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
October 6th, 2009, 00:43
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.