TraceLife

真正的幸福,在于对平淡生活的热爱……
posts - 37, comments - 0, trackbacks - 0, articles - 6

2012年4月27日

下面列举一下各种 shell 对应的配置文件:

Bourne Shell (sh)  -----  .profile

Korn Shell (ksh) ----- .profile (兼容 sh)

C Shell (csh) ----- .login

Bourne Again Shell (bash)  ----- bash被设计成兼容sh,ksh,并有csh的特征, 所以会在用户的主目录下依次查找.bash_profile,.bash_login,.profile文件,并用找到的第一个作为自己的配置文件;

 

When you start a login shell, bash consults the following files in the specified order:
1. /etc/profile
2. ~/.bash.profile
3. ~/.bash_login
4. ~/.profile
When you start a nonlogin shell, bash consults only one startup file, ~/bash.rc.

posted @ 2012-04-27 16:56 hallo 阅读(7) 评论(0) 编辑

source command

命令的格式有两种

source .bashrc

. .bashrc

下面的方式更简洁,但后续的脚本如果要解析可能就稍微要复杂些。对应的文档:

. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command exe-
cuted from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file-
name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched
if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.
If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional
parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are
executed), and false if filename is not found or cannot be read.

 

posted @ 2012-04-27 16:02 hallo 阅读(7) 评论(0) 编辑

在 Unix* Like 环境下工作时,不免会遇到 bash 环境变量的配置。而每次面对 $HOME 目录下的一些 .bash* 文件时,总会暂时性的忘记该修改那个好。

实际中在那个文件中添加配置信息都没有问题,都可以正常工作。那为什么要设置两个配置文件呢?它们的区别是什么?

查找 bash 的帮助文档。可以找到下面的描述。

~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file

.bash_profile 在登录 shell 时执行,而 .bashrc 会在不是以 login 方式打开 shell 时执行(比如执行 /bin/bash )。

login shell vs non-login shell

不论你是坐在电脑旁或者通过 ssh 远程登录主机,以用户名/密码登录终端时,.bash_profile 都会在初始化命令行提示符之前执行。

如果你已登录到机器上,并且在 Gnome 或 KDE 桌面下打开了一个新的终端(通常这种方式打开 xterm),那么 .bashrc 会在初始化命令行提示符之前执行。当然上面提到的,通过在终端执行 /bin/bash 开启一个新的 bash 示例时,同样也会调用 .bashrc 。

.bash_profile vs .bashrc

举例来说,如果你想在每次登录机器时,都在终端打印机器的诊断信息(当前用户,内存使用、负载均衡等),则需要将配置信息写入 .bash_profile。而如果你想在每次打开终端时进行提示,则记入 .bashrc 。

这样做的好处是针对某些应用场景,可以进行更精细的配置。

上面的描述并不适用于 Mac OS ,对于 Terminal.app ,每次运行时,都只执行 .bash_profile。

那到底写到哪个文件呢?

大多数情况下,你不想为 login shell 和 non-login shell 两种方式分别维护配置文件,因为实际上维护的这两个配置文件通常用完成相似的工作。比如设定 PATH 环境变量,希望在两种方式下都能正常执行,你可以在 .bash_profile 中执行 source .bashrc 命令,并将 PATH 变量设置信息存放到 .bashrc 中。

vim ~/.bash_profile

    if [ -f ~/.bashrc ]; then
        source ~/.bashrc
    fi

这种情况下,在 login 到机器时,.bashrc 文件也会被执行。

现在新版的 Linux 通常也都已经考虑到了这个问题,通常都会在 .bash_profile 中添加如下的代码:

[root@vsso ~]# cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

所以一般性的共通内容,都可以写入到 .bashrc 文件中。 

 

posted @ 2012-04-27 15:51 hallo 阅读(12) 评论(0) 编辑

2012年4月1日

Linux shell 连续执行命令

在 Linux shell 中,可以使用下面的格式,顺序的批量执行一组命令。

1. 格式一 &&

command 1 && command 2 && command 3

2. 格式二 ;

command 1 ; command 2; command 3

 

 

posted @ 2012-04-01 14:40 hallo 阅读(33) 评论(0) 编辑

2012年3月31日

在 SSH 默认端口 22 被屏蔽时,

