Can someone tell me why this doesn’t work in .bashrc?
alias ffind=“find / -name ‘$1’”
Cheers.
Can someone tell me why this doesn’t work in .bashrc?
alias ffind=“find / -name ‘$1’”
Cheers.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Because an alias is not a script that has parameters but rather a
replacement of the original text with the new text. Get rid of the $1 and
just type what you want as if ffind were everything else (which it is):
alias ffind="find / -name "
ffind something
Good luck.
Confuseling wrote:
> Can someone tell me why this doesn’t work in .bashrc?
>
> alias ffind=“find / -name ‘$1’”
>
> Cheers.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJK+2jCAAoJEF+XTK08PnB5WKgP/3P1w3XJCQ0sAmOrs3TnxO7n
fhqZvXlVTQ9Sibo+2faSeVmsSKDMi+q5/rEv6mX0QikP3klkmYLc/wBFPGwMcA3R
LCpkao8E7diwu32Vsr98f+pisTXCCUlQs6bdEcl4biat/278Hgz/0Cbor8XLxtoA
nus61DSwxognPdoiIclFuVhGHKKGwT9SHF23PA/5jU+/pfutzNtKCczePhwiWD6L
GmkT590WGSfCEhjis9V3+mzYwaHBRaWLo5LDqNNGruYhgLnvW+fkt3gsrYvdlSs8
wOXr3n85I7F416Xm3/ammHJMWmKAIb6jBe0EiG/gfM3ATK6GfqiThVtBLPHv/Jj1
rEzyvnlwTFdRqZDXASM5E+h96wX1nd3a2TC4bASbBZaIprTLSQ6/jO/jra6/J+Lq
Yf6f9yq0xQNzVb9GmZv+7kMPe7zkmJRLCsw1LSxAgz8M2vM0zAgkHt/ofETz/eKe
grwFOKgA9E8J+7h71xI+6fGp7jyWql5ET9sneG+Ah7YRITnJfJFfF/mgzmH0/rxb
MzCGkuoJLBtKcbWXEvqFUyMmi8gW0vTp/gF7LyvLzQLP5v0bptNUT25+CFuF6BAx
bz0NDmS6XE7nmkZARJWCvgoWsydgPuJpyNK3d+VJoH3IPCeGt//kOU5Nq2h9phay
mjpETi3q9jtskbLcOApi
=N5At
-----END PGP SIGNATURE-----
Well, I’ve worked out that I should be using a function. I’m trying to do something like this;
function ffind() { find $1 -name '$2' 2>/dev/null ; }
But evidently it doesn’t like it…
ETA: Cross posted - but hopefully you see what I’m getting at. Surely there must be some way to do this… I just can’t figure it out.
Why don’t you put your script in a file, e.g. ffind.sh and make your alias in .bashrc to point to it?
I do not have experience with it, but I did a similar thing on cygwin few years ago to have X starting in an environment I like.
Good luck.
Speaking of problems with arguments can anyone tell me what’s wrong with this:
function overlap{
comm -1 -2 <(sort “$1”) <(sort “$2”) ;
}
Thanks,
D
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Missing parentheses:
<code interpreter=’/bin/bash/’>
function overlap(){
comm -1 -2 <(sort “$1”) <(sort “$2”) ;
}
</code>
Good luck.
danielfortin86 wrote:
> Speaking of problems with arguments can anyone tell me what’s wrong with
> this:
>
> function overlap{
> comm -1 -2 <(sort “$1”) <(sort “$2”) ;
> }
>
> Thanks,
>
> D
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJK+8g8AAoJEF+XTK08PnB5QCUP/2qxi9LyMxeOyLPFF9/BZhvW
mXQXxyBmetFXF7R1dchWPUHcEswiTYWl8U9tyr/iN4bk5d4g+BJqwDkF45unBdtX
gUy9pko6GET8z2cNb4B/PpV9NaLIXgfZTv/qti7NcBW8NU3ImRjalb7PQ1j1wzFN
lRvO7hFqHZhHpqtxBdwgvqm1r0Syq7j7KkWNGx6MfkqCcGG2FZBp2wkN6Y2PB1ks
nV6D943+Hr90+kYjxH1Ll8psuOmfUPcVoJKrwd+6k5VR3YhHfj0nm0x1fyLySax3
bluVZDP31Lw2x/7+h2nqk+KvTUtbxwyyCsNq89eSpAmtKVrkqav/uy3GrSDInRPQ
FJrByPLNOxavty+9k9/chxeA8cEiy3qHtJFkbAxnAUIybQoU+zpTvTVlvr8HIOuW
wRENOzVVLVUOrGVQkzNRYS4MD3x3RCtjzZKx9iwkji3ct6FvETPtYRFYXhXqGqPk
j4UI3c7JMrWsB8xn0Fb4BM5KEF5cky79rDtZVm9qA3IL7nGaJ6mP6qKmc3NTYeeo
rfoAPD7aIbZvkD22m09eHt29WB4ZC1FNN6BfhKeeVOWGaheIE/jzwZ/KDA9xub+q
CQ52Kcl9d5YIc4NLxOS32rWTz1/B29P1Tdg6ZmBLjVYgik/norDWQyT8U4V4ELkL
4R1WzF1x93Szo+JiPA7B
=nUTV
-----END PGP SIGNATURE-----
Hi Confuseling,
It doesn’t work because the function you are creating doesn’t accept any parameters(which is the same problem for danielfortin86):
function ffind() { find $1 -name ‘$2’ 2>/dev/null ; }
the function doesn’t know what is $1 and $2 as they were not defined in the function and the function is not accepting them.
The function should look something like this:
function ffind(param1,param2) { find $1 -name ‘$2’ 2>/dev/null ; }
now I don’t know if command find
will be recognized inside the function without telling the shell that this is an outside function and depending on shell might need some type of quotes, which tells the shell that this is an outside function/executable to process it.
Incorrect. Shell functions don’t need formal parameters.
In fact the problem is that confuseling should remove the single quotes around $2 that prevent its substitution.
Just tried it and it works fine.
Ha! You know what, I could have sworn that was one of the first things I tried…
Thanks all!
Excuse the double post: The final product
function ffind() { find $1 -iname '*'$2'*' 2>/dev/null ; }
Great little find function. Cheers Ken.
But you should really double quote $2 just in case it has a space in it.
Thanks again. I’ll point out this thread, rather than copying and pasting what I’m doing there.
My Daily Rant - openSUSE Forums
It now works as I’d like it to (I think… blooming function creep :P), but any further suggestions would be most welcome.
Sorry for the wrong reply,Confuseling and thank you for correction ken_yap. I was not thinking straight bash scripting and thinking that a function has to have input parameters like in a lot of programming languages. I learned something today which is a plus. Again sorry for incorrect info/suggestion/comment.
No problems.
Can anyone help with my next puzzle? This one’s actually a script, rather than a .bashrc excerpt. It doesn’t behave as desired: obviously, I can fix it with case or if branches, but I’d like to know if there’s an elegant solution, forcing find to ‘evaluate’ (if that’s the word) the $POSITION variable and see it as an argument.
#!/bin/sh
optstr=clbe
CASE=-iname
LIST=
POSITION=\"*\$1*\"
while getopts $optstr var
do
case $var in
c) echo "case"; CASE=-name ;;
l) echo "list"; LIST=-ls ;;
b) echo "beginning"; POSITION=\"\$1*\" ;;
e) echo "end"; POSITION=\"*\$1\" ;;
esac
done
shift $(( $OPTIND - 1 ))
echo $POSITION
find ${2:-/} ${3:+-maxdepth $3} $CASE $POSITION $LIST 2>/dev/null
In order to get it to work (ish), replace $POSITION in the find line with “$1” , “$1*” or “*$1”
Also it only accepts the switches if they’re before the other arguments - but I assume that’s normal.
Any clues?
Can you give some examples of how you hope to invoke the POSITION argument?
Well, if you run it as
./ffind.sh home
for example, it would run as
POSITION = “$1”
CASE = -iname
LIST =
${2:-/} = /
${3:±maxdepth $3} =
so the final line is supposed to run as
find / -iname “$1” 2>/dev/null
which should be seen by find as
find / -iname “home” 2>/dev/null
but it doesn’t work. Find receives the string “$1” instead. If you change the last line to
find ${2:-/} ${3:±maxdepth $3} $CASE “$1” $LIST 2>/dev/null
it works. Does that make sense?
A simpler example;
#!/bin/sh
POSITION=\"*\$1*\"
echo $POSITION
find / -name $POSITION
find / -name "*$1*"
Why are the outputs of the two lines different?
Your main problem is that you are not using getopt correctly. It doesn’t work like you thought. You should look at the example provided in /usr/share/getopt/getopt-parse.bash to see how to invoke it. getopt is there to massage the command line into a standard form and to detect illegal options, not to yield the arguments one by one. That’s is done in the next step, with your own loop.
Here’s the corrected script. I have not looked too closely at possible issues with quoting and eval; that’s homework for you.
#!/bin/sh
optstr=clb:e:
CASE=-iname
LIST=
POSITION="*$1*"
# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o $optstr -- "$@"`
if $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-c) echo "case"; CASE=-name; shift ;;
-l) echo "list"; LIST=-ls; shift ;;
-b) echo "beginning"; POSITION="$2*"; shift 2 ;;
-e) echo "end"; POSITION="*$2"; shift 2 ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "$POSITION"
find ${2:-/} ${3:+-maxdepth $3} $CASE "$POSITION" $LIST 2>/dev/null
So I still haven’t been able to figure this problem out, this is the error that I get
compare.sh: line 2: syntax error near unexpected token <' compare.sh: line 2:
function overlap{comm <(sort “$1”) <(sort “$2”) ; ’
Any ideas would be greatly appreciated!
D
Hmm… I’m using getopts, not getopt.
Don’t know how much difference that makes. I’m away for the weekend, but I’ll have a look at this later - thank you for all your help (and for the homework! :)).