分页: 1 / 1

终端操作的记录在哪里?

发表于 : 2007-09-29 9:40
dongr
安装好ubuntu后,首先与终端打交道,写了N个sudo apt-get。还好,通过方向键(↑),能够把所有的操作召唤回来,非常方便。那么,这些终端操作的记录在哪里呢? 我想把第一次键入的命令保留下来。



不会是通过方向键(↑),来实现我的目的吧。

发表于 : 2007-09-29 9:45
oiniya
~/.bash_history

发表于 : 2007-09-29 9:48
bones7456
我是在想,同时开了N个终端的时候,怎么记的?

发表于 : 2007-09-29 9:50
eexpress

发表于 : 2007-09-29 10:07
bones7456
ee干嘛哦?弄个不相关的链接...

发表于 : 2007-09-29 10:09
xhy
bones7456 写了:我是在想,同时开了N个终端的时候,怎么记的?
根据退出的顺序
每退出一个shell 就记录那个shell下的命令

代码: 全选

static int 
history_do_write (filename, nelements, overwrite)
     const char *filename;
     int nelements, overwrite;
{
  register int i;
  char *output;
  int file, mode, rv; 
#ifdef HISTORY_USE_MMAP
  size_t cursize;

  mode = overwrite ? O_RDWR|O_CREAT|O_TRUNC|O_BINARY : O_RDWR|O_APPEND|O_BINARY;
#else
  mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
#endif
  output = history_filename (filename);
  rv = 0;

  if ((file = open (output, mode, 0600)) == -1) 
    {
      FREE (output);
      return (errno);
    }

#ifdef HISTORY_USE_MMAP
  cursize = overwrite ? 0 : lseek (file, 0, SEEK_END);
#endif

  if (nelements > history_length)
    nelements = history_length;

  /* Build a buffer of all the lines to write, and write them in one syscall.
     Suggested by Peter Ho ([email protected]). */
  {
    HIST_ENTRY **the_history;   /* local */
    register int j;
    int buffer_size;
    char *buffer;

    the_history = history_list ();
    /* Calculate the total number of bytes to write. */
    for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
#if 0
      buffer_size += 2 + HISTENT_BYTES (the_history[i]);
#else
      {
    if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
      buffer_size += strlen (the_history[i]->timestamp) + 1;
    buffer_size += strlen (the_history[i]->line) + 1;
      }
#endif

    /* Allocate the buffer, and fill it. */
#ifdef HISTORY_USE_MMAP
    if (ftruncate (file, buffer_size+cursize) == -1)
      goto mmap_error;
    buffer = (char *)mmap (0, buffer_size, PROT_READ|PROT_WRITE, MAP_WFLAGS, file, cursize);
    if ((void *)buffer == MAP_FAILED)
      {
mmap_error:
    rv = errno;
    FREE (output);
    close (file);
    return rv;
      }
#else
    buffer = (char *)malloc (buffer_size);
    if (buffer == 0)
     {
        rv = errno;
    FREE (output);
    close (file);
    return rv;
      }
#endif

    for (j = 0, i = history_length - nelements; i < history_length; i++)
      {
    if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
      {
        strcpy (buffer + j, the_history[i]->timestamp); 
        j += strlen (the_history[i]->timestamp); 
        buffer[j++] = '\n';
      }
    strcpy (buffer + j, the_history[i]->line);
    j += strlen (the_history[i]->line);
    buffer[j++] = '\n';
      }

#ifdef HISTORY_USE_MMAP
    if (msync (buffer, buffer_size, 0) != 0 || munmap (buffer, buffer_size) != 0)
      rv = errno;
#else
    if (write (file, buffer, buffer_size) < 0)
      rv = errno;
    free (buffer);
#endif
  }

  close (file);
 FREE (output);

  return (rv);
}

less .bash_history 能看到自己的操作历史

发表于 : 2007-09-30 18:06
dongr
:D :D :D :D

