[Linux秘籍31] PS3 - 在shell脚本中的提示符
在shell脚本中,你能够定义一个自定义的提示符,举个例子如下:
当没有定义PS3的时候
jcai@jcai-notebook:~/tmp$ cat ps3.sh select i in mon tue wed exit do case $i in mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;; esac done jcai@jcai-notebook:~/tmp$ ./ps3.sh 1) mon 2) tue 3) wed 4) exit #? 1 Monday #? 2 Tuesday #? 4 |
当定义了PS3的时候如下:
jcai@jcai-notebook:~/tmp$ cat ps3.sh PS3="请选择 1-4:" select i in mon tue wed exit do case $i in mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;; esac done jcai@jcai-notebook:~/tmp$ ./ps3.sh 1) mon 2) tue 3) wed 4) exit 请选择 1-4:2 Tuesday 请选择 1-4:4 |
[命令集3.6]挂载/重挂载一个文件系统
举个 cdrom 的例子。如果已经列于 /etc/fstab 中:
# mount /cdrom
或在 /dev/ 中查找设备,亦或使用 dmesg 命令
FreeBSD
# mount -v -t cd9660 /dev/cd0c /mnt # cdrom # mount_cd9660 /dev/wcd0c /cdrom # 另外一个方法 # mount -v -t msdos /dev/fd0c /mnt # 软驱
/etc/fstab 中的一条:
# Device Mountpoint FStype Options Dump Pass# /dev/acd0 /cdrom cd9660 ro,noauto 0 0
要允许用户做这些,可以这么做:
# sysctl vfs.usermount=1 # 或者在 /etc/sysctl.conf 中插入一条 "vfs.usermount=1"
Linux
# mount -t auto /dev/cdrom /mnt/cdrom # 典型的 cdrom 挂载命令 # mount /dev/hdc -t iso9660 -r /cdrom # IDE # mount /dev/sdc0 -t iso9660 -r /cdrom # SCSI
/etc/fstab 中的条目:
/dev/cdrom /media/cdrom subfs noauto,fs=cdfss,ro,procuid,nosuid,nodev,exec 0 0
用 Linux 挂载一个 FreeBSD 分区
用 fdisk 查找分区号,这通常是 root 分区,但也可能是其他 BSD slice。如果 FreeBSD 有许多 slice,他们
不列于同一个 fdisk 分区表中,但可见于 /dev/sda* 或 /dev/hda* 中。
# fdisk /dev/sda # 查找 FreeBSD 分区 /dev/sda3 * 5357 7905 20474842+ a5 FreeBSD # mount -t ufs -o ufstype=ufs2,ro /dev/sda3 /mnt /dev/sda10 = /tmp; /dev/sda11 /usr # 其他 slice
重挂载
不用卸载一个设备来重挂载。 对 fsck 来说是必须的。举个例子:
# mount -o remount,ro / # Linux # mount -o ro / # FreeBSD
从 cdrom 拷贝原始数据进一个 iso 映像文件:
# dd if=/dev/cd0c of=file.isoTags: command, Hack, Linux
[linux秘笈30]. PS2 - 持续交互的提示符
A very long command can be broken down to multiple lines by giving \ at the
end of the line. The default interactive prompt for a multi-line command is
“> “. Let us change this default behavior to display “continue->” by using PS2
environment variable as shown below.
ramesh@dev-db ~> myisamchk –silent –force –fast –
update-state \
> –key_buffer_size=512M –sort_buffer_size=512M \
> –read_buffer_size=4M –write_buffer_size=4M \
> /var/lib/mysql/bugs/*.MYI
[Note: This uses the default ">" for continuation
prompt]
ramesh@dev-db ~> export PS2=”continue-> ”
ramesh@dev-db ~> myisamchk –silent –force –fast –
update-state \
continue-> –key_buffer_size=512M –
sort_buffer_size=512M \
continue-> –read_buffer_size=4M –write_buffer_size=4M
\
continue-> /var/lib/mysql/bugs/*.MYI
[Note: This uses the modified "continue-> " for continuation prompt]
I found it very helpful and easy to read, when I break my long commands into
multiple lines using \. I have also seen others who don’t like to break-up long
commands.
[命令集3.4]系统挂载点/磁盘使用情况
# mount | column -t # 显示系统已挂载分区情况 # df # 显示磁盘剩余空间和挂载的设备 # cat /proc/partitions # 显示所有设备的所有分区(Linux) |
[命令集3.3]FreeBSD下的启动引导
如果新内核不能引导,要引导一个旧内核,停止启动倒计时,做如下动作:
# unload # load kernel.old # boot |
[linux秘笈27]Diff命令
diff命令,通常用来比较两个文件的差别,命令的输出比较隐晦,不能直接阅读。使用方法
Syntax: diff [options] file1 file2 |
具体的使用方法
jcai@jcai-notebook:~$ diff --help Usage: diff [OPTION]... FILES Compare files line by line. -i --ignore-case Ignore case differences in file contents. --ignore-file-name-case Ignore case when comparing file names. --no-ignore-file-name-case Consider case when comparing file names. -E --ignore-tab-expansion Ignore changes due to tab expansion. -b --ignore-space-change Ignore changes in the amount of white space. -w --ignore-all-space Ignore all white space. -B --ignore-blank-lines Ignore changes whose lines are all blank. -I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE. --strip-trailing-cr Strip trailing carriage return on input. -a --text Treat all files as text. -c -C NUM --context[=NUM] Output NUM (default 3) lines of copied context. -u -U NUM --unified[=NUM] Output NUM (default 3) lines of unified context. --label LABEL Use LABEL instead of file name. -p --show-c-function Show which C function each change is in. -F RE --show-function-line=RE Show the most recent line matching RE. -q --brief Output only whether files differ. -e --ed Output an ed script. --normal Output a normal diff. -n --rcs Output an RCS format diff. -y --side-by-side Output in two columns. -W NUM --width=NUM Output at most NUM (default 130) print columns. --left-column Output only the left column of common lines. --suppress-common-lines Do not output common lines. -D NAME --ifdef=NAME Output merged file to show `#ifdef NAME' diffs. --GTYPE-group-format=GFMT Similar, but format GTYPE input groups with GFMT. --line-format=LFMT Similar, but format all input lines with LFMT. --LTYPE-line-format=LFMT Similar, but format LTYPE input lines with LFMT. LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'. GFMT may contain: %< lines from FILE1 %> lines from FILE2 %= lines common to FILE1 and FILE2 %[-][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER LETTERs are as follows for new group, lower case for old group: F first line number L last line number N number of lines = L-F+1 E F-1 M L+1 LFMT may contain: %L contents of line %l contents of line, excluding any trailing newline %[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number Either GFMT or LFMT may contain: %% % %c'C' the single character C %c'\OOO' the character with octal code OOO -l --paginate Pass the output through `pr' to paginate it. -t --expand-tabs Expand tabs to spaces in output. -T --initial-tab Make tabs line up by prepending a tab. -r --recursive Recursively compare any subdirectories found. -N --new-file Treat absent files as empty. --unidirectional-new-file Treat absent first files as empty. -s --report-identical-files Report when two files are the same. -x PAT --exclude=PAT Exclude files that match PAT. -X FILE --exclude-from=FILE Exclude files that match any pattern in FILE. -S FILE --starting-file=FILE Start with FILE when comparing directories. --from-file=FILE1 Compare FILE1 to all operands. FILE1 can be a directory. --to-file=FILE2 Compare all operands to FILE2. FILE2 can be a directory. --horizon-lines=NUM Keep NUM lines of the common prefix and suffix. -d --minimal Try hard to find a smaller set of changes. --speed-large-files Assume large files and many scattered small changes. -v --version Output version info. --help Output this help. FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or `FILE... DIR'. If --from-file or --to-file is given, there are no restrictions on FILES. If a FILE is `-', read standard input. |
当用我的新文件去和老文件比较那些发生了修改?
当执行比较的时候,在diff命令中使用选项 -w 能够忽略掉空格,
查看diff输出时候的注意是:
- ‘—’ 线上面的是第一个文件发生的变化
- ‘—’线向下的第二个文件发生的变化
- < 符号表示的内容,属于第一个文件,>符号表示的内容属于第二个文件
jcai@jcai-notebook:~/tmp$ cat test.txt 我们 你们现在还好吗?比较文本 jcai@jcai-notebook:~/tmp$ cat test2.txt 他们 我们 你们现在还好吗? jcai@jcai-notebook:~/tmp$ diff test.txt test2.txt 0a1 > 他们 2c3 < 你们现在还好吗?比较文本 --- > 你们现在还好吗? |
[linux秘笈26]Stat命令
Stat命令被用来查看单个文件或者文件系统的状态以及属性。
显示某一个文件或者文件系统的状态
jcai@jcai-notebook:~$ stat /etc/mysql/my.cnf File: `/etc/mysql/my.cnf' Size: 3978 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 449073 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2009-03-18 21:31:03.000000000 +0800 Modify: 2009-03-04 18:08:03.000000000 +0800 Change: 2009-03-04 18:08:03.000000000 +0800 jcai@jcai-notebook:~$ stat /home/jcai File: `/home/jcai' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: fe00h/65024d Inode: 2187265 Links: 66 Access: (0755/drwxr-xr-x) Uid: ( 1000/ jcai) Gid: ( 1000/ jcai) Access: 2009-03-19 08:14:47.000000000 +0800 Modify: 2009-03-19 09:22:12.000000000 +0800 Change: 2009-03-19 09:22:12.000000000 +0800 |
使用 -f 选项来战士文件系统的状态
jcai@jcai-notebook:~$ stat -f / File: "/" ID: f346283e2da86295 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 4845177 Free: 3879940 Available: 3635752 Inodes: Total: 1224000 Free: 1082670 |
[命令集3.1]linux文件的权限
用 chmod 和 chown 更改访问权限和所有权。对于所有用户的默认掩码(umask)可以在 /etc/profile (Linux)
或 /etc/login.conf (FreeBSD) 中修改。其默认掩码(umask)通常为 022。掩码可以和777做减法,从而得到755
的权限。
1 –x 执行 # Mode 764 = 执行/读/写 | 读/写 | 读
2 -w- 写 # |—所有者|—用户组|—其他用户|
4 r– 读
ugo=a u=所有者, g=用户组, o=其他用户, a=所有用户
# chmod [OPTION] MODE[,MODE] FILE # MODE 可以是 [ugoa]*([-+=]([rwxXst])) # chmod 640 /var/log/maillog # 更改 maillog 访问权限为 -rw-r----- # chmod u=rw,g=r,o= /var/log/maillog # 同上 # chmod -R o-r /home/* # 递归去除所有其他用户的可读权限 在可执行位设置 SUID (知道你在干什么!13) # chmod u+s /path/to/prog # # find / -perm -u+s -print # 查找所有设置过 SUID 位的程序 # chown user:group /path/to/file # 改变文件的所有者和文件关联的组 # chgrp group /path/to/file # 改变文件关联的组 # chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files # chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories |
Linux秘笈25.Linux的Cut命令
Cut command can be used to display only specific columns from a text file or
other command outputs.
Following are some of the examples.
Display the 1st field (employee name) from a colon delimited file
$ cut -d: -f 1 names.txt Emma Thomas Alex Jason Madison Randy Sanjay Gupta Nisha Singh |
Display 1st and 3rd field from a colon delimited file
$ cut -d: -f 1,3 names.txt Emma Thomas:Marketing Alex Jason:Sales Madison Randy:Product Development Sanjay Gupta:Support Nisha Singh:Sales |
Display only the first 8 characters of every line in a file
$ cut -c 1-8 names.txt Emma Tho Alex Jas Madison Sanjay G Nisha Si |