hallo@hallo /usr/bin
$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection timed out

可以使用 443 端口访问 github 。

 


 

这里在 cygwin 环境中做演示:

1. 在 /home/hallo/.ssh 目录中添加配置文件 config

Host github.com
User xxxx@gmail.com
Port 443
Hostname ssh.github.com
identityfile ~/.ssh/id_rsa

2. clone 仓库到本地。

kysnail@kysnail /cygdrive/f/Books/gitrepo
$ git clone git@github.com:xxx/xxx.git
Cloning into 'PHP'...
remote: Counting objects: 23, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 23 (delta 0), reused 23 (delta 0)
Receiving objects: 100% (23/23), 3.26 KiB, done.

 

Ref:

Git over port 443

posted @ 2012-03-31 17:22 hallo 阅读(34) 评论(0) 编辑

2012年3月26日

编写模块

经过一个工作周的摸索,已经能够搭建起内核测试环境了。今天又把模块测试环境搭建好了。

搭建模块开发过程中也遇到了很多问题,这里做一个概览性的描述。

1. 内核源码准备

不同于运行在用户态的一般程序,编写 Linux 下的内核程序时,需要引入内核文件。故编写的第一步就是准备内核源码。这几年发行的 Distribution 往往都不在光盘提供,都需要单独下载。同样两大发行版安装源码的方式也有所不同。RedHat 系列需要下载相应的 src*.rpm 源码包进行安装后即可在 /usr/src 中看到源码压缩包。而 Debian 系列可以使用

apt-get install linux-source

获取源码,同样对应的源码包也存放在了 /usr/src 目录。下面的步骤两个版本上的操作就基本没有区别了。

1) 解压源码

tar -jxvf linux-source

2) 准备 .config 文件

关于这个文件的作用在这里也有过描述。随后拷贝 /boot/config-* 文件到 linux-source 目录中。

3) 执行 make oldconfig

make oldconfig

依据本机原有的 .config 文件对当前的源码进行配置设定。

4) make prepare

make prepare

5) make scripts

make scripts

6) 修改源码 Makefile 文件

修改 Makefile 前四行的内容,使其与 /lib/modules/`uname -r`/build/ 中的内容保持一致。

VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 32
EXTRAVERSION = .15+drm33.5

2. 创建 /usr/src/linux 链接

通常默认情况下会使用 /usr/src/linux 作为源码的存放目录,但为了灵活起见,也为了多内核操作的方便,通常是创建一个链接指向 /usr/src/linux 目录。

ln -s /usr/src/linux-source /usr/src/linux

3. 第一个内核 demo

/* hello.c – Our First Driver code */#
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static int __init ofd_init(void) /* Constructor */
{
printk(KERN_INFO "kysnail: hello registered");
return 0;
}

static void __exit ofd_exit(void) /* Destructor */
{
printk(KERN_INFO "kysnail: hello unregistered");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kysnail@gmail.com");

对应的 Makefile

# Makefile – makefile of our first driver 

# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.

ifneq (${KERNELRELEASE},)
hello-m := hello.o

# Otherwise we were called directly from the command line.
# Invoke the kernel build system.

else
KERNEL_SOURCE := /usr/src/linux
PWD := $(shell pwd)
default:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif

4. 内核载入、卸载测试。

sudo insmod hello.ko
sudo rmmod hello
sudo lsmod | grep hello

tail -f /var/log/messages # 动态追踪 /var/log/messages 文件



posted @ 2012-03-26 08:26 hallo 阅读(13) 评论(0) 编辑

2012年3月25日

linux 查看文件系统类型

Linux 查看文件系统的方式有多种,列举如下:

 

1. mount

:~$ mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro,user_xattr)
proc on /proc type proc (rw,noexec,nosuid,nodev)
none on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
none on /dev type devtmpfs (rw,mode=0755)
none on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
none on /dev/shm type tmpfs (rw,nosuid,nodev)
none on /var/run type tmpfs (rw,nosuid,mode=0755)
none on /var/lock type tmpfs (rw,noexec,nosuid,nodev)
none on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
none on /var/lib/ureadahead/debugfs type debugfs (rw,relatime)
none on /proc/fs/vmblock/mountPoint type vmblock (rw)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
gvfs-fuse-daemon on /home/kysnail/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=kysnail)
:~$

