How to match pattern in bash?

2011-02-22 Thread Peng Yu
Suppose that I have a variable $x, I want to test if the content of $x
match the pattern 'abc*'. If yes, then do something. (The operator ==
doesn't match patterns, if I understand it correctly.)

Is there such a build-in feature in bash? Or I have to rely on some
external program such as perl to test the pattern matching?

-- 
Regards,
Peng



pushd, popd and dirs of different bash processes communicate by an external file

2011-06-09 Thread Peng Yu
Hi,

I'm wondering if there is a way that different bash processes share
the same stack. One way of doing so is to use a file to save the
stack. It is not difficult to implement this function myself, but I'm
just wondering if there is any simpler solution or anybody has done it
already. Thanks!

-- 
Regards,
Peng



How to match regex in bash? (any character)

2011-09-26 Thread Peng Yu
Hi,

I know that I should use =~ to match regex (bash version 4).

However, the man page is not very clear. I don't find how to match
(matching any single character). For example, the following regex
doesn't match txt. Does anybody know how to match any character
(should be '.' in perl) in bash.

[[ "$1" =~ "xxx.txt" ]]


-- 
Regards,
Peng



Re: How to match regex in bash? (any character)

2011-09-26 Thread Peng Yu
On Mon, Sep 26, 2011 at 9:49 PM, John Reiser  wrote:
> Peng Yu wrote:
>> I know that I should use =~ to match regex (bash version 4).
>>
>> However, the man page is not very clear. I don't find how to match
>> (matching any single character). For example, the following regex
>> doesn't match txt. Does anybody know how to match any character
>> (should be '.' in perl) in bash.
>>
>> [[ "$1" =~ "xxx.txt" ]]
>
> The manual page for bash says that the rules of regex(3) apply:
>
>              An additional binary operator,  =~,  is  available,  with  the  
> same
>              precedence  as  == and !=.  When it is used, the string to the 
> right
>              of the operator is considered an  extended  regular  expression  
> and
>              matched  accordingly (as in regex(3)).  The return value is 0 if 
> the
>              string matches the pattern, and 1 otherwise.
> and also:
>               Any part of the pattern may be quoted to force it to be matched 
> as a
>              string.
>
> Thus in the expression   [[ "$1" =~ "xxx.txt" ]]   the fact that the pattern
> is quoted [here the whole pattern appears within double quotes] has turned the
> dot '.' into a plain literal character, instead of a meta-character which 
> matches
> any single character.
>
> The usual method of avoiding quotes in the pattern is to omit them:
>              [[ $1 =~ xxx.txt ]]   # the dot '.' in the pattern is a 
> meta-character
> or to use a variable:
>              pattern="xxx.txt"   # a 7-character string
>              [[ $1 =~ $pattern ]]   # the dot '.' in $pattern is a 
> meta-character
> Example: using all literals in an instance of bash:
>               $ [[ txt =~ xxx.txt ]] && echo true
>               true
>               $
>
> Also notice that quotes are not needed around the left-hand side  $1 :
>                                                                       Word 
> split‐
>              ting and pathname expansion are not performed on the  words  
> between
>              the  [[  and  ]] ...
>
> Thus there is no need to use quotation marks to suppress word splitting
> inside double brackets [[ ... ]].

Thanks for the clarifications of all the replies. Now the manual makes
much more sense to me.

-- 
Regards,
Peng



Re: How to match regex in bash? (any character)

2011-09-27 Thread Peng Yu
On Tue, Sep 27, 2011 at 6:51 PM, Chet Ramey  wrote:
> On 9/27/11 6:41 PM, Roger wrote:
>
>> Correct.  After reading the entire Bash Manual page, I didn't see much 
>> mention
>> of documentation resources (of ERE) besides maybe something about egrep from
>> Bash's Manual Page or elsewhere on the web.  After extensive research for
>> regex/regexpr, only found Perl Manual Pages.
>>
>> Might be worth mentioning a link or good reference for this ERE within the 
>> Bash
>> Manual (Page)?
>
> The bash man page refers to regex(3).  On my BSD (Mac OS X) system, that
> refers to re_format(7), which documents the BRE and ERE regular expression
> formats.  On an Ubuntu box, to choose a representative Linux example, that
> refers to regex(7), which contains the same explanation, and the GNU regex
> manual.  This sort of "chained" man page reference is common.
>
> If you like info, `info regex' on a Linux box should display both pages.

Since regex(7) is actually what should be referred on ubuntu, and
there is indeed a manpage regex(3) on ubuntu, the difference on which
regex man page should be specify in man bash. I was looking at
regex(3) on my ubuntu, which doesn't have any relevant information.

Also, adding a few more examples just cost a few extra lines, I don't
think that the manpage should be so frugal in terms of adding examples
to elucidate important concepts.

-- 
Regards,
Peng



Re: How to match regex in bash? (any character)

2011-09-29 Thread Peng Yu
On Thu, Sep 29, 2011 at 7:22 AM, Greg Wooledge  wrote:
> On Wed, Sep 28, 2011 at 12:43:01PM -0800, Roger wrote:
>> Seems I used 'man regex' as well here.  AKA regex(3).  But I did
>> realize this a few weeks ago; the real regex description being 'man 7 regex'.
>> The Bash Manual Page denotes only regex(3).
>
> You're relatively fortunate that it's *that* easy to find on Linux.  On
> Linux, regex(3) points directly to regex(7), and you're done.
>
> On HP-UX, regex(3X) points to regcomp(3C) which points to regexp(5) which
> contains the actual definitions.
>
> On OpenBSD, regex(3) doesn't even *have* a SEE ALSO section; it's a dead
> end.  And regcomp(3) is the same page as regex(3), so that doesn't help
> either.  One would have to backtrack entirely, perhaps to grep(1).
> However, buried deep in the regex(3) page is a reference to re_format(7)
> (not even boldface).  And re_format(7) has the definitions, but getting
> there takes perseverance.  (For the record, grep(1) does point straight
> to re_format(7).)
>
> So you see, bash(1) *cannot* just link directly to regex(7), because
> that's not actually the correct final destination on most operating
> systems.  It's only correct on Linux.  Bash uses the regex(3) library
> interface, so that is the correct place for bash to refer the reader.

Therefore, either bash manpage should specify clearly which regex
manpage it should be in each system (which a bad choice, because there
can be a large number of systems), or the bash manpage should omit all
the non consistent reference and say something like "see more details
in info" or something else that is platform independent. Referring to
regex(3) without any quantification is not a very good choice .

-- 
Regards,
Peng



Re: How to match regex in bash? (any character)

2011-09-29 Thread Peng Yu
On Thu, Sep 29, 2011 at 10:38 AM, Chet Ramey  wrote:
> On 9/29/11 9:48 AM, Peng Yu wrote:
>
>> Therefore, either bash manpage should specify clearly which regex
>> manpage it should be in each system (which a bad choice, because there
>> can be a large number of systems), or the bash manpage should omit all
>> the non consistent reference and say something like "see more details
>> in info" or something else that is platform independent. Referring to
>> regex(3) without any quantification is not a very good choice .
>
> Why, exactly?  regex(3) is the one thing that's portable across systems,
> it happens to describe the interfaces bash uses, and it contains the
> appropriate system-specific references.  `info' is considerably less
> portable and widespread than `man', so a man page reference is the best
> choice.

We all have discovered that regex(3) is not consistent across all the
platform. Why you say it is portable?

As I mentioned previously, the best is to add a few examples in man
bash. Based on the assumption that you don't want to add an example in
man bash, then the next choice to add a reference to info, even though
it may not always be available in all the system (but as least it
should be downloadable from bash gnu website).

-- 
Regards,
Peng



Re: How to match regex in bash? (any character)

2011-09-29 Thread Peng Yu
On Thu, Sep 29, 2011 at 11:06 AM, Greg Wooledge  wrote:
> On Thu, Sep 29, 2011 at 10:59:19AM -0500, Peng Yu wrote:
>> We all have discovered that regex(3) is not consistent across all the
>> platform. Why you say it is portable?
>
> The three systems I mentioned earlier today all have regex(3).  Which
> system have you found, which doesn't have it?

I think that I misunderstood some of the previous emails.

However, on ubuntu, there is regex(3) and regex(7). Based on the
context in man bash, regex(7) is more relevant than regex(3), although
regex(3) does mention extend regular expression, it is more of a
document for the C interface.

"When it is used, the string to the right of the operator is
considered an extended regular expression and matched accordingly (as
in regex(3))"

Also, regex(3) does not mention the difference between $x =~ .txt
and $x=~ ".txt". I think that the difference should be addressed
in man bash.

Bottom line, regex(3) is not a good manpage to refer in the above
sentence. It is better to think of other alternative rather than
trying to justify we should stuck with it.

>> As I mentioned previously, the best is to add a few examples in man
>> bash.
>
> I would not object to that, but I can't speak for Chet.
>
> Another option would be to refer to the POSIX definition of
> Extended Regular Expressions as a web site.  I wish they had
> better URLs, though.  The URL I have for it at the moment is
> http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04
>



-- 
Regards,
Peng



How to automatically load alias from .bashrc in a bash script?

2011-10-27 Thread Peng Yu
Hi,

I want to use some alias in a bash script. But I have to include the
following line in the script in order to use the alias defined in
~/.bashrc. Is there a way to automatically load the alias from .bashrc
so that I don't have to explicitly include these two lines?

shopt -s expand_aliases
. ~/.bashrc


-- 
Regards,
Peng



Re: How to automatically load alias from .bashrc in a bash script?

2011-10-27 Thread Peng Yu
On Thu, Oct 27, 2011 at 11:43 AM, Greg Wooledge  wrote:
> On Thu, Oct 27, 2011 at 04:39:23PM +, Stephane CHAZELAS wrote:
>> You mean you'd like every bash script you run on your system to
>> automatically source your ~/.bashrc.
>>
>> That sounds like a very unwise thing to do to me,  but that
>> could be done with:
>>
>> export BASH_ENV="$HOME/.bashrc"
>
> He'd still have to turn on the expand_aliases shell option in each
> script, though.  Unless he puts it in .bashrc.
>
> (Might as well hand him enough rope to hang himself.)
>
> If you want to do things properly, use functions instead of aliases.
> They are more powerful and more flexible.

Thanks for the input. I have some somecommonname.sh, which may be
conflict with other commands. Therefore, I only need to rename them to
avoid name conflict. But I'm not using any arguments. I think that
creating symbolic link rather than alias probably is a better
solution.


-- 
Regards,
Peng



bash-completion between do and done

2011-11-04 Thread Peng Yu
Hi,

Current, bash doesn't do command completion between do and done (for loop).
I'm wondering if this feature can be added.

-- 
Regards,
Peng


What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Peng Yu
Hi,

Suppose that I have a verbatim string "  a b c ( a'b | " in bash, and
I want to pass them as 6 command line arguments. I have to the
following conversion using quoteverb.sed to pass the 6 arguments
correctly to echo, which is a little bit cumbersome. I'm wondering if
there is any better way to pass the 6 arguments.

~$ cat quoteverb.sed
1s/^\s+//g
s/^\s\+//g
s/\s\+$//g
s/\s\+/\n/g
s/'/'\\''/g
s/^/'/gm
s/$/'/gm
s/\n/ /g
~$ cat main.sh
#!/usr/bin/env bash

verbatim_string="  a b c ( a'b | "

args="`echo \"$verbatim_string\" | sed -f quoteverb.sed`"

cmd="echo $args"
eval "$cmd"

~$ ./main.sh
a b c ( a'b |

-- 
Regards,
Peng



Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Peng Yu
Hi Clark,

>> > v="  a b c ( a'b | "
>> > a=( $v )
>> > echo "${a[@]}"
>
> There's a @ char here.

I see. It's my mistake.

But I want to pass the 6 short arguments instead of 1 long argument to
echo. (echo is just an example, it can be any command that accepts
multiple arguments.)


~$ cat ./main1.sh
#!/usr/bin/env bash

#set -o noglob
verbatim_string="  a b c ( a'b | "

args=( $verbatim_string )
#set +o noglob

echo "${args[@]}"

~$  ./main1.sh
a b c ( a'b |


-- 
Regards,
Peng



Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-07 Thread Peng Yu
Hi Clark,

> What do you mean by "1 long argument"?
>
> [bash-4.2.10] # cat foo.sh
> v="  a b c ( a'b | "
> set -o noglob
> a=( $v )
> set +o noglob
> for i in "${a[@]}"; do
>     echo "$i"
> done
> [bash-4.2.10] # bash foo.sh
> a
> b
> c
> (
> a'b
> |
> [bash-4.2.10] #


I misunderstood the usage of "${args[@]}". I though it returns only
one long argument "  a b c ( a'b | ", but it actually expanded to 6
short arguments "a", "b", "c", "(", "a'b" and  "|". Thanks for
clarification.



-- 
Regards,
Peng



Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-07 Thread Peng Yu
On Mon, Nov 7, 2011 at 8:29 AM, Dennis Williamson
 wrote:
> On Mon, Nov 7, 2011 at 7:23 AM, Peng Yu  wrote:
>> Hi Clark,
>>
>>> What do you mean by "1 long argument"?
>>>
>>> [bash-4.2.10] # cat foo.sh
>>> v="  a b c ( a'b | "
>>> set -o noglob
>>> a=( $v )
>>> set +o noglob
>>> for i in "${a[@]}"; do
>>>     echo "$i"
>>> done
>>> [bash-4.2.10] # bash foo.sh
>>> a
>>> b
>>> c
>>> (
>>> a'b
>>> |
>>> [bash-4.2.10] #
>>
>>
>> I misunderstood the usage of "${args[@]}". I though it returns only
>> one long argument "  a b c ( a'b | ", but it actually expanded to 6
>> short arguments "a", "b", "c", "(", "a'b" and  "|". Thanks for
>> clarification.
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>>
>
> If you use "${args[*]}" (with quotes and an asterisk instead of an at
> sign), the result is one long argument. Otherwise, it's split.

Thanks! I didn't notice this difference before.


-- 
Regards,
Peng



What is the correct way to set up login environment in crontab?

2011-11-08 Thread Peng Yu
Hi,

I need to use cron to run some job. I know that cron only set up very
basic environment. I'd like to duplicate my login environment. But
some environment variables are still not seen when I set the following
crontab entry. Does anybody know how to correctly set up the login
enviroment? (The manual describes what files are sourced, but I don't
want to manually source these files. I'm looking for a simpler way.)

* * * * * /bin/bash -i -l -c '/path/programtorun.sh > /tmp/my.log'


-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 7:45 AM, Greg Wooledge  wrote:
> On Tue, Nov 08, 2011 at 09:46:37PM -0600, Peng Yu wrote:
>> I need to use cron to run some job. I know that cron only set up very
>> basic environment. I'd like to duplicate my login environment.
>
> Just source /etc/profile and your ~/.bash_profile or ~/.profile (or
> whatever) from the script that your cron job executes.
>
> Personally I would advise against this.  Login environments are for
> interactive logins, not cron jobs.

So neither -i nor -l is necessary?

-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 7:45 AM, Greg Wooledge  wrote:
> On Tue, Nov 08, 2011 at 09:46:37PM -0600, Peng Yu wrote:
>> I need to use cron to run some job. I know that cron only set up very
>> basic environment. I'd like to duplicate my login environment.
>
> Just source /etc/profile and your ~/.bash_profile or ~/.profile (or
> whatever) from the script that your cron job executes.
>
> Personally I would advise against this.  Login environments are for
> interactive logins, not cron jobs.

I sourced my ~/.bashrc, which source some other files. It seems the
environment variables defined in these files are not seen with env.
Why is so?


-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 10:41 AM, Greg Wooledge  wrote:
> On Wed, Nov 09, 2011 at 10:29:52AM -0600, Peng Yu wrote:
>> I sourced my ~/.bashrc, which source some other files. It seems the
>> environment variables defined in these files are not seen with env.
>> Why is so?
>
> Without seeing the code?  Impossible to say.  But you're doing it backwards.
> ~/.bashrc should be sourced FROM ~/.bash_profile.  A login shell reads
> ~/.bash_profile only, so it's the responsibility of ~/.bash_profile to
> read ~/.bashrc to set up aliases, functions, shopts, and other ephemeral
> shell settings that can't be inherited from the environment.

Sorry for the confusion. ~/.bash_profile is not the problem here. I
have the following line in /path/programtorun.sh

. ~/.bashrc

In ~/.bashrc, I have ". ~/bash_some". In ~/.bash_some, I have some
variable assignment VAR=blah.

However, VAR is not seen in env in /path/programtorun.sh (called from cron).

-- 
Regards,
Peng



Customize the command resolution in bash?

2011-11-11 Thread Peng Yu
Hi,

bash by default searchs in paths specified in the environment variable
PATH (separated by ":"). I'm not aware if there is any cache mechanism
to save the run time (but even so, different terminals still can not
see the same cache, hence each terminal has the overhead to create the
cache). When there are many files in PATH, it is going to slow down
the performance.

One simple remedy is to instead search in a file where the abspaths of
all the commands are saved (of course, this database file can be
generated by using the command 'find' to search for all the
directories in $PATH, which process can be scheduled to run
periodically using cron). To make this work, I'm wondering if there is
an easy way to customize the way that bash resolve a command.

-- 
Regards,
Peng



invoke tilde expansion on quoted string

2011-11-11 Thread Peng Yu
Hi,

I know from the document that tilde expansion only works if the string
is unquoted (see below)

~$ cd '~/..'
-bash: cd: ~/..: No such file or directory
~$ cd ~/..
/Users$

I'm wondering if I already have a string variable, is there a bash
native to do tilde expansion on it.

var='~/..'
cd $var#how to change this line?

-- 
Regards,
Peng



Why complete doesn't print anything if it is called in a bash script?

2011-11-12 Thread Peng Yu
Hi,

It is strange to me why complete doesn't print anything when it is
called in a bash script. I must have misunderstood some fundamentals.
Does anybody know why? Thanks!

~$ cat main.sh
#!/usr/bin/env bash

. ~/.bashrc

complete

~$ ./main.sh
~$ complete |head
complete -F _kill kill
complete -F _renice renice
complete -F _smbpasswd smbpasswd
complete -F _postconf postconf
complete -F _ldapwhoami ldapwhoami
complete -F _ldapaddmodify ldapadd
complete -F _launchctl launchctl
complete -F _java java
complete -F _stream stream
complete -F _filedir_xspec oodraw


-- 
Regards,
Peng



Re: Why complete doesn't print anything if it is called in a bash script?

2011-11-12 Thread Peng Yu
On Sat, Nov 12, 2011 at 10:01 AM, Andreas Schwab  wrote:
> Peng Yu  writes:
>
>> It is strange to me why complete doesn't print anything when it is
>> called in a bash script. I must have misunderstood some fundamentals.
>> Does anybody know why? Thanks!
>
> If complete does not print anything then there are no completions
> defined.

The question is why. I have source my bashrc, as you can see there are
completions defined in the interactive shell.

-- 
Regards,
Peng



Re: Why complete doesn't print anything if it is called in a bash script?

2011-11-12 Thread Peng Yu
On Sat, Nov 12, 2011 at 10:18 AM, Chet Ramey  wrote:
> On 11/12/11 10:41 AM, Peng Yu wrote:
>> Hi,
>>
>> It is strange to me why complete doesn't print anything when it is
>> called in a bash script. I must have misunderstood some fundamentals.
>> Does anybody know why? Thanks!
>
> Since complete happily shows completions when run from a shell script,
> there must be code in your bashrc that prevents them from being
> defined if the shell is not interactive.

Thank you for reminding me! That's indeed the case (shown below is
from the bashrc file).

case $- in
  *i*) [[ -f /opt/local/etc/bash_completion ]] && .
/opt/local/etc/bash_completion ;;
esac

-- 
Regards,
Peng



converting array to string by quoting each element for eval

2011-11-15 Thread Peng Yu
Hi,

I find that I have to make a program quotearg.sh to convert an array
to a string by quoting each element. So that it be used for eval.

I'm not sure if there is a way that I can do eval in bash without
having to use quotearg.sh. If there is no such a way, should
quotearg.sh be added in bash (if it is not available in bash yet), as
it provides a fundamental functionality?


~/linux/bin/src/bash/quotearg/main$ cat ./main.sh
#!/usr/bin/env bash

../quotearg.sh a b c
../quotearg.sh "'" ' ' '"'
../quotearg.sh 'a' 'a b'

echo 

args=('a' 'a b')
cmd="printf 'x%sx\n' ${args[@]}"
eval "$cmd"

echo #the following is what I want, the above is not.

args=('a' 'a b')
arg_string=`../quotearg.sh "${args[@]}"`
cmd="printf 'x%sx\n' $arg_string"
eval "$cmd"

~/linux/bin/src/bash/quotearg/main$  ./main.sh
'a' 'b' 'c'
''\''' ' ' '"'
'a' 'a b'

xax
xax
xbx

xax
xa bx



-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-15 Thread Peng Yu
>   Why not use the array instead of making it into a single string?
>
> $cmd "${args[@]}"
>
>   Why are you using eval or quotearg.sh? It sounds as if you are
>   making the process more complicated than it need be.

For the examples that I gave, probably it is not necessary.

I'm yet to make a concrete complex example to demonstrate its
usefulness in practice. But I think that it is easy to imagine that
there will be some complex commands that are composed by concatenating
multiple strings to be evaled, in which case this may be useful.

-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-15 Thread Peng Yu
>    In any case, combining a command and its arguments in a single
>    string is almost always the wrong way to go about it.

Please compare the two scripts and see if the second one makes more sense.

/tmp$ cat not_convert_args_to_string.sh
#!/bin/bash

options="$2"
find $1 $options
echo find $1 $options
/tmp$ cat convert_args_to_string.sh
#!/bin/bash

options="$2"
cmd="find $1 $options"
eval "$cmd"
echo $cmd
/tmp$ ./not_convert_args_to_string.sh . "-type f -name '*'"
find: `./cvcd': Permission denied
find . -type f -name '*'
/tmp$ ./convert_args_to_string.sh . "-type f -name '*'"|head
find: `./cvcd': Permission denied
./.main.sh.swp
./0001e4ecf6175
./alm.log
./amt.log
./convert_args_to_string.sh
./FLEXnet/11.5.0.0 build 56285Macrovision
./FLEXnet/524288-16F7558F-328B-4dc3-BEDF-095C1F14FFF1
./FLEXnet/524288-34E9EE98-50ED-4c6c-BD0F-F539123FD064
./FLEXnet/524288-85A0F138-527D-4012-8175-79A3AEA4152E
./FLEXnet/freCqflgCxFrwiBvBiCadibCwg



-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-15 Thread Peng Yu
On Tue, Nov 15, 2011 at 6:43 PM, Chris F.A. Johnson
 wrote:
> On Tue, 15 Nov 2011, Peng Yu wrote:
>
>>>    In any case, combining a command and its arguments in a single
>>>    string is almost always the wrong way to go about it.
>>
>> Please compare the two scripts and see if the second one makes more sense.
>>
>> /tmp$ cat not_convert_args_to_string.sh
>> #!/bin/bash
>>
>> options="$2"
>> find $1 $options
>> echo find $1 $options
>
>  More sensible would be to have each option a separate argument and
>  do:
>
> location=$1
> shift
> find "$location" "$@"

No. My real example use getopt. If I have each option in a separate
argument, I need to know all the possible arguments to find, which is
not a viable route.

>> /tmp$ cat convert_args_to_string.sh
>> #!/bin/bash
>>
>> options="$2"
>> cmd="find $1 $options"
>> eval "$cmd"
>> echo $cmd
>
>   See above.
>
>> /tmp$ ./not_convert_args_to_string.sh . "-type f -name '*'"
>> find: `./cvcd': Permission denied
>> find . -type f -name '*'
>
>   Use 'set -x' to see exactly what your script is doing.

How to pass the option "-type f -name '*'" correctly?

/tmp$ ./not_convert_args_to_string.sh . "-type f -name '*'"
+ options='-type f -name '\''*'\'''
+ find . -type f -name ''\''*'\'''
find: `./cvcd': Permission denied
+ echo find . -type f -name ''\''*'\'''
find . -type f -name '*'

-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-15 Thread Peng Yu
>  Then why don't you post that?

Please take a look and see if you have a better solution than the
following code without using quotearg.sh.

/tmp/tmp$ ll *
c d:
total 0
-rw-r--r-- 1 pengy wheel 0 2011-11-15 20:18:11 xx

a b:
total 0
-rw-r--r-- 1 pengy wheel 0 2011-11-15 20:18:15 yy
/tmp/tmp$ ../convert_args_to_string.sh 'a b' 'c d' -O "-type f -name '*'"
TEMP= -O '-type f -name '\''*'\''' -- 'a b' 'c d'
a b/yy
c d/xx
find 'a b' 'c d' -type f -name '*'
/tmp/tmp$ cat ../convert_args_to_string.sh
#!/bin/bash

TEMP=`getopt -o O: --long options: -n "${script_name}.sh" -- "$@"`

echo TEMP=$TEMP

if [ $? != 0 ] ; then printf "Terminating...\n" >&2 ; exit 1 ; fi

eval set -- "$TEMP"

abspath_script=`readlink -f -e "$0"`
script_absdir=`dirname "$abspath_script"`

while true ; do
  case "$1" in
-O|--options)
  options="$2"
  shift 2
  ;;
--)
  shift
  break
  ;;
*)
  echo "Internal error!"
  exit 1
  ;;
  esac
done

arg_string=`quotearg.sh "$@"`

cmd="find $arg_string $options"
eval "$cmd"
echo $cmd



-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-16 Thread Peng Yu
Hi Greg,

> **NEVER** use getopt(1).  It is broken.  It cannot be made to work
> correctly.  Its entire design is flawed.

I don't see these warnings in my systems (macports and ubuntu) (This
is version of getopt on macports and ubuntu is free, I don't see there
is a reason that getopt can not be ported to the two systems that you
mentioned). All I see that is relevant is the following. I don't think
that just because that it has a BUGS section in the manpage, it can be
called broken. man bash also has BUGS section, is bash considered as
broken?

BUGS
   getopt(3) can parse long options with optional arguments that
are given an empty optional argument (but can not do this for short
options). This  getopt(1)  treats
   optional arguments that are empty as if they were not present.

   The syntax if you do not want any short option variables at all
is not very intuitive (you have to set them explicitely to the empty
string).

AUTHOR
   Frodo Looijaard 


Note that it doesn't mean that I am resistant to getopts. It is just
that I don't think that your logic is valid. But I will read more
about getopts to see if it is necessary to convert from getopt to
getopts.

> ./myscript . -type f -name '*'

To support your claim, tell me what myscript would be if the commands

./myscript 'a b' 'c d' -type f -name '*"
./myscript 'a b' -type f -name '*"

actually do

find 'a b' 'a d' -maxdepth 1 -type f -name '*"
find 'a b' -maxdepth 1 -type f -name '*"

-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-16 Thread Peng Yu
> And that is enough of this nonsense.  I have cited three official manuals
> for you already.  Let's move on.

I don't get it. Do you mean both traditional getopt and Debian getopt
are broken. To me it seems that Debian getopt is made to address the
short coming of transitional getopt. Yet you still think Debian getopt
is broken?

> Now that we know the goal, it's simple enough:

No. That is not my goal. It is just a simplification of my goal. It is
really hard to enumerate all the possible use cases, but I will try.

In additional to the use cases I stated in the previous email, let's
consider some more:

 ./myscript 'a b' 'c d' -o output.txt -type f -name '*'

or

 ./myscript 'a b' 'c d' -output output.txt -type f -name '*'

or

 ./myscript -output output.txt -type f -name '*' 'a b' 'c d'

or

 ./myscript -type f -name '*' 'a b' -output output.txt  'c d'

is equivalent

 find 'a b' 'a d' -maxdepth 1 -type f -name '*' > output.txt


-- 
Regards,
Peng



Re: converting array to string by quoting each element for eval

2011-11-16 Thread Peng Yu
> You may safely use getopts (the builtin).  Never getopt.

If my understanding is correct, 'getopts' doesn't support the long
format. Hence, it does not satisfy my need and I shall not use it.

-- 
Regards,
Peng



How to directly modify $@?

2011-11-20 Thread Peng Yu
Hi,

I don't see if there is a way to directly modify $@. I know 'shift'.
But I'm wondering if there is any other way to modify $@.

~$ 1=x
-bash: 1=x: command not found
~$ @=(a b c)
-bash: syntax error near unexpected token `a'

-- 
Regards,
Peng



How to protect > and interpret it later on? (w/o using eval)

2011-12-01 Thread Peng Yu
Hi,

~$ cat ../execute.sh
#!/usr/bin/env bash

echo "$@"
"$@"

$  ../execute.sh  ls >/tmp/tmp.txt
$ cat /tmp/tmp.txt #I don't want "ls" be in the file
ls
main.sh

'>' will not work unless eval is used in execute.sh.

$ ../execute.sh  ls '>' /tmp/tmp.txt
ls > /tmp/tmp.txt
ls: cannot access >: No such file or directory
/tmp/tmp.txt

How to make execute protect > and interpret it later on w/o using eval?

-- 
Regards,
Peng



Re: How to protect > and interpret it later on? (w/o using eval)

2011-12-02 Thread Peng Yu
> WHAT are you trying to DO?

I think that you might completely miss my point. I try to explain it
better. Let me know if this time it makes more sense to you.

I want to execute any command as if the 'execute.sh' does not present,
except that I want to print the command so that I know want the
command is executed. (This can be used when I call several commands in
a script and I know what part of the output associated to what
command). E.g. I can run

ls > /tmp/tmp.txt

When I call,

execute.sh ls > /tmp/tmp.txt

I want it actually to do

echo "ls > /tmp/tmp.txt"
ls > /tmp/tmp.txt


Note that I could define execute.sh such that

execute.sh "ls > /tmp/tmp.txt"

means

echo "ls > /tmp/tmp.txt"
eval "ls > /tmp/tmp.txt"

But this interface of execute.sh is not as good the previous one.

Note that there could be other symbols that bash normal process, such as '2>&1'.

I'm looking for a general solution, Pierre's answer is not as general as I want.

The FAQ http://mywiki.wooledge.org/BashFAQ/050 doesn't really answer
my question.

BTW, where is the help-bash mailing list mentioned (at least not on
bash home page)? I have never seen it before.

http://tiswww.case.edu/php/chet/bash/bashtop.html

-- 
Regards,
Peng



Re: How to protect > and interpret it later on? (w/o using eval)

2011-12-03 Thread Peng Yu
> THAT will work.  But why are you writing a script to read a shell command
> and then execute it?  There is already a program that reads shell commands

This capability will be useful for debugging bash script.

For example, I have a set of commands in a bash script, each of them
output some thing to stdout. However, I can not tell the start and end
of each output. I could have a command that does what I expect
execute.sh to do.

cmd1
cmd2
...

I can easily append each command with 'execute.sh', which will not
only tell me the boundary of each output but also help me remember
what command was executed.

execute.sh cmd1
execute.sh cmd2
...

Since what I expect is not possible, the next solution is to use the
eval version which requires to put quote around cmd1, cmd2... But this
is very annoying, when cmd1, cmd2 themselves have the quotation marks,
I have to escape these marks.

So neither solution to execute.sh is ideal, although any of them is
better than the other in certain situations.

-- 
Regards,
Peng



Ill positioned 'until' keyword

2011-12-14 Thread Peng Yu
Hi,

I looks a little wired why 'until' is the way it is now. According to
the manual until is before the do-done block.

until test-commands; do consequent-commands; done

A common design of until in other language is that it allows the loop
body be executed at least once and test the condition at the end of
the run of the first time. It seems that a better of bash should also
follow the practice. If I understand it correctly, the above is exact
the same as the following, in which case the do done block can be
executed zero time. Because of this, I think that the current 'until'
is not necessary, and probably better to change its definition so that
it allows the execution of the loop at least once.

while ! test-commands; do consequent-commands; done


In short, I'd expect the following code echo 9 (not working with the
current bash).

COUNTER=9
do
 echo COUNTER $COUNTER
 let COUNTER-=1
done
until [  $COUNTER -lt 10 ];


I'd like to hear for what reason 'until' is designed in the way it is
now. Shall we considered to at least allow an option in bash to change
it meaning to the one I propose (or adding a different command, like
until2, for what I proposed), which give us time to let the orignal
until usage dies out.

-- 
Regards,
Peng



Re: [Help-bash] How to keep only files match the prefix when do command completion?

2011-12-15 Thread Peng Yu
Hi Greg,

> New users do not mess with programmable completion.

Given the context, I though that it was clear that "new users" means
users new to command completion. If it was not clear, I make it
explicit  here.

-- 
Regards,
Peng



Is the description of set -- missing in man bash or at least difficult to find?

2011-12-22 Thread Peng Yu
Hi,

As I mentioned previously, there are shortcomings in man bash. Here, I
just point another example. And I hope my suggestion will be
addressed.

As a reasonable search strategy to search for how to set $@ is to
search for '$@' in man bash. The literal word '$@' appears at the
following locations.

...performed,  with the exception of "$@" as explained below under Special...
...expands to a separate word.  That is, "$@" is equivalent to "$1"...
...When there are no positional parameters, "$@" and $@  expand  to...
...of "$@" and "${name[@]}" as explained above (see PARAMETERS)...

Search for 'set --' returns nothing. And search for '--' is prohibited
as there are too many of them. If the manual discusses how to 'set
$@', then it is at least hard to find. My suggestion is to add a new
paragraph to the existing discussion of $@, so that by searching $@,
readers can easily see how set $@.

   @  Expands  to  the positional parameters, starting from one.  When
  the  expansion  occurs  within  double  quotes,  each  parameter
  expands to a separate word.  That is, "$@" is equivalent to "$1"
  "$2" ...  If the double-quoted expansion occurs within  a  word,
  the  expansion  of the first parameter is joined with the begin-
  ning part of the original word, and the expansion  of  the  last
  parameter  is  joined  with  the last part of the original word.
  When there are no positional parameters, "$@" and $@  expand  to
  nothing (i.e., they are removed).

As others pointed out LDP/abs is a more readable document, the
following link is a much better document on helping me to find how to
set "$@".

http://tldp.org/LDP/abs/html/internalvariables.html

-- 
Regards,
Peng



Re: Is the description of set -- missing in man bash or at least difficult to find?

2011-12-22 Thread Peng Yu
> Second, just search for the 'set' builtin, near the bottom of the man page.

Thank for clarifying the usage of set.

I looked closely to the document of set. I just find another problem,
it says the following. However, the description of -- way down below.
It should be the option be described. A rule of thumb is that the
order of the description of the options should be the same as the
order in the following line.

 set [--abefhkmnptuvxBCEHPT] [-o option-name] [arg ...]

But my main point is if a person only knows the keyword $@, he will
not be able to find how to set it in the manual. The description of
'set --' should also be added to the description of $@ to explain how
to set $@.

Also, "If no arguments follow this option, then the positional
parameters are unset.  Otherwise, the positional parameters are set to
the args, even if some of them begin with a -."
should be rewritten as
"If no arguments follow this option, then the positional parameters $@
are unset.  Otherwise, the positional parameters $@ are set to the
args, even if some of them begin with a -."
so that $@ is searchable.

-- 
Regards,
Peng



Re: Is the description of set -- missing in man bash or at least difficult to find?

2011-12-22 Thread Peng Yu
> +1 vote on getting the parameters listed with a leading dollar sign.
> The individual single character is difficult to search for but the
> combination of "$@" and so forth for the others is a useful search
> string.  I have often wanted the manual to include the "$@"
> combination instead of just the "@" name.

I agree $@ is better than @.

But not to repeat myself again, did you get my major point that I have
restated to DJ Mills?

-- 
Regards,
Peng



Re: Is the description of set -- missing in man bash or at least difficult to find?

2011-12-22 Thread Peng Yu
> There are shortcomings in _the man documentation format_ and one of them
> is that it doesn't work (at least for me...) when the documentation is
> longer than one screen or thereabouts. I've pretty much come to the
> conclusion that any man page that is over a couple of hundred lines is
> a waste of my time and should probably not even exist in the first
> place.

Hi CJ,

You didn't get my point. Please see my reply to DJ Mills, if my first
email is not clear enough to you.

The shortcomings is not in the man format. Even if it is in the man
format, it can be compensated in the way that I mentioned.

-- 
Regards,
Peng



Specify completion without name

2012-01-04 Thread Peng Yu
Hi,

I want to customize the command completion for completing executables,
I want to search in a file (which includes all the executables in
PATH) rather than the default PATH variable. But I don't see how to do
so, as the following help indicates that it can only configure how to
complete the arguments of a command. Does anybody know how to
configure command completion for the name itself rather than its
arguments? Thanks!

~/Downloads$ help complete
complete: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G
globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F
function] [-C command] [name ...]
For each NAME, specify how arguments are to be completed.
If the -p option is supplied, or if no options are supplied, existing
completion specifications are printed in a way that allows them to be
reused as input.  The -r option removes a completion specification for
each NAME, or, if no NAMEs are supplied, all completion specifications.

-- 
Regards,
Peng



Re: Specify completion without name

2012-01-04 Thread Peng Yu
> empty lines.  There is no programmable completion mechanism to complete
> on non-empty command names.

I'm wondering if it is worthwhile to add such a feature. I have run
into the problem that it is very slow to command complete an
incomplete command especially when other programs are accessing the
disk (note that I frequently open new terminals, so the caching done
within a bash process does not help me much).

If I can configure how to complete on non-empty command names, I could
just check a file with all the commands in PATH are stored. By
checking just a single file, presumable the search can be much faster
than searching many directories.

-- 
Regards,
Peng


-- 
Regards,
Peng



Re: Specify completion without name

2012-01-05 Thread Peng Yu
> Presumably you would also include aliases, shell builtins, and functions
> in this file.

Yes. I just want to replace executables in PATH by the result from my
custom function. I think that aliases, builtins, and functions are all
in the memory of bash already, so it doesn't take extra time to search
them from bash. Therefore, the ideal behaviors is that bash still
search for aliases, builtins and functions, and in addition check the
results returned by my custom function. Hence, my custom function
should not return anything about them (this should be expect at least
by default).

-- 
Regards,
Peng



Re: Specify completion without name

2012-01-05 Thread Peng Yu
> I would envision that such a completion function would assemble its list
> of possible completions by using your read-from-a-file mechanism and
> augment the list using compgen -a/compgen -b/compgen -A function.  It
> would probably also want to handle glob patterns and expand them to
> potentially multiple completions, but that gets tricky.

I did not know that it is so simple to get the alias (compgen -a),
buildins (compgen -b) and functions (compgen -A function) as you
mentioned. Once I know these, I agree with you that bash need not
handle these internally, rather user can call these three functions
directly. But beware to clearly document these by giving working
EXAMPLE code which include these three commands (not just text
explanation without working code, by "working code" I mean code
snippet is discouraged, a complete completion function should be
provided).

BTW, as I mentioned several times the bash man favors document
maintainer rather readers. For example, the following help doesn't
help me much when I want to learn how to use compgen.

~$ help compgen
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat]
[-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C
command] [word]
Display the possible completions depending on the options.  Intended
to be used from within a shell function generating possible completions.
If the optional WORD argument is supplied, matches against WORD are
generated.

The manpage also use a reference rather than list all the options
directly. Readers have to jump to complete to understand how to use
compgen. This is also inconvenient to users.

  Generate possible completion matches for word according  to  the
  options,  which  may  be  any  option  accepted  by the complete
  builtin


If you consider it repetitive to discuss the same option twice in both
compgen and complete, at least, you can expand "help compgen" to
describe all the options (merge the current description of compgen and
complete in man). Other help messages are so concise that they are not
very helpful for learning how to use them. I'd suggest change all of
them as well.

-- 
Regards,
Peng



Re: Specify completion without name

2012-01-05 Thread Peng Yu
> The bash man page already has ~70 pages manual. I don't like it to grow to
> ~700 pages (like the ABS Guide) with all the working examples you expected.
> :)

Do you use search at all? :) If you use search, it doesn't really
matter if is a 700 page manual.

-- 
Regards,
Peng



cd won't change the prompt if the command is typed in the vi editor (vi mode).

2013-05-12 Thread Peng Yu
Hi,

I have vi mode set.

set -o vi.

Then I type v to enter the vi editor. In the vi editor, I type 'cd
/tmp'. Then, I get the following screen output. Notice that the prompt
does not change immediately after the cd command. Could anybody
reproduce this behavior? Thanks.

~$
cd /tmp
~$ echo $PWD
/tmp
/tmp$

-- 
Regards,
Peng



"echo" does not follow the "getopt" convention

2014-08-19 Thread Peng Yu
Hi,

The following shows that echo does not following the "getopt"
convection. Is it better to make all bash internal command following
the getopt convention?

~$ echo -n
~$ echo -n -n
~$ echo -n -- -n # I think that the output should just be "-n" not "-- -n".
-- -n~$

http://software.frodo.looijaard.name/getopt/

-- 
Regards,

Peng Yu, Ph.D.
Assistant Professor
Dept. Electrical and Computer Engineering & TEES-AgriLife Center for
Bioinformatics and Genomic Systems Engineering (CBGSE)
Texas A&M University
Office: 215F WEB
Phone: (979) 320-9822
Email: pe...@tamu.edu
http://yubiolab.wordpress.com



How to change the command completion behavior in bash

2009-11-15 Thread Peng Yu
If I have the following in the command line,

~/.bash

when I type , it will become /home/my_user_name/.bash

I'm wondering if it is possible to configure bash command completion,
so that it will still be '~/.bash'




Re: How to change the command completion behavior in bash

2009-11-17 Thread Peng Yu
On Mon, Nov 16, 2009 at 8:30 AM, Chet Ramey  wrote:
>> If I have the following in the command line,
>>
>> ~/.bash
>>
>> when I type , it will become /home/my_user_name/.bash
>>
>> I'm wondering if it is possible to configure bash command completion,
>> so that it will still be '~/.bash'
>
> You don't say what version of bash you're using, but bash-3.2 and bash-4.0
> both preserve the tilde by default.


Here are my bash version and /etc/inputrc. I don't find where it
changes the default behavior

$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

$ cat /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, comment out
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion
# set bell-style none
# set bell-style visible

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# allow the use of the Delete/Insert keys
"\e[3~": delete-char
"\e[2~": quoted-insert

# mappings for "page up" and "page down" to step to the beginning/end
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history

# alternate mappings for "page up" and "page down" to search the history
# "\e[5~": history-search-backward
# "\e[6~": history-search-forward

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

$if term=rxvt
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word
$endif

# for non RH/Debian xterm, can't hurt for RH/Debian xterm
# "\eOH": beginning-of-line
# "\eOF": end-of-line

# for freebsd console
# "\e[H": beginning-of-line
# "\e[F": end-of-line

$endif

$ cat ~/.inputrc
set editing-mode vi
set bell-style none
"\e[A": history-search-backward  ## up-arrow




Re: How to change the command completion behavior in bash

2009-11-17 Thread Peng Yu
On Mon, Nov 16, 2009 at 4:50 AM, Henning Garus
 wrote:
> On Sun, Nov 15, 2009 at 08:31:38PM -0600, Peng Yu wrote:
>> If I have the following in the command line,
>>
>> ~/.bash
>>
>> when I type , it will become /home/my_user_name/.bash
>>
>> I'm wondering if it is possible to configure bash command completion,
>> so that it will still be '~/.bash'
>>
>
> Add
>
> set expand-tilde off
>
> to your ~/.inputrc . However off is the default value, so you could
> try to find out where it is set to on and remove that.
>
> See "Readline Variables" in man bash.

I add

set expand-tilde off

to my ~/.inputrc. Then I login again.

Then I type the following after bash prompt

cd ~

Later, if I type  after ~, ~ still expands to the abspath of my
home directory. I'm confused why 'set expand-tilde off' does not
change the behavior of bash.

Regards,
Peng




Re: How to change the command completion behavior in bash

2009-11-17 Thread Peng Yu
On Tue, Nov 17, 2009 at 1:15 PM, Chet Ramey  wrote:
> Peng Yu wrote:
>> On Mon, Nov 16, 2009 at 8:30 AM, Chet Ramey  wrote:
>>>> If I have the following in the command line,
>>>>
>>>> ~/.bash
>>>>
>>>> when I type , it will become /home/my_user_name/.bash
>>>>
>>>> I'm wondering if it is possible to configure bash command completion,
>>>> so that it will still be '~/.bash'
>>> You don't say what version of bash you're using, but bash-3.2 and bash-4.0
>>> both preserve the tilde by default.
>
> I think I may have been confused.  If you're not doing command completion
> with the tilde-prefixed string (that is, if it's an argument to another
> command already on the line), you're probably using the
> bash_completion set of programmable completions, and the completion
> specification for the command to which this is an argument is probably
> expanding the tilde.
>
> I assumed you were trying to complete the above string as a command, not
> an argument to a command.
>
> What is the full command line on which you're attempting completion?

I'm trying to complete an argument rather than a command as I just
replied in the previous message.




What files are source in bash login shell?

2009-11-17 Thread Peng Yu
I make my ~/.bash_profile empty. I still see

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

I checked /etc/profile and /etc/bash.bashrc. But I don't PATH is set
in the two files. I'm wondering from which file PATH is set. BTW, my
system is ubuntu.




Re: What files are source in bash login shell?

2009-11-17 Thread Peng Yu
On Tue, Nov 17, 2009 at 3:12 PM, Bob Proulx  wrote:
> Peng Yu wrote:
>> I make my ~/.bash_profile empty. I still see
>>
>> $ echo $PATH
>> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
>
> What is in your ~/.bashrc file?
>
>> I checked /etc/profile and /etc/bash.bashrc. But I don't PATH is set
>> in the two files. I'm wondering from which file PATH is set. BTW, my
>> system is ubuntu.
>
> Are you sure that it isn't being set in /etc/profile?
>
> It may be the compiled in value.  Run strings on your binary and see
> what is there.  On my Debian system:
>
>  $ grep /usr/bin /etc/profile
>  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
>  PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
>
>  $ strings /bin/bash | grep /usr/bin
>  /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Here are what I have.

$ grep /usr/bin /etc/profile
$ strings /bin/bash | grep /usr/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin




Command completion considering characters after the cursor?

2010-01-18 Thread Peng Yu
Suppose I have 'some.sh' in my command line, and my cursor is at '.'

$some.sh

Suppose there is only one command that start with 'some', which is
'something.sh'. After I type TAB, I will have 'something.sh.sh' in my
command line.

$something.sh.sh

I'm wondering if there is a way to configure bash, so that I will get
'something.sh' rather than 'something.sh.sh'.




Re: Command completion considering characters after the cursor?

2010-01-23 Thread Peng Yu
On Mon, Jan 18, 2010 at 10:00 AM, Chet Ramey  wrote:
> On 1/18/10 9:49 AM, Peng Yu wrote:
>> Suppose I have 'some.sh' in my command line, and my cursor is at '.'
>>
>> $some.sh
>>
>> Suppose there is only one command that start with 'some', which is
>> 'something.sh'. After I type TAB, I will have 'something.sh.sh' in my
>> command line.
>>
>> $something.sh.sh
>>
>> I'm wondering if there is a way to configure bash, so that I will get
>> 'something.sh' rather than 'something.sh.sh'.
>
> There is nothing directly analogous in bash.  The closest thing is the
> `skip-completed-text' readline variable in bash-4.1/readline-6.1, but
> that will not help in this case.

Can I request this feature be added in future version of bash?




Argument list too long (how to remove the limit)

2010-01-25 Thread Peng Yu
I got the following message. Is there a way to configure bash such
that there is not a limit like this?

/bin/bash: Argument list too long




Colored command completion

2010-01-26 Thread Peng Yu
I'm wondering if it is possible to color the files shown by command
completion (i.e. after typing ). The color scheme can be of the
same as the one used in "ls --color=auto". By this way, it is easier
to see what the type of the file is.




Circulate matches in command completion?

2010-02-11 Thread Peng Yu
Suppose I file 'a1.txt' and 'a2.txt' in my current directory. When I
type 'cat a' then TAB, it will show me 'a1.txt' and 'a2.txt'. If I
type TAB repeatedly, it will always show me the same thing.

However, a better response might be
1. complete the command to 'cat a1.txt' at the 2nd TAB,
2. complete the command to 'cat a2.txt' at the 3rd TAB,
3. return to the original 'cat a' at the 4th TAB,
4. complete the command to 'cat a1.txt' again at the 5th TAB.

I'm wondering if there is a way to configure bash this way.




Re: Circulate matches in command completion?

2010-02-11 Thread Peng Yu
On Thu, Feb 11, 2010 at 9:58 AM, Chet Ramey  wrote:
> On 2/11/10 10:54 AM, Peng Yu wrote:
>> Suppose I file 'a1.txt' and 'a2.txt' in my current directory. When I
>> type 'cat a' then TAB, it will show me 'a1.txt' and 'a2.txt'. If I
>> type TAB repeatedly, it will always show me the same thing.
>>
>> However, a better response might be
>> 1. complete the command to 'cat a1.txt' at the 2nd TAB,
>> 2. complete the command to 'cat a2.txt' at the 3rd TAB,
>> 3. return to the original 'cat a' at the 4th TAB,
>> 4. complete the command to 'cat a1.txt' again at the 5th TAB.
>>
>> I'm wondering if there is a way to configure bash this way.
>
> bind 'TAB:menu-complete'

This is helpful. But it is not exactly what I'm looking for. I still
want to show 'a1.txt' and 'a2.txt' at the 1st TAB. The above bind
command gives me 'cat a1.txt' directly at the 1st TAB.




Re: Circulate matches in command completion?

2010-02-11 Thread Peng Yu
On Thu, Feb 11, 2010 at 10:36 AM, Chet Ramey  wrote:
> On 2/11/10 11:05 AM, Peng Yu wrote:
>> On Thu, Feb 11, 2010 at 9:58 AM, Chet Ramey  wrote:
>>> On 2/11/10 10:54 AM, Peng Yu wrote:
>>>> Suppose I file 'a1.txt' and 'a2.txt' in my current directory. When I
>>>> type 'cat a' then TAB, it will show me 'a1.txt' and 'a2.txt'. If I
>>>> type TAB repeatedly, it will always show me the same thing.
>>>>
>>>> However, a better response might be
>>>> 1. complete the command to 'cat a1.txt' at the 2nd TAB,
>>>> 2. complete the command to 'cat a2.txt' at the 3rd TAB,
>>>> 3. return to the original 'cat a' at the 4th TAB,
>>>> 4. complete the command to 'cat a1.txt' again at the 5th TAB.
>>>>
>>>> I'm wondering if there is a way to configure bash this way.
>>>
>>> bind 'TAB:menu-complete'
>>
>> This is helpful. But it is not exactly what I'm looking for. I still
>> want to show 'a1.txt' and 'a2.txt' at the 1st TAB. The above bind
>> command gives me 'cat a1.txt' directly at the 1st TAB.
>
> Look at the 'show-all-if-ambiguous' option.  The combination may do what
> you want.

set show-all-if-ambiguous On
bind 'TAB:menu-complete'

I typed in the above two commands. It seems that command completion is
the same as if I only typed in the second command. Do you know why?




Re: Circulate matches in command completion?

2010-02-11 Thread Peng Yu
On Fri, Feb 12, 2010 at 1:36 PM, DennisW  wrote:
> On Feb 11, 11:33 am, Peng Yu  wrote:
>> On Thu, Feb 11, 2010 at 10:36 AM, Chet Ramey  wrote:
>> > On 2/11/10 11:05 AM, Peng Yu wrote:
>> >> On Thu, Feb 11, 2010 at 9:58 AM, Chet Ramey  wrote:
>> >>> On 2/11/10 10:54 AM, Peng Yu wrote:
>> >>>> Suppose I file 'a1.txt' and 'a2.txt' in my current directory. When I
>> >>>> type 'cat a' then TAB, it will show me 'a1.txt' and 'a2.txt'. If I
>> >>>> type TAB repeatedly, it will always show me the same thing.
>>
>> >>>> However, a better response might be
>> >>>> 1. complete the command to 'cat a1.txt' at the 2nd TAB,
>> >>>> 2. complete the command to 'cat a2.txt' at the 3rd TAB,
>> >>>> 3. return to the original 'cat a' at the 4th TAB,
>> >>>> 4. complete the command to 'cat a1.txt' again at the 5th TAB.
>>
>> >>>> I'm wondering if there is a way to configure bash this way.
>>
>> >>> bind 'TAB:menu-complete'
>>
>> >> This is helpful. But it is not exactly what I'm looking for. I still
>> >> want to show 'a1.txt' and 'a2.txt' at the 1st TAB. The above bind
>> >> command gives me 'cat a1.txt' directly at the 1st TAB.
>>
>> > Look at the 'show-all-if-ambiguous' option.  The combination may do what
>> > you want.
>>
>> set show-all-if-ambiguous On
>> bind 'TAB:menu-complete'
>>
>> I typed in the above two commands. It seems that command completion is
>> the same as if I only typed in the second command. Do you know why?
>
> The first command should be:
>
> bind 'set show-all-if-ambiguous On'

bind 'set show-all-if-ambiguous On'
bind 'TAB:menu-complete'

I typed the above commands in the command line, but I still don't see
the print out of all matches. Would you please let me know how to
debug what is wrong?




Is there a special variable for the directory where the script is in?

2010-02-11 Thread Peng Yu
$0 gives the file name of the script. I could use several shell
command to get the directory where the script is in. But I'm wondering
if there is an easy-to-use variable that refers to the directory where
the script is in?




Re: Circulate matches in command completion?

2010-02-12 Thread Peng Yu
On Fri, Feb 12, 2010 at 10:43 AM, Chet Ramey  wrote:
> On 2/11/10 6:14 PM, Peng Yu wrote:
>
>> bind 'set show-all-if-ambiguous On'
>> bind 'TAB:menu-complete'
>>
>> I typed the above commands in the command line, but I still don't see
>> the print out of all matches. Would you please let me know how to
>> debug what is wrong?
>
> The combination of those two commands does exactly what you want in
> bash-4.1.  Menu completion on previous versions of bash behaves
> differently.

My bash is of the following version. I guess this is the reason what
the command is not working?

$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.




Why tab is not colored?

2010-03-07 Thread Peng Yu
My grep is aliased to "grep --color=auto".

I then use grep "", where  is a tab character. But the
result is not colored. How to make the tabs colored?




How cd to a directory with special characters like environment\<\-?

2010-04-08 Thread Peng Yu
I make the following directory and try to cd to it. But I can't. Could
you let me know what is the correct way of doing so?

$ mkdir environment\<\-
$ cd environmen\<\-
-bash: cd: environmen<-: No such file or directory

-- 
Regards,
Peng




Re: How cd to a directory with special characters like environment\<\-?

2010-04-08 Thread Peng Yu
On Thu, Apr 8, 2010 at 4:58 PM, Peng Yu  wrote:
> I make the following directory and try to cd to it. But I can't. Could
> you let me know what is the correct way of doing so?
>
> $ mkdir environment\<\-
> $ cd environmen\<\-
> -bash: cd: environmen<-: No such file or directory

Never mind. I get it.

-- 
Regards,
Peng




How to start bash without inheriting any environment variables and user level profiles (such as .bash_profile)?

2010-04-28 Thread Peng Yu
I'm wondering how to start bash without inheriting any environment
variables and user level profiles (such as .bash_profile). Would you
please let me know what option to use?

-- 
Regards,
Peng




How to control the width of a number?

2010-05-04 Thread Peng Yu
Hi,

I want $page has three digits. If it is less than 100, prefix it with
0. Is there an easy way to do so in bash?

for i in `seq 28 55`;
do
  page=`echo '148-('$i'-28)*2'|bc`
  echo $i $page
done

-- 
Regards,
Peng




Re: How to control the width of a number?

2010-05-04 Thread Peng Yu
On Tue, May 4, 2010 at 10:28 AM, Dennis Williamson
 wrote:
> for i in {28..55}
> # or: for ((i=28; i<=55; i++))
> do
>  (( page = 148 - ( i - 28 ) * 2 ))
>  printf '$d $03d\n' $i $page
> done

I assume that you mean '%d %03d'.

> This eliminates external calls to bc and seq in addition to zero-padding 
> $page.

It is good to know this trick.

> On Tue, May 4, 2010 at 8:05 AM, Peng Yu  wrote:
>> Hi,
>>
>> I want $page has three digits. If it is less than 100, prefix it with
>> 0. Is there an easy way to do so in bash?
>>
>> for i in `seq 28 55`;
>> do
>>  page=`echo '148-('$i'-28)*2'|bc`
>>  echo $i $page
>> done
>>
>> --
>> Regards,
>> Peng
>>
>>
>>
>



-- 
Regards,
Peng




How to overwrite a symbolic link?

2010-05-06 Thread Peng Yu
Suppose that I have a symbolic link link1 pointing to file1. When I
write to link1, I don't want file1 change. I want it to remove the
link generated a new file and write to it.

pipe '>' will change file 1. I'm wondering if there is way to do so,
so that I don't have to test whether it is a symbolic link or not
explicitly.

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-06 Thread Peng Yu
On Thu, May 6, 2010 at 10:53 AM, Pierre Gaston  wrote:
> On Thu, May 6, 2010 at 5:53 PM, Peng Yu  wrote:
>> Suppose that I have a symbolic link link1 pointing to file1. When I
>> write to link1, I don't want file1 change. I want it to remove the
>> link generated a new file and write to it.
>>
>> pipe '>' will change file 1. I'm wondering if there is way to do so,
>> so that I don't have to test whether it is a symbolic link or not
>> explicitly.
>>
> rm link1

Is there a way to overload operators like '>' and '>>' in bash, just
as overloading in C++, etc. Suppose I have already made some bash
program using '>' and '>>' without thinking about symbolic link, but I
begin aware of them later. I would be cumbersome to add a test
statement and deciding whether 'rm' or not for each usage of '>' and
'>>'.

A more general question is how to change the behavior of a program
(for example compiled from C code) to delete symbolic link and write a
new file, without recompiling the program.

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-07 Thread Peng Yu
On Thu, May 6, 2010 at 5:36 PM, Bob Proulx  wrote:
> Peng Yu wrote:
>> Is there a way to overload operators like '>' and '>>' in bash, just
>> as overloading in C++, etc. Suppose I have already made some bash
>> program using '>' and '>>' without thinking about symbolic link, but I
>> begin aware of them later. I would be cumbersome to add a test
>> statement and deciding whether 'rm' or not for each usage of '>' and
>> '>>'.
>
> It sounds to me like you are trying to implement a "script space"
> version of copy-on-write semantics?  Perhaps you should investigate
> using one of the filesystem based solutions.  It would probably give
> you a better result.
>
>> A more general question is how to change the behavior of a program
>> (for example compiled from C code) to delete symbolic link and write a
>> new file, without recompiling the program.
>
> A Comment: Symbolic links are designed to be transparent.  Normal
> programs are not aware of them.
>
> You could probably create an LD_PRELOAD library to intercept file
> modification calls and then handle them in your own code.  But I think
> it would be difficult to do it all completely correctly and not to
> introduce bugs.  I advise against it.

Would you please elaborate a little more on how to use LD_PRELOAD to
modify the call. If the library (for example, 'open' from stdlib.h)
is statically compiled in the binary, is LD_PRELOAD going to replace
it with a different 'open' function?

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-07 Thread Peng Yu
On Fri, May 7, 2010 at 9:26 AM, Marc Herbert  wrote:
> Le 06/05/2010 15:53, Peng Yu a écrit :
>> Suppose that I have a symbolic link link1 pointing to file1. When I
>> write to link1, I don't want file1 change. I want it to remove the
>> link generated a new file and write to it.
>
> This is a very strange question. Symbolic links are expressly designed
> to fool everyone. Why are you using symbolic links if you do not want
> to be fooled?
>
> Please give more details about what you are trying to do.

Suppose I have N files in a directory that does some job. Lets say M
(' and '>>' and the open file function call.

This is why I ask the question in the OP. Let me know if you have any
thought on solving this problem.

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-07 Thread Peng Yu
On Fri, May 7, 2010 at 10:16 AM, Eric Blake  wrote:
> On 05/07/2010 09:02 AM, Peng Yu wrote:
>> Suppose I need to modify one primary file slightly to do something a
>> little bit different. But I still need to do the original job,
>> therefore I need to keep it the original M files. I can copy the whole
>> directory and then modify one file in the newly copied N files. But
>> I'll lose track of which file has been changed later on, which is
>> important to me.
>
> Consider using a version control system.  Track the contents of your
> directory under your favorite VCS, like git, and then you can use
> version control commands to generate the delta for both primary and
> secondary files across any state that you committed.

I can't use version control for
1. I need to frequently change file names.
2. Both primary and secondary files could be of hundred of MB or even GB.

> Or, for a one-shot solution, you could use a BSD union mount, where the
> difference between the mount overlay and the original directory shows
> exactly what was modified.

The problem is that if I have many directories that form a complex
copying graph. It is not possible to keep track of all the possible
lineages. Also, I need to avoid change multiple copies of the same
file.

> But overloading bash's > and >> operators is not possible.

Is it because the underlying library that used in bash doesn't support
the semantic of symbolic link that I propose? Or it is because of the
OS?

Is it possible to modify source code of bash to change the semantics
of symbolic link.

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-09 Thread Peng Yu
On Fri, May 7, 2010 at 10:41 AM, Eric Blake  wrote:
> On 05/07/2010 09:31 AM, Peng Yu wrote:
>> On Fri, May 7, 2010 at 10:16 AM, Eric Blake  wrote:
>>> On 05/07/2010 09:02 AM, Peng Yu wrote:
>>>> Suppose I need to modify one primary file slightly to do something a
>>>> little bit different. But I still need to do the original job,
>>>> therefore I need to keep it the original M files. I can copy the whole
>>>> directory and then modify one file in the newly copied N files. But
>>>> I'll lose track of which file has been changed later on, which is
>>>> important to me.
>>>
>>> Consider using a version control system.  Track the contents of your
>>> directory under your favorite VCS, like git, and then you can use
>>> version control commands to generate the delta for both primary and
>>> secondary files across any state that you committed.
>>
>> I can't use version control for
>> 1. I need to frequently change file names.
>> 2. Both primary and secondary files could be of hundred of MB or even GB.
>
> git handles both of those situations, without too much hassle.
>
>>> But overloading bash's > and >> operators is not possible.
>>
>> Is it because the underlying library that used in bash doesn't support
>> the semantic of symbolic link that I propose? Or it is because of the
>> OS?
>>
>> Is it possible to modify source code of bash to change the semantics
>> of symbolic link.
>
> Yes, it's possible to modify the source of bash to change how bash
> treats symlinks when using the > operator.  But I would advise against
> it, as your fork of bash would no longer comply with POSIX, and would
> probably break a lot more than it fixes.  Rather, if you insist on
> modifying bash, consider adding a new operator (maybe spelled '>;',
> similar to the noclobber override spelling of '>|'), so that your use of
> the new operator is explicit that you know what you are doing in your
> scripts (if the operand of the new operator is a symlink, break the
> symlink and create a file in its place instead of operating on the
> target of the symlink).

Is there a way to define the operator '>;' without touching the bash
source code?

-- 
Regards,
Peng




Re: How to overwrite a symbolic link?

2010-05-10 Thread Peng Yu
On Mon, May 10, 2010 at 6:59 AM, Marc Herbert  wrote:
> Le 07/05/2010 16:02, Peng Yu a écrit :
>> I can copy the whole
>> directory and then modify one file in the newly copied N files. But
>> I'll lose track of which file has been changed later on, which is
>> important to me.
>
> You will not lose track of the changed files: just run a recursive
> diff comparing the old and new directories and you will see what
> was/has changed (this is actually the poor man's version control
> system).

If I have X similar directories that are resulted from the copying and
modifying in the above, I will end up X*(X-1) comparisons. And it is
still hard to figure out the lineage of these X directories from these
X*(X-1) comparisons (because I need to compare diffs of diffs).
Therefore, I think that this is not a viable solution.

>> Suppose I have N files in a directory that does some job. Lets say M
>> (> files (say, there are a number of programs, each takes some of the M
>> files to generate some of the N-M files).
>> [...]
>> Instead, I'd rather create symbolic link for all the N-M-1 primary
>> files but copy only one remaining file and modify it. Remember, I
>> can't tell which are primary files and which are derived files. So I
>> can not do so.
>>
>> One solution is create symbolic link to N-1 files as long as I can
>> overload '>' and '>>' and the open file function call.
>
> So you are really trying to re-invent some copy-on-write/versioning
> file system by hacking symbolic links and redirections. This looks
> like a lot of pain. You'd better start by looking at existing
> solutions; even if none is ideal for your case, you would at least
> leverage the existing knowledge and experience in this field instead
> of starting from zero.

http://en.wikipedia.org/wiki/Versioning_file_system
http://en.wikipedia.org/wiki/Ext3cow

I'm trying to understand how versioning and copy-on-write are relevant
to my cases. But I don't think that they are really relevant to my
cases.

The closest file system that I have used is snapshot (I don't know the
exact technical term, but I remember there is a '.snapshot' directory
in my home, where I can find previous 1 hour, 4 hours, 1 day and 1week
versions of my home directory).

Please correct me if I'm wrong. These versioning file systems
automatically keep different versions (modified at different time) of
the same file, and doesn't allow users to choose which versions to
keep and which version not to keep. This will end up to many versions
that are actually not useful and it would a pain to look for the
actual useful versions. Also I need the same file appear in two
different directories. I think that these versioning systems may not
help me on this aspect unless I use symbolic links. Therefore, the
solution to my particular problem is still in hacking symbolic link.

>> Let me know if you have any thought on solving this problem.
>
> Since you are using the wrong tool for the job I am afraid you are
> going to be rather alone on this road.

For right now, I think that the best solution is, instead of reloading
>, >>, and related system calls, to test symbolic link and rm it and
then write a new file with the same name. The trick of _ function by
Dennis is useful.

-- 
Regards,
Peng




How to use variable in a range like {a..b}?

2010-05-12 Thread Peng Yu
x=10
for i in {1..$x}; do echo $i; done

The above code give me
{1..10}

, rather than printing numbers from 1 to 10.

I'm wondering how to use variable in a range?

-- 
Regards,
Peng



How to make a directory name with '/' in it?

2010-05-16 Thread Peng Yu
I tried the following command to create a dir with '/' in the name.
But it only create a directory with name 'm'. Is there a way to make a
directory with '/' in the name?

mkdir m\/\/

-- 
Regards,
Peng



How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
When I type something after 'which', something means a command.
However, bash doesn't do command completion for the argument after
'which'. Is there a way to configure bash behave depending on the
context (in this case, do autocomplete after 'which')?

-- 
Regards,
Peng



Re: How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
On Fri, May 21, 2010 at 10:39 AM, Chet Ramey  wrote:
> On 5/21/10 11:25 AM, Peng Yu wrote:
>> When I type something after 'which', something means a command.
>> However, bash doesn't do command completion for the argument after
>> 'which'. Is there a way to configure bash behave depending on the
>> context (in this case, do autocomplete after 'which')?
>
> You can start with `complete -c which' and look further at the
> bash-completion package, which uses the bash programmable completion
> framework to provide custom completions for a large number of commands.

complete -c works. I'm not familar with bash-completion package. How
to customize search path for each command.

For exmaple, I want search in $PYTHONPATH after the command python.

BTW, where is the manual for the -c option of complete. There are too
many 'complete' in man bash. Would you please point me which section
is relevant to complete -c.

-- 
Regards,
Peng



Re: How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
On Fri, May 21, 2010 at 11:19 AM, Marc Herbert  wrote:
> Le 21/05/2010 16:25, Peng Yu a écrit :
>> When I type something after 'which', something means a command.
>> However, bash doesn't do command completion for the argument after
>> 'which'. Is there a way to configure bash behave depending on the
>> context (in this case, do autocomplete after 'which')?
>
> By the way it is better to use "type" instead of "which". "which" is
> often misleading, compare for instance:
>
> type pwd
> which pwd
>
> "which" is misleading in many other cases.

Since pwd is a shell command, when /bin/pwd is actually used? In
shells that don't have built-in pwd?

-- 
Regards,
Peng



Re: How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
On Fri, May 21, 2010 at 11:06 AM, Eric Blake  wrote:
> On 05/21/2010 09:50 AM, Peng Yu wrote:
>> complete -c works. I'm not familar with bash-completion package. How
>> to customize search path for each command.
>
> By using appropriate complete invocations for each command, and lots of
> shell functions.
>
>>
>> For exmaple, I want search in $PYTHONPATH after the command python.
>
> bash-completion could do that for you, too.
>
> $ complete -p python
> complete -o filenames -F _python python
> $ type _python
> _python is a function
> _python ()
> {
>    local prev cur i;
>    COMPREPLY=();
>    cur=`_get_cword`;
>    prev=${COMP_WORDS[COMP_CWORD-1]##*/};
>    case "$prev" in
>        -Q)
>            COMPREPLY=($( compgen -W "old new warn warnall" -- "$cur" ));
>            return 0
>        ;;
>        -W)
>            COMPREPLY=($( compgen -W "ignore default all module once
> error"             -- "$cur" ));
>            return 0
>        ;;
>        -c)
>            _filedir '@(py|pyc|pyo)';
>            return 0
>        ;;
>        !(python|-?))
>            [[ ${COMP_WORDS[COMP_CWORD-2]} != -@(Q|W) ]] && _filedir
>        ;;
>    esac;
>    for ((i=0; i < ${#comp_wor...@]}-1; i++ ))
>    do
>        if [[ ${COMP_WORDS[i]} == -c ]]; then
>            _filedir;
>        fi;
>    done;
>    if [[ "$cur" != -* ]]; then
>        _filedir '@(py|pyc|pyo)';
>    else
>        COMPREPLY=($( compgen -W "- -d -E -h -i -O -Q -S -t -u
>   -U -v -V -W -x -c" -- "$cur" ));
>    fi;
>    return 0
> }
>
> Actually, I don't see any mention of PYTHONPATH in that function, so you
> may want to file an enhancement request against the bash-completion
> package to improve their _python() function to also consider PYTHONPATH,
> rather than just *.py[co] files.
>
>>
>> BTW, where is the manual for the -c option of complete. There are too
>> many 'complete' in man bash. Would you please point me which section
>> is relevant to complete -c.
>
> 'help complete' is the easiest way to see the short details; other than
> that, look for complete under 'SHELL BUILTIN COMMANDS' in the man page.

I see the following error. What is wrong with my bash?

$ complete -p python
bash: complete: python: no completion specification
$ type _python
bash: type: _python: not found

-- 
Regards,
Peng



Re: How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
On Fri, May 21, 2010 at 12:07 PM, Eric Blake  wrote:
> On 05/21/2010 10:41 AM, Peng Yu wrote:
>>> 'help complete' is the easiest way to see the short details; other than
>>> that, look for complete under 'SHELL BUILTIN COMMANDS' in the man page.
>>
>> I see the following error. What is wrong with my bash?
>
> Nothing is wrong with bash.  Bash is telling you that you have not yet
> installed a completion handler for python.
>
> But it _is_ a sign that you have not installed the bash-completion
> package, nor installed completions manually.
>
>>
>> $ complete -p python
>> bash: complete: python: no completion specification
>> $ type _python
>> bash: type: _python: not found
>
> What's your distro?  On fedora, do 'yum install -y bash-completion'.  Or
> get it yourself: http://bash-completion.alioth.debian.org/files/
>
> Then start a new shell, and compare the difference.

I installed bash-completion. Then I start a new shell. But I still see
the above error messages.

$ sudo apt-get install bash-completion
[sudo] password for administrator:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  bash-completion
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 124kB of archives.
After this operation, 479kB of additional disk space will be used.
Get:1 http://escalade.imgen.bcm.tmc.edu hardy/main bash-completion
20060301-3ubuntu3 [124kB]
Fetched 124kB in 0s (4413kB/s)
Selecting previously deselected package bash-completion.
(Reading database ... 66941 files and directories currently installed.)
Unpacking bash-completion (from
.../bash-completion_20060301-3ubuntu3_all.deb) ...
Setting up bash-completion (20060301-3ubuntu3) ...


-- 
Regards,
Peng



Re: How to autocomplete after 'which'?

2010-05-21 Thread Peng Yu
> You will need to source the file that contains the functions:
>
> source /etc/bash_completions
>
> The installation you did most likely added something like this to your
> ~/.bashrc:
>
> # enable programmable completion features (you don't need to enable
> # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
> # sources /etc/bash.bashrc).
> if [ -f /etc/bash_completion ]; then
>    . /etc/bash_completion
> fi

The above code changed the behavior of, for example, cd $DATA.
After I type , it becomes cd \$DATA. This is not what want. When
it is not sourced, typing  would not change cd $DATA. Is there a
way to perserve the behavior of cd $DATA?



-- 
Regards,
Peng



How only show time of files with ls?

2010-05-22 Thread Peng Yu
ls -go gives me permission and file sizes. But I only want to show
time and file names. Would you please let me know what command to use?

-- 
Regards,
Peng



How not to expand a environment variable when doing command completion?

2010-05-22 Thread Peng Yu
When I type 'cd $HOME/.', bash will expand the command to 'cd
/home/my_user_name/.'. Would you please let me know how to let bash
expand the environment variable?

-- 
Regards,
Peng



Re: How not to expand a environment variable when doing command completion?

2010-05-24 Thread Peng Yu
On Sat, May 22, 2010 at 3:48 PM, Peng Yu  wrote:
> When I type 'cd $HOME/.', bash will expand the command to 'cd
> /home/my_user_name/.'. Would you please let me know how to let bash
> expand the environment variable?

I mean 'NOT to let bash ...'. Also, I forget to mention that
/etc/bash-completion is not sourced in my .bashrc and I have make sure
that it is not sourced anywhere before .bashrc. Therefore, this is a
question regarding bash. Could somebody let me know how to not expand
the variable.

I also tried 'python $SOME_VARIABLE/', where $SOME_VARIABLE is
set to a directory. Wired enough, this time $SOME_VARIABLE is not
expanded at all, neither any completion to any files under the
directory $SOME_VARIABLE.

BTW, my bash is of version 4.1.0(2)-release (x86_64-unknown-linux-gnu).

-- 
Regards,
Peng



Re: How not to expand a environment variable when doing command completion?

2010-05-24 Thread Peng Yu
> I also tried 'python $SOME_VARIABLE/', where $SOME_VARIABLE is
> set to a directory. Wired enough, this time $SOME_VARIABLE is not
> expanded at all, neither any completion to any files under the
> directory $SOME_VARIABLE.

I was confused. The above statement is not true.

-- 
Regards,
Peng



Re: How not to expand a environment variable when doing command completion?

2010-05-25 Thread Peng Yu
On Tue, May 25, 2010 at 9:18 PM, Chet Ramey  wrote:
>> When I type 'cd $HOME/.', bash will expand the command to 'cd
>> /home/my_user_name/.'. Would you please let me know how to let bash
>> expand the environment variable?
>
> There is no setting to suppress the environment variable expansion.  Bash
> has behaved this way for many years.

This default behavior may not be always wanted. Will a setting be
added to suppress the expansion in future versions of bash?

-- 
Regards,
Peng



process substitution with a give suffix

2010-06-08 Thread Peng Yu
I have a program that only accept argument with a give suffix

./program xxx.suffix

If I use process substitution, which gives me /dev/fd/xx, it will not
work with the program. Is there a way to make sure a suffix is added
to the substitute process file handle in /def/fd/, so that the program
can work with process substitution?

-- 
Regards,
Peng



cd multiple levels up?

2010-06-13 Thread Peng Yu
Hello,

I frequently need do cd multiple levels up. For example,

cd ../..
cd ../../../../

It would be convenient to type something like "cd 2" or "cd 4". Is
there a command for this?

-- 
Regards,
Peng



How to convert symbolic links pointing outside to a file?

2010-06-14 Thread Peng Yu
Hi,

I only want to convert symbolic links that point outside the directory
to be archived to a file. But I still want to keep symbolic links
point inside as symbolic links. Is there an option or a walkaround to
do so in tar?

-- 
Regards,
Peng



How not to inherit any environment variable from parent process?

2010-06-22 Thread Peng Yu
I use bash --noprofile to start a bash session. Since this doesn't
source any profile files, I'd think that no environment variable
should be set. But I still see environment variables set. Are they
inherit from the parent session. Is there a way to start a bash
session without using parent environment variables?

-- 
Regards,
Peng



The usage of {0..${#parameter}}

2010-06-23 Thread Peng Yu
The commented line in the following bash script is not working. I
notice the following description from man bash and I think that it is
the reason. But I'm not completely sure. Would you please confirm it
for me?

Also, I'm wondering where this decision was made. Can't bash be made
more powerful by allowing the commented line? Will it cause any
problem if such thing is allowed?


#!/usr/bin/env bash

parameter=abcdefg

#for i in {0..${#parameter}};
for i in {0..7};
do
  echo $i ${parameter:$i}
done


   Brace expansion is performed before any other expansions, and any char‐
   acters  special to other expansions are preserved in the result.  It is
   strictly textual.  Bash does not apply any syntactic interpretation  to
   the context of the expansion or the text between the braces.


-- 
Regards,
Peng



How to supply a string with space in it as parameter to a function?

2010-06-23 Thread Peng Yu
Hi,

According to man bash, I thought that $@ instead of $* can help me
pass a string with space as a parameter. But it is not. Would you
please show me how to print 'b c' as a single argument in the
following code?

#!/usr/bin/env bash

function f {
#for i in $*;
for i in $@;
do
  echo $i
done
}

f a 'b c' d e f g


$ ./main.sh
a
b
c
d
e
f
g

-- 
Regards,
Peng



Re: How to supply a string with space in it as parameter to a function?

2010-06-23 Thread Peng Yu
On Wed, Jun 23, 2010 at 5:43 PM, Chris F.A. Johnson
 wrote:
> On Wed, 23 Jun 2010, Peng Yu wrote:
>
>> Hi,
>>
>> According to man bash, I thought that $@ instead of $* can help me
>> pass a string with space as a parameter. But it is not. Would you
>> please show me how to print 'b c' as a single argument in the
>> following code?
>
>   Always quote variable references unless you have a good reason not
>   to.
>
>> #!/usr/bin/env bash
>>
>> function f {
>> #for i in $*;
>> for i in $@;
>> do
>>   echo $i
>> done
>> }
>
> for i in "$@"  ## note the quotes
> do
>  echo "$i"  ## better is: printf "%s\n" "$i"
> done

Why printf is better than echo? Is this because printf is more robust than echo?

-- 
Regards,
Peng



Where is the syntax of ${arr...@]:1:2} described?

2010-07-08 Thread Peng Yu
Hi,

array=(a b c)
echo ${arr...@]:1:2}


I'm looking for the document for the above usage. I checked the Arrays
section of man bash, but I don't see a description of such usage. I
also searched man bash with [0-9]+:[0-9]+. But I still don't find a
description. Could anybody let me know where this is documented?

-- 
Regards,
Peng



Re: Where is the syntax of ${arr...@]:1:2} described?

2010-07-08 Thread Peng Yu
I find the following. But this is for string according to the
highlighted word "Substring Expansion".

${parameter:offset:length}
  Substring Expansion.  Expands to  up  to  length  characters  of
  


The following description is actually for array. Therefore, I suggest
to put this into the array section and add references between the two
sections.

   . If
  parameter is @,  the  result  is  length  positional  parameters
  beginning at offset.  If parameter is an indexed array
name sub\u2010
 .



On Thu, Jul 8, 2010 at 9:27 PM, Peng Yu  wrote:
> Hi,
>
> array=(a b c)
> echo ${arr...@]:1:2}
>
>
> I'm looking for the document for the above usage. I checked the Arrays
> section of man bash, but I don't see a description of such usage. I
> also searched man bash with [0-9]+:[0-9]+. But I still don't find a
> description. Could anybody let me know where this is documented?
>
> --
> Regards,
> Peng
>



-- 
Regards,
Peng



  1   2   >