发表于 : 2007-09-30 18:09
BigSnake.NET
xhy 写了:
bones7456 写了:我是在想,同时开了N个终端的时候,怎么记的?
根据退出的顺序
每退出一个shell 就记录那个shell下的命令

代码: 全选

static int 
history_do_write (filename, nelements, overwrite)
     const char *filename;
     int nelements, overwrite;
{
  register int i;
  char *output;
  int file, mode, rv; 
#ifdef HISTORY_USE_MMAP
  size_t cursize;

  mode = overwrite ? O_RDWR|O_CREAT|O_TRUNC|O_BINARY : O_RDWR|O_APPEND|O_BINARY;
#else
  mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
#endif
  output = history_filename (filename);
  rv = 0;

  if ((file = open (output, mode, 0600)) == -1) 
    {
      FREE (output);
      return (errno);
    }

#ifdef HISTORY_USE_MMAP
  cursize = overwrite ? 0 : lseek (file, 0, SEEK_END);
#endif

  if (nelements > history_length)
    nelements = history_length;

  /* Build a buffer of all the lines to write, and write them in one syscall.
     Suggested by Peter Ho ([email protected]). */
  {
    HIST_ENTRY **the_history;   /* local */
    register int j;
    int buffer_size;
    char *buffer;

    the_history = history_list ();
    /* Calculate the total number of bytes to write. */
    for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
#if 0
      buffer_size += 2 + HISTENT_BYTES (the_history[i]);
#else
      {
    if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
      buffer_size += strlen (the_history[i]->timestamp) + 1;
    buffer_size += strlen (the_history[i]->line) + 1;
      }
#endif

    /* Allocate the buffer, and fill it. */
#ifdef HISTORY_USE_MMAP
    if (ftruncate (file, buffer_size+cursize) == -1)
      goto mmap_error;
    buffer = (char *)mmap (0, buffer_size, PROT_READ|PROT_WRITE, MAP_WFLAGS, file, cursize);
    if ((void *)buffer == MAP_FAILED)
      {
mmap_error:
    rv = errno;
    FREE (output);
    close (file);
    return rv;
      }
#else
    buffer = (char *)malloc (buffer_size);
    if (buffer == 0)
     {
        rv = errno;
    FREE (output);
    close (file);
    return rv;
      }
#endif

    for (j = 0, i = history_length - nelements; i < history_length; i++)
      {
    if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
      {
        strcpy (buffer + j, the_history[i]->timestamp); 
        j += strlen (the_history[i]->timestamp); 
        buffer[j++] = '\n';
      }
    strcpy (buffer + j, the_history[i]->line);
    j += strlen (the_history[i]->line);
    buffer[j++] = '\n';
      }

#ifdef HISTORY_USE_MMAP
    if (msync (buffer, buffer_size, 0) != 0 || munmap (buffer, buffer_size) != 0)
      rv = errno;
#else
    if (write (file, buffer, buffer_size) < 0)
      rv = errno;
    free (buffer);
#endif
  }

  close (file);
 FREE (output);

  return (rv);
}
是追加模式?.. 不会覆盖掉其他shell写的?

发表于 : 2007-09-30 19:08
xhy
追加模式不会覆盖的
如果不是追加模式 就要使用文件锁了

当前文件长度记录在V表中 每个文件的V节点在内核中只有一份
追加模式打开文件 每次写的时候都会把文件表中的偏移量设置为V节点中记录的数值
磁盘IO的write 当数据量不是太大的时候 一般可以认为是原子操作完成

发表于 : 2007-09-30 19:37
eexpress
我上面那帖子里面就有这设置的啊。

Re: 终端操作的记录在哪里?

发表于 : 2012-01-15 21:51
dgsnow
我不知道别的版本在哪,ubuntu11.10是在用户的home中。

Re: 终端操作的记录在哪里?

发表于 : 2012-01-15 22:30
photor
:em09