2. df

:~$ df -lhT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/sda1 ext4 19G 11G 7.8G 57% /
none devtmpfs 498M 248K 497M 1% /dev
none tmpfs 502M 252K 501M 1% /dev/shm
none tmpfs 502M 96K 502M 1% /var/run
none tmpfs 502M 0 502M 0% /var/lock
none tmpfs 502M 0 502M 0% /lib/init/rw
none debugfs 19G 11G 7.8G 57% /var/lib/ureadahead/debugfs
:~$

3. fdisk

:~$ sudo fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00077544

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 40105983 20051968 83 Linux
/dev/sda2 40108030 41940991 916481 5 Extended
/dev/sda5 40108032 41940991 916480 82 Linux swap / Solaris

Command (m for help): q

4. file

:~$ sudo file -s /dev/sda
/dev/sda: x86 boot sector; partition 1: ID=0x83, active, starthead 32, startsector 2048, 40103936 sectors; partition 2: ID=0x5, starthead 254, startsector 40108030, 1832962 sectors, code offset 0x63
kysnail@ubunkysnail:~$ sudo file -s /dev/sda1
/dev/sda1: Linux rev 1.0 ext4 filesystem data, UUID=4942da40-8a49-4bfd-9dc2-45c906d48413 (needs journal recovery) (extents) (large files) (huge files)
:~$

5. parted

:~$ sudo parted
GNU Parted 2.2
使用 /dev/sda
欢迎使用 GNU Parted! 输入 'help'可获得命令列表.
(parted) p
Model: VMware, VMware Virtual S (scsi)
磁盘 /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
分区表:msdos

数字 开始: End 大小 类型 文件系统 标志
1 1049kB 20.5GB 20.5GB primary ext4 启动
2 20.5GB 21.5GB 938MB extended
5 20.5GB 21.5GB 938MB logical linux-swap(v1)

(parted)

6. 查看 fstab 

# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc nodev,noexec,nosuid 0 0
# / was on /dev/sda1 during installation
UUID=4942da40-8a49-4bfd-9dc2-45c906d48413 / ext4 errors=remount-ro,user_xattr 0 1
# swap was on /dev/sda5 during installation
UUID=935fb95d-771f-448e-9d23-4820106e1783 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0






 

posted @ 2012-03-25 23:22 hallo 阅读(263) 评论(0) 编辑

2012年3月20日

摘要: 在 Cygwin 中安装完 git 后,如果直接连接到 github 直接进行仓库操作,会报如下的错误。Administrator@99638e047c1e4a0 /cygdrive/f/Project/AutoIt$ git pullerror: cannot run ssh: No such file or directoryfatal: unable to fork查看用户主目录,确实没有对应的 .ssh 目录。解决方法:开启 setup.exe 程序,找到 openssh 进行安装。安装后的配置操作,可以参考这个链接。阅读全文

posted @ 2012-03-20 03:02 hallo 阅读(44) 评论(0) 编辑

2012年3月17日

摘要: Cygwin 安装 gitCygwin 安装比较容易,标准的 Windows 系统安装过程。下面主要记录配置过程:1. 查看 Cygwin 版本2. 访问 Windows 盘符使用 mount 命令可以看到 Windows 系统的各个盘符被挂在到了 /cygdrive 目录下。如果对于目录有迷惑,可以使用 cygpath 进行格式的转换。Administrator@99638e047c1e4a0 ~$ cygpath -w ~/C:\cygwin\home\Administrator\Administrator@99638e047c1e4a0 ~$ cygpath -u C:\\Windows阅读全文

posted @ 2012-03-17 17:52 hallo 阅读(108) 评论(0) 编辑

2012年3月16日

摘要: .gitignore对于项目中产生的中间文件、测试文件、可执行文件等,这类不需要被 git 所监控的文件,都可以使用 .gitignore 进行忽略设定。下面以 VS2008 项目为例,设置 .gitignore 文件。项目目录结构| README|\---MergingData | .gitignore | MergingData.sln | \---MergingData | CodeFile1.cs | favicon.ico | Form1.cs | Form1.Designer...阅读全文

posted @ 2012-03-16 12:52 hallo 阅读(224) 评论(0) 编辑