`history -r` can not read from /dev/stdin ?

2012-08-16 Thread Techlive Zheng
I was trying to reload the bash history file which changed by another
bash session with the following commands, but it wouldn't work, please
help me, why?

```
new_history=$(history -a /dev/stdout)
history -c
history -r
echo "$new_history" | history -r /dev/stdin
```



Re: `history -r` can not read from /dev/stdin ?

2012-08-16 Thread Chet Ramey
On 8/16/12 9:17 AM, 郑文辉(Techlive Zheng) wrote:
> I was trying to reload the bash history file which changed by another
> bash session with the following commands, but it wouldn't work, please
> help me, why?
> 
> ```
> new_history=$(history -a /dev/stdout)
> history -c
> history -r
> echo "$new_history" | history -r /dev/stdin
> ```

One possible cause that springs to mind is the fact that the `history -r'
at the end of the pipeline is run in a subshell and cannot affect its
parent's history list.



-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: `history -r` can not read from /dev/stdin ?

2012-08-16 Thread Techlive Zheng
2012/8/17 Chet Ramey :
> On 8/16/12 9:17 AM, 郑文辉(Techlive Zheng) wrote:
>> I was trying to reload the bash history file which changed by another
>> bash session with the following commands, but it wouldn't work, please
>> help me, why?
>>
>> ```
>> new_history=$(history -a /dev/stdout)
>> history -c
>> history -r
>> echo "$new_history" | history -r /dev/stdin
>> ```
>
> One possible cause that springs to mind is the fact that the `history -r'
> at the end of the pipeline is run in a subshell and cannot affect its
> parent's history list.
>
>
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/

So, How could I accomplish this kind of thing?



Re: `history -r` can not read from /dev/stdin ?

2012-08-16 Thread Chet Ramey
On 8/16/12 10:11 PM, 郑文辉(Techlive Zheng) wrote:
> 2012/8/17 Chet Ramey :
>> On 8/16/12 9:17 AM, 郑文辉(Techlive Zheng) wrote:
>>> I was trying to reload the bash history file which changed by another
>>> bash session with the following commands, but it wouldn't work, please
>>> help me, why?
>>>
>>> ```
>>> new_history=$(history -a /dev/stdout)
>>> history -c
>>> history -r
>>> echo "$new_history" | history -r /dev/stdin
>>> ```
>>
>> One possible cause that springs to mind is the fact that the `history -r'
>> at the end of the pipeline is run in a subshell and cannot affect its
>> parent's history list.
>
> So, How could I accomplish this kind of thing?

Why not just use a regular file?

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Shell comment ignored

2012-08-16 Thread Keith Clifford
Hi,

I first noticed this in a shell script under mingw but tested it in bash
under Linux and it has the same behaviour.

 

some_var=# What I thought was a comment.

 

The '#' is eaten by the variable assignment so that some_var gets a null
value and the rest of the line is not treated like a command.

 

If there is no space after the '#':

 

some_var=#What

 

Then some_var is set to '#What'.

 

Regards,

Keith Clifford.



Re: Shell comment ignored

2012-08-16 Thread Mike Frysinger
On Thursday 16 August 2012 20:36:45 Keith Clifford wrote:
> some_var=# What I thought was a comment.
> 
> The '#' is eaten by the variable assignment so that some_var gets a null
> value and the rest of the line is not treated like a command.

this is correct (if sometimes confusing to people) behavior.  you need to have 
some byte in there that breaks up the tokens -- be it a tab, space, or 
newline.
-mike


signature.asc
Description: This is a digitally signed message part.


Re: Shell comment ignored

2012-08-16 Thread Eric Blake
On 08/16/2012 06:36 PM, Keith Clifford wrote:
> 
> some_var=# What I thought was a comment.
   ^

Not the first byte of a word.

# starts a comment only when it is the first byte of a word.

You can write:

some_var= #Now this is a comment, since there is space between = and #

if you meant for some_var to be assigned the empty string, or write:

some_var='# Embedded as part of the assignment'

if you meant to include more than just the # in the value assigned to
the var.


> 
> some_var=#What
> 
>  
> 
> Then some_var is set to '#What'.

Yep, and that is correct behavior as mandated by POSIX.

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: `history -r` can not read from /dev/stdin ?

2012-08-16 Thread Techlive Zheng
2012/8/17 Chet Ramey :
> On 8/16/12 10:11 PM, 郑文辉(Techlive Zheng) wrote:
>> 2012/8/17 Chet Ramey :
>>> On 8/16/12 9:17 AM, 郑文辉(Techlive Zheng) wrote:
 I was trying to reload the bash history file which changed by another
 bash session with the following commands, but it wouldn't work, please
 help me, why?

 ```
 new_history=$(history -a /dev/stdout)
 history -c
 history -r
 echo "$new_history" | history -r /dev/stdin
 ```
>>>
>>> One possible cause that springs to mind is the fact that the `history -r'
>>> at the end of the pipeline is run in a subshell and cannot affect its
>>> parent's history list.
>>
>> So, How could I accomplish this kind of thing?
>
> Why not just use a regular file?
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/

Actually, I was tring to erase duplicate entries and share history
across bash sessions.'erasedups' in `HISTCONTROL` only have effect for
history list in the memory, so my solution is to load entire history
file into memory and save it after every command finished.

Here is what I am currently have in .bashrc, and it works as expected.

reload_history() {
local HISTHASH_NEW=`md5sum $HOME/.bash_history | cut -d' ' -f1`
if [ "$HISTHASH" = "$HISTHASH_NEW" ]; then
history -w
# This is necessay because we need
# to clear the last append signture
history -c
history -r
else
HISTTEMP=`mktemp`
history -a $HISTTEMP
history -c
history -r
history -r $HISTTEMP
history -w
rm $HISTTEMP
fi
HISTHASH=`md5sum $HOME/.bash_history | cut -d' ' -f1`
}

export PROMPT_COMMAND="reload_history;$PROMPT_COMMAND"

Considering `mkemp` then remove the temp file on every prompt command
is a little bit expensive, I want to directly pipe the output of the
`history -a` to `hisotory -r` like below, unfortunately, this wouldn't
work, because `history -r` could not handle /dev/stdin.

reload_history() {
local HISTHASH_NEW=`md5sum $HOME/.bash_history | cut -d' ' -f1`
if [ "$HISTHASH" = "$HISTHASH_NEW" ]; then
history -w
# This is necessay because we need
# to clear the last append signture
history -c
history -r
else
new_history=$(history -a /dev/stdout)
history -c
history -r
echo "$new_history" | history -r /dev/stdin
history -w
fi
HISTHASH=`md5sum $HOME/.bash_history | cut -d' ' -f1`
}

export PROMPT_COMMAND="reload_history;$PROMPT_COMMAND"