关于分区表错乱(叠加)的修复问题

2013年7月8日 Code007 评论已被关闭
分类: IT 常识 标签:

二记

2013年1月15日 Code007 没有评论

1月15 无法忘记的承诺,似乎每天总会会想起,发自内心,无法磨灭。。。

分类: 未分类 标签:

git 后出现以下^M的情况

2012年8月18日 Code007 没有评论

git 后出现以下^M的情况按以下情况解决:

摘自http://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/
Windows Git Tip: Hide ^M (Carriage Return) in Diff
A common point of confusion when getting started with Git on Windows is line endings, with Windows still using CR+LF while every other modern OS uses LF only. Git provides three ways to deal with this discrepancy, as described in the msysGit installer:

Checkout Windows-style, commit Unix-style (core.autocrlf = true)
Checkout as-is, commit Unix-style (core.autocrlf = input)
Checkout as-is, commit as-is (core.autocrlf = false)
The first option is the default, which I find rather unfortunate—I don’t consider line ending manipulation to be the responsibility of my VCS. Instead, I prefer to keep core.autocrlf set to false and let my text editors deal with line endings. (If you like having core.autocrlf set to true or input, I’d love to hear why.)

One downside of turning off autocrlf is that the output of git diff highlights CR characters (indicated by ^M) as whitespace errors. To turn off this “error”, you can use the core.whitespace setting:

git config –global core.whitespace cr-at-eol
If your core.whitespace is already set, you should add cr-at-eol to the end of the comma-delimited list instead.

分类: 备忘 标签:

3.Freescale I.MX51 bbg Uboot —-Makefile

2012年7月25日 Code007 没有评论

续第二篇文章,在I.MX51中用以下命令编译uboot:

$ cd ~/myandroid/bootable/bootloader/uboot-imx

$ export ARCH=arm
$ export CROSS_COMPILE=~/myandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin/arm-eabi-

Command to build for i.MX51 BBG board is:
$ make distclean
$ make mx51_bbg_android_config
$ make

接下来主要解析一下uboot中的Makefile文件,Makefile作为迷宫的地图是必须得看的。看一下注释:

VERSION = 2009 #看以下信息
PATCHLEVEL = 08
SUBLEVEL =
EXTRAVERSION =
#判断SUBLEVEL是否为空,该uboot的版本为:2009.08$(EXTRAVERSION)
ifneq "$(SUBLEVEL)" ""
U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
else
U_BOOT_VERSION = $(VERSION).$(PATCHLEVEL)$(EXTRAVERSION)
endif
#timestamp_autogenerated.h在makefile下面会有一个地方生成的uboot时间并写到该文件中,格式如下
##define U_BOOT_DATE "May 17 2012"
##define U_BOOT_TIME "09:45:08"
TIMESTAMP_FILE = $(obj)include/timestamp_autogenerated.h
#在makefile下面会有一个地方生成uboot版本号并写到version_autogenerated.h,格式如下
##define U_BOOT_VERSION "U-Boot 2009.08-00322-gf654a73"
VERSION_FILE = $(obj)include/version_autogenerated.h 

##########################################
#Versioning:#这部分摘自README中对uboot版本的解释
#U-Boot版本信息由version, sub-version和patchlevel组成。
#例如”U-Boot-2.34.5”表示version “2”, sub-version “34”, 和patchlevel “4”.
#对于正式的发布版本来说,patchlevel总为“0”.
#===========
#
#U-Boot uses a 3 level version number containing a version, a
#sub-version, and a patchlevel: "U-Boot-2.34.5" means version "2",
#sub-version "34", and patchlevel "4".
#
#The patchlevel is used to indicate certain stages of development
#between released versions, i. e. officially released versions of
#U-Boot will always have a patchlevel of "0".
########################################

#HOSTARCH 执行uname -m后为i686(不同平台是不一样的)
#通过管道传送给并执行sed -e s/i.86/i386/ 等进行匹配替换,
#这里是将I686替换成i386,即HOSTARCH=i386.
#sed -e 的用法:sed -e 's/Code007/uboot/' 即把Code007替换成uboot
HOSTARCH := $(shell uname -m | \
	sed -e s/i.86/i386/ \
	    -e s/sun4u/sparc64/ \
	    -e s/arm.*/arm/ \
	    -e s/sa110/arm/ \
	    -e s/powerpc/ppc/ \
	    -e s/ppc64/ppc/ \
	    -e s/macppc/ppc/)

#这里使用uname -s 得到host os为 Linux,再通过管道tr '[:upper:]' '[:lower:]'大写变成小写,
#sed -e 跟上面的用法一样,最后为linux
HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
	    sed -e 's/\(cygwin\).*/cygwin/')

# Set shell to bash if possible, otherwise fall back to sh
#如果存在环境变量$BASH并且该文件为-x,即可执行文件,那么输出$BASH
#如果/bin/bash为可执行文件则输出/bin/bash
#不然最后输出sh
SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	else if [ -x /bin/bash ]; then echo /bin/bash; \
	else echo sh; fi; fi)
#以下为export命令就不多讲了
export	HOSTARCH HOSTOS SHELL

# Deal with colliding definitions from tcsh etc.
#vendor是什么,例如Marvell,freescale等厂商
VENDOR=

# Allow for silent builds
#是否进行silent 编译,下面这几行主要是在MAKEFLAGS进行findstring s,要是有就进行silent编译,在递归编译中禁止-w的自动打开,-#w的作用是显示以下信息,要是在make -s后将禁止类似以下信息
#make[1]: Entering directory `/home/code007/Documents/test/imx51_android/android-#2.3.7_r1/bootable/bootloader/uboot-imx/tools'
#make[1]: Leaving directory `/home/code007/Documents/test/imx51_android/android-#2.3.7_r1/bootable/bootloader/uboot-imx/tools'
#如下
#make:Entering directory 。。。
#make:Leaving directory 。。。
ifeq (,$(findstring s,$(MAKEFLAGS)))
XECHO = echo
else
XECHO = :
endif

#下面这个我就不说了,英文够详细了
#########################################################################
#
# U-boot build supports producing a object files to the separate external
# directory. Two use cases are supported:
#
# 1) Add O= to the make command line
# 'make O=/tmp/build all'
#
# 2) Set environement variable BUILD_DIR to point to the desired location
# 'export BUILD_DIR=/tmp/build'
# 'make'
#
# The second approach can also be used with a MAKEALL script
# 'export BUILD_DIR=/tmp/build'
# './MAKEALL'
#
# Command line 'O=' setting overrides BUILD_DIR environent variable.
#
# When none of the above methods is used the local build is performed and
# the object files are placed in the source directory.
#

ifdef O
ifeq ("$(origin O)", "command line")
BUILD_DIR := $(O)
endif
endif

#如果${BUILD_DIR}不等于空的话saved-output= $(BUILD_DIR),并检查如果没有定义则创建${BUILD_DIR}该目录
ifneq ($(BUILD_DIR),)
saved-output := $(BUILD_DIR)

# Attempt to create a output directory.
$(shell [ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR})

# Verify if it was successful.
BUILD_DIR := $(shell cd $(BUILD_DIR) && /bin/pwd)
$(if $(BUILD_DIR),,$(error output directory "$(saved-output)" does not exist))
endif # ifneq ($(BUILD_DIR),)
# $(BUILD_DIR)为空,则$(BUILD_DIR)等于$(CURDIR),这里$(CURDIR)是makefile 内置变量表示当前进行make的路径
#这里应该是/home/code007/Documents/test/imx51_android/android-2.3.7_r1/bootable/bootloader/uboot-imx
#当BUILD_DIR为空时执行完后以下变量的值分别为:
#OBJTREE,SRCTREE,TOPDIR,LNDIR都为/home/code007/Documents/test/imx51_android/android-#2.3.7_r1/bootable/bootloader/uboot-imx,
#MKCONFIG为/home/code007/Documents/test/imx51_android/android-#2.3.7_r1/bootable/bootloader/uboot-imx/mkconfig

OBJTREE		:= $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR))
SRCTREE		:= $(CURDIR)
TOPDIR		:= $(SRCTREE)
LNDIR		:= $(OBJTREE)
export	TOPDIR SRCTREE OBJTREE

MKCONFIG	:= $(SRCTREE)/mkconfig
export MKCONFIG

ifneq ($(OBJTREE),$(SRCTREE))
REMOTE_BUILD	:= 1
export REMOTE_BUILD
endif

##下面这些我就不讲了,英文注释非常详细,主要是从config.mk中获取一些变量的的值把它include进来
# $(obj) and (src) are defined in config.mk but here in main Makefile
# we also need them before config.mk is included which is the case for
# some targets like unconfig, clean, clobber, distclean, etc.
ifneq ($(OBJTREE),$(SRCTREE))
obj := $(OBJTREE)/
src := $(SRCTREE)/
else
obj :=
src :=
endif
export obj src

# Make sure CDPATH settings don't interfere
unexport CDPATH

#########################################################################

ifeq ($(ARCH),powerpc)
ARCH = ppc
endif

# The "tools" are needed early, so put this first
# Don't include stuff already done in $(LIBS)
SUBDIRS	= tools \
	  examples/standalone \
	  examples/api

.PHONY : $(SUBDIRS)

ifeq ($(obj)include/config.mk,$(wildcard $(obj)include/config.mk))

# Include autoconf.mk before config.mk so that the config options are available
# to all top level build files.  We need the dummy all: target to prevent the
# dependency target in autoconf.mk.dep from being the default.
all:
sinclude $(obj)include/autoconf.mk.dep
sinclude $(obj)include/autoconf.mk

# load ARCH, BOARD, and CPU configuration
include $(obj)include/config.mk
export	ARCH CPU BOARD VENDOR SOC

# set default to nothing for native builds
ifeq ($(HOSTARCH),$(ARCH))
CROSS_COMPILE ?=
endif

# load other configuration
include $(TOPDIR)/config.mk

#大概从168行到290行主要是加入链接文件,动态库什么的。在这里就不展开说明,
#这里我们进行执行make的时候默认就是执行make all也就是说执行下面all这个target。all 依赖于宏ALL,这个make all后将会在
#BUILD_DIR生成 $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND),
#这里u-boot.hex,u-boot.srec,u-boot.bin,u-boot.ldr,u-boot.dis都是根据ELF格式的UBOOT调用objcopy命令来生成。
#这里我找里一篇比较好的文章下面是链接地址:
#<a href="http://docs.blackfin.uclinux.org/doku.php?id=bootloaders:u-boot:compiling">
#下面是部分截图(下面的objcopy工具链改成我们的工具链就可以了):


# Always append ALL so that arch config.mk's can add custom ones
ALL += $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) $(U_BOOT_ONENAND)

all:		$(ALL)

$(obj)u-boot.hex:	$(obj)u-boot
		$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@

$(obj)u-boot.srec:	$(obj)u-boot
		$(OBJCOPY) -O srec $< $@

$(obj)u-boot.bin:	$(obj)u-boot
		$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

$(obj)u-boot.ldr:	$(obj)u-boot
		$(obj)tools/envcrc --binary > $(obj)env-ldr.o
		$(LDR) -T $(CONFIG_BFIN_CPU) -c $@ $< $(LDR_FLAGS)

$(obj)u-boot.ldr.hex:	$(obj)u-boot.ldr
		$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@ -I binary

$(obj)u-boot.ldr.srec:	$(obj)u-boot.ldr
		$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@ -I binary

$(obj)u-boot.img:	$(obj)u-boot.bin
		./tools/mkimage -A $(ARCH) -T firmware -C none \
		-a $(TEXT_BASE) -e 0 \
		-n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | \
			sed -e 's/"[	 ]*$$/ for $(BOARD) board"/') \
		-d $< $@

$(obj)u-boot.sha1:	$(obj)u-boot.bin
		$(obj)tools/ubsha1 $(obj)u-boot.bin

$(obj)u-boot.dis:	$(obj)u-boot
		$(OBJDUMP) -d $< > $@

#其实这里上面那么多最重要的就是u-boot,因为所有的什么u-boot.hex,u-boot.bin等都是通过
#ELF格式的u-boot来转换,所以下面我们要看一下下面这个u-boot,u-boot依赖于
#depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds $(GEN_UBOOT)
#这些宏在下面都进行了定义,通过depend把这些都include了进来。

GEN_UBOOT = \
		UNDEF_SYM=`$(OBJDUMP) -x $(LIBBOARD) $(LIBS) | \
		sed  -n -e 's/.*\($(SYM_PREFIX)__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \
			--start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \
			-Map u-boot.map -o u-boot

$(obj)u-boot:	depend $(SUBDIRS) $(OBJS) $(LIBBOARD) $(LIBS) $(LDSCRIPT) $(obj)u-boot.lds
		$(GEN_UBOOT)
ifeq ($(CONFIG_KALLSYMS),y)
		smap=`$(call SYSTEM_MAP,u-boot) | \
			awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "\\\\000"}'` ; \
		$(CC) $(CFLAGS) -DSYSTEM_MAP="\"$${smap}\"" \
			-c common/system_map.c -o $(obj)common/system_map.o
		$(GEN_UBOOT) $(obj)common/system_map.o
endif

#上面省略的168行到290行中有定义如下
#在我们上面一篇mkconfig,定义的CPU中的值为CPU    = arm_cortexa8
#OBJS  = cpu/$(CPU)/start.o

$(OBJS):	depend
		$(MAKE) -C cpu/$(CPU) $(if $(REMOTE_BUILD),$@,$(notdir $@))

$(LIBS):	depend $(SUBDIRS)
		$(MAKE) -C $(dir $(subst $(obj),,$@))

$(LIBBOARD):	depend $(LIBS)
		$(MAKE) -C $(dir $(subst $(obj),,$@))

#上边SUBDIRS已定义过如下
#SUBDIRS	= tools \
#	  examples/standalone \
#	  examples/api
#下面命令将会对以上目录进行make all
$(SUBDIRS):	depend
		$(MAKE) -C $@ all

$(LDSCRIPT):	depend
		$(MAKE) -C $(dir $@) $(notdir $@)

$(obj)u-boot.lds: $(LDSCRIPT)
		$(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@

$(NAND_SPL):	$(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk
		$(MAKE) -C nand_spl/board/$(BOARDDIR) all

$(U_BOOT_NAND):	$(NAND_SPL) $(obj)u-boot.bin
		cat $(obj)nand_spl/u-boot-spl-16k.bin $(obj)u-boot.bin > $(obj)u-boot-nand.bin

$(ONENAND_IPL):	$(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk
		$(MAKE) -C onenand_ipl/board/$(BOARDDIR) all

$(U_BOOT_ONENAND):	$(ONENAND_IPL) $(obj)u-boot.bin
		cat $(obj)onenand_ipl/onenand-ipl-2k.bin $(obj)u-boot.bin > $(obj)u-boot-onenand.bin
		cat $(obj)onenand_ipl/onenand-ipl-4k.bin $(obj)u-boot.bin > $(obj)u-boot-flexonenand.bin

$(VERSION_FILE):
		@( printf '#define U_BOOT_VERSION "U-Boot %s%s"\n' "$(U_BOOT_VERSION)" \
		 '$(shell $(TOPDIR)/tools/setlocalversion $(TOPDIR))' ) > $@.tmp
		@cmp -s $@ $@.tmp && rm -f $@.tmp || mv -f $@.tmp $@

$(TIMESTAMP_FILE):
		@date +'#define U_BOOT_DATE "%b %d %C%y"' > $@
		@date +'#define U_BOOT_TIME "%T"' >> $@

gdbtools:
		$(MAKE) -C tools/gdb all || exit 1

updater:
		$(MAKE) -C tools/updater all || exit 1

env:
		$(MAKE) -C tools/env all MTD_VERSION=${MTD_VERSION} || exit 1

depend dep:	$(TIMESTAMP_FILE) $(VERSION_FILE) $(obj)include/autoconf.mk
		for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir _depend ; done

TAG_SUBDIRS += include
TAG_SUBDIRS += lib_generic board/$(BOARDDIR)
TAG_SUBDIRS += cpu/$(CPU)
TAG_SUBDIRS += lib_$(ARCH)
TAG_SUBDIRS += fs/cramfs
TAG_SUBDIRS += fs/fat
TAG_SUBDIRS += fs/fdos
TAG_SUBDIRS += fs/jffs2
TAG_SUBDIRS += fs/yaffs2
TAG_SUBDIRS += net
TAG_SUBDIRS += disk
TAG_SUBDIRS += common
TAG_SUBDIRS += drivers/bios_emulator
TAG_SUBDIRS += drivers/block
TAG_SUBDIRS += drivers/gpio
TAG_SUBDIRS += drivers/hwmon
TAG_SUBDIRS += drivers/i2c
TAG_SUBDIRS += drivers/input
TAG_SUBDIRS += drivers/misc
TAG_SUBDIRS += drivers/mmc
TAG_SUBDIRS += drivers/mtd
TAG_SUBDIRS += drivers/mtd/nand
TAG_SUBDIRS += drivers/mtd/onenand
TAG_SUBDIRS += drivers/mtd/spi
TAG_SUBDIRS += drivers/net
TAG_SUBDIRS += drivers/net/sk98lin
TAG_SUBDIRS += drivers/pci
TAG_SUBDIRS += drivers/pcmcia
TAG_SUBDIRS += drivers/qe
TAG_SUBDIRS += drivers/rtc
TAG_SUBDIRS += drivers/serial
TAG_SUBDIRS += drivers/spi
TAG_SUBDIRS += drivers/usb
TAG_SUBDIRS += drivers/video

tags ctags:
		ctags -w -o $(obj)ctags `find $(SUBDIRS) $(TAG_SUBDIRS) \
						-name '*.[ch]' -print`

etags:
		etags -a -o $(obj)etags `find $(SUBDIRS) $(TAG_SUBDIRS) \
						-name '*.[ch]' -print`
cscope:
		find $(SUBDIRS) $(TAG_SUBDIRS) -name '*.[ch]' -print \
						> cscope.files
		cscope -b -q -k

SYSTEM_MAP = \
		$(NM) $1 | \
		grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \
		LC_ALL=C sort
$(obj)System.map:	$(obj)u-boot
		@$(call SYSTEM_MAP,$<) > $(obj)System.map

#
# Auto-generate the autoconf.mk file (which is included by all makefiles)
#
# This target actually generates 2 files; autoconf.mk and autoconf.mk.dep.
# the dep file is only include in this top level makefile to determine when
# to regenerate the autoconf.mk file.
$(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h
	@$(XECHO) Generating $@ ; \
	set -e ; \
	: Generate the dependancies ; \
	$(CC) -x c -DDO_DEPS_ONLY -M $(HOSTCFLAGS) $(CPPFLAGS) \
		-MQ $(obj)include/autoconf.mk include/common.h > $@

$(obj)include/autoconf.mk: $(obj)include/config.h
	@$(XECHO) Generating $@ ; \
	set -e ; \
	: Extract the config macros ; \
	$(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \
		sed -n -f tools/scripts/define2mk.sed > $@.tmp && \
	mv $@.tmp $@

#########################################################################
else	# !config.mk
all $(obj)u-boot.hex $(obj)u-boot.srec $(obj)u-boot.bin \
$(obj)u-boot.img $(obj)u-boot.dis $(obj)u-boot \
$(SUBDIRS) $(TIMESTAMP_FILE) $(VERSION_FILE) gdbtools updater env depend \
dep tags ctags etags cscope $(obj)System.map:
	@echo "System not configured - see README" >&2
	@ exit 1
endif	# config.mk

.PHONY : CHANGELOG
CHANGELOG:
	git log --no-merges U-Boot-1_1_5.. | \
	unexpand -a | sed -e 's/\s\s*$$//' > $@

include/license.h: tools/bin2header COPYING
	 cat COPYING | gzip -9 -c | ./tools/bin2header license_gzip > include/license.h
#########################################################################

unconfig:
	@rm -f $(obj)include/config.h $(obj)include/config.mk \
		$(obj)board/*/config.tmp $(obj)board/*/*/config.tmp \
		$(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep

未完再续!

转载时请注明出处和作者
文章出处:http://www.code007.org/
作者:Code007

分类: Android 标签:

shell命令备忘录 — comm

2012年7月19日 Code007 没有评论

文件交集差集:
以下面文件为例子:

:~/Desktop/SHELL$ cat one.txt
1
2
3
4
52
3
4
5
:~/Desktop/SHELL$ cat two.txt
1
2
5
6
3
7

很明显上面两个文件是没有进行排序过的,所以要是进行comm的话将会提示:

:~/Desktop/SHELL$ comm one.txt two.txt
		1
		2
3
4
	5
52
comm: file 1 is not in sorted order
3
4
5
	6
comm: file 2 is not in sorted order
	3
	7

如果排序方式不一样也将无法会提示:

:~/Desktop/SHELL$ cat one.txt
1
2
3
4
52
3
4
5
:~/Desktop/SHELL$ sort -n one.txt -o one.txt
:~/Desktop/SHELL$ cat one.txt
1
2
3
3
4
4
5
52
:~/Desktop/SHELL$ sort -rn two.txt -o two.txt
:~/Desktop/SHELL$ cat two.txt
7
6
5
3
2
1
:~/Desktop/SHELL$ comm one.txt two.txt
1
2
3
3
4
4
5
52
	7
comm: file 2 is not in sorted order
	6
	5
	3
	2
	1

所以必须进行同样的排序才能进行comm如下:

:~/Desktop/SHELL$ sort -n two.txt -o two.txt
:~/Desktop/SHELL$ comm one.txt two.txt
		1
		2
		3
3
4
4
		5
52
	6
	7

上面第一列为one与two的差集,第二列为two跟one的差集,第三列为他们的交集。
如果想要显示部门列只要减去对应的列即可,如果减去第一列或者是1,3或者是减去2:

:~/Desktop/SHELL$ comm one.txt two.txt -1
	1
	2
	3
	5
6
7
:~/Desktop/SHELL$ comm one.txt two.txt -1 -3
6
7
:~/Desktop/SHELL$ comm one.txt two.txt -2
	1
	2
	3
3
4
4
	5
52
:~/Desktop/SHELL$ 

如果我们想要使用上面减去2的数据做如下操作:

:~/Desktop/SHELL$ comm one.txt two.txt -2
	1
	2
	3
3
4
4
	5
52
:~/Desktop/SHELL$ comm one.txt two.txt -2 | sed 's/^\t//'
1
2
3
3
4
4
5
52
:~/Desktop/SHELL$

sed命令之前在分析uboot的时候有讲到,sed中的s表示替换(subsitute),^为行首符号,/^\t//表示使用//来匹配行首的\t,而//没有字符,所以就之前删除了\t了.

分类: 未分类 标签:

shell命令备忘录 — 扩展名切分文件名

2012年7月19日 Code007 没有评论

获取文件名以及后缀名,这样就可以做批量修改文件名或者后缀名等有关操作:

:~/Desktop/SHELL$ file="myfile.txt" \
> fileName=${file%.*} \
> echo "file name is :$fileName"
file name is :myfile
:~/Desktop/SHELL$ file="myfile.txt" \
>extensionName=${file#*.} \
>echo "file exsentionName is :$extensionName"
file exsentionName is :txt

NOTE:${file%.*}将会删除%后边紧跟所匹配的字符(.*)这里是.txt。如果文件名为myfile.txt.pdf.jpg那么这里匹配的还是.txt,最后获取到为myfile.pdf.jpg。所以这里%属于非贪婪(non-greedy)匹配,%%属于贪婪匹配,如果${file%.*}换成${file%%.*}那么以上面的文件名为例将会获取到myfile。同理#也属于非贪婪匹配,##属于贪婪匹配。以myfile.txt.pdf.jpg为例,非贪婪匹配最后获取为:txt.pdf.jpg,贪婪匹配为jpg,所以在操作获取文件名或者后缀时候最好使用贪婪匹配。

分类: SHELL 标签:

shell命令备忘录 — sort&uniq

2012年7月19日 Code007 没有评论

以下面out.txt为例子:


:~/Desktop/SHELL$ cat out.txt > /tmp/out.$$ # out.$$ 中的.$$会扩展成运行脚本的进程ID
:~/Desktop/SHELL$ cat /tmp/out.3390
     4  log_header.sh
     4  log_header.sh
     4  log_header.sh
     5  log.txt
     6  out.txt
     7  password.sh
     8  root_or_not
     9  sleep.sh
    10  stderr.txt
    11  stdout.txt
    12  success_test.sh
    13  temp.txt
    14  time_take.sh

以第1列为降序数字排列:

:~/Desktop/SHELL$ sort -nrk 1 out.txt
    14  time_take.sh
    13  temp.txt
    12  success_test.sh
    11  stdout.txt
    10  stderr.txt
     9  sleep.sh
     8  root_or_not
     7  password.sh
     6  out.txt
     5  log.txt
     4  log_header.sh
     4  log_header.sh
     4  log_header.sh

以第2列做字母降序升序排列:

:~/Desktop/SHELL$ sort -rk 2  out.txt
    14  time_take.sh
    13  temp.txt
    12  success_test.sh
    11  stdout.txt
    10  stderr.txt
     9  sleep.sh
     8  root_or_not
     7  password.sh
     6  out.txt
     5  log.txt
     4  log_header.sh
     4  log_header.sh
     4  log_header.sh
:~/Desktop/SHELL$ sort -k 2  out.txt
     4  log_header.sh
     4  log_header.sh
     4  log_header.sh
     5  log.txt
     6  out.txt
     7  password.sh
     8  root_or_not
     9  sleep.sh
    10  stderr.txt
    11  stdout.txt
    12  success_test.sh
    13  temp.txt
    14  time_take.sh

显示唯一行:

:~/Desktop/SHELL$ sort -u  out.txt
    10  stderr.txt
    11  stdout.txt
    12  success_test.sh
    13  temp.txt
    14  time_take.sh
     4  log_header.sh
     5  log.txt
     6  out.txt
     7  password.sh
     8  root_or_not
     9  sleep.sh

统计各行在文件中出现的次数:

:~/Desktop/SHELL$ sort   out.txt | uniq -c
      1     10  stderr.txt
      1     11  stdout.txt
      1     12  success_test.sh
      1     13  temp.txt
      1     14  time_take.sh
      3      4  log_header.sh
      1      5  log.txt
      1      6  out.txt
      1      7  password.sh
      1      8  root_or_not
      1      9  sleep.sh
分类: SHELL 标签:

shell命令备忘录 — 操作符

2012年7月17日 Code007 没有评论

备忘:
运算符:

if [ $var -eq 0 ]  && echo "well done";#&& 如果$var等于0为真,则 echo "well done"
if [ $var -eq 0 ]  || echo "well done";#|| 如果$var等于0为假,则 echo "well done"
if [ $var -ne 0  -a $var -ne 9]# -a 逻辑与 $var 不等于0 并且 不等于9
if [ $var -ne 0  -o $var -ne 9]# -a 逻辑与 $var 不等于0 或者 不等于9
-eq(Equal):等于
-ne (not equal):不等于
-gt (greater than):大于
-lt (less than):小于
-ge (greater && equal):大于等于
-le (less && equal):小于等于

文件系统操作:

[ -f $file ] :$file包含正常的文件路径或者文件名,则为真
[ -x $file ] :$file为可执行文件,则为真
[ -d $file ] :$file为目录,则为真
[ -e $file ] :$file文件存在,则为真
[ -c $file ] :$file是一个字符(char)设备文件,则为真
[ -b $file ] :$file是一个块(block)设备文件,则为真
[ -w $file ] :$file为可写文件则为真
[ -r $file ] :$file为可读文件则为真
[ -L $file ] :$file为符号链接文件则为真

字符串:

[[ -z $var ]] :如果$var为空字符串,为真
[[ -n $var ]] :如果$var为非空字符串,为真
分类: SHELL 标签:

shell命令备忘录 — Fork Bomb

2012年7月17日 Code007 没有评论

以下内容摘自WIKI:http://en.wikipedia.org/wiki/Fork_bomb 自己这里做一备忘,下面函数符号.(小数点)原本应该为: (冒号)的这里下面那样写是为提醒自己bash脚本默认处于非 POSIX 模式,所以fork bomb中的冒号会优先当成一个函数进行匹配而不会当成一个内嵌符号。

In computing, the fork bomb is a form of denial-of-service attack against a computer system which makes use of the fork operation (or equivalent functionality) whereby a running process can create another running process

The following well-known example of a fork bomb[4] is executed by pasting the following 13 characters (including spaces) into a UNIX shell such as bash or zsh.

.(){.|.&;};.

Understanding the above:

/- Define the function ':' without any parameters '()' as follows:
| /- Beginning of function-block.
| | /- Load a copy of the function ':' into memory ...
| | |/- ... and pipe its output to ...
| | ||/- ... another copy of the ':'-function, which has to be loaded into memory.
| | |||   (In other words, ':|:' loads two more copies of ':', thus causing a chain-reaction)
| | |||/- Disown the functions (make them a background process), so that the children of a parent
| | ||||  will not be killed when the parent gets auto-killed.
| | |||| /- End of function-block.
| | |||| |/- End of definition.
/-\| |||| ||/- Execute the function ':'.  The chain-reaction begins.
.(){.|.&;};.
分类: SHELL 标签:

shell命令备忘录 — exec

2012年7月17日 Code007 没有评论

这个命令之前在分析repo的bootstrap的时候有说过,文章链接http://www.code007.org/?p=435,这里是用来当成自定义文件描述符,例如0 stdin,1 stdout,2 stderr。

:~/Desktop/SHELL$ exec 3<out.txt
:~/Desktop/SHELL$ echo test for this > out.txt
:~/Desktop/SHELL$ cat <&3
test for this
:~/Desktop/SHELL$ cat <&3#说明必须重新分配不然将不起作用
:~/Desktop/SHELL$
:~/Desktop/SHELL$ exec 4> out.txt
:~/Desktop/SHELL$ echo test for 4 >&4
:~/Desktop/SHELL$ cat out.txt
test for 4
:~/Desktop/SHELL$ exec 5>>out.txt
:~/Desktop/SHELL$ echo test for 5 >&5
:~/Desktop/SHELL$ cat out.txt
test for 4
test for 5
Note:
       |              Separates distinct commands in the pipeline.  The  stan‐
                      dard  output of the preceding command will be piped into
                      the standard input of the next command.

       |&             Separates distinct commands in the pipeline.  Both stan‐
                      dard  output and standard error of the preceding command
                      will be piped into the standard input of the  next  com‐
                      mand.   This form of redirection overrides forms such as
                      2> and >&.

       < fileName     The file named by fileName is opened  and  used  as  the
                      standard input for the first command in the pipeline.

       <@ fileId      FileId  must be the identifier for an open file, such as
                      the return value from a previous call to  open.   It  is
                      used  as the standard input for the first command in the
                      pipeline.  FileId must have been opened for reading.

       << value       Value is passed to the first  command  as  its  standard
                      input.

       > fileName     Standard  output  from the last command is redirected to
                      the file named fileName, overwriting its  previous  con‐
                      tents.

       2> fileName    Standard  error  from  all  commands  in the pipeline is
                      redirected to the file named fileName,  overwriting  its
                      previous contents.

      >& fileName    Both  standard output from the last command and standard
                      error from all commands are redirected to the file named
                      fileName, overwriting its previous contents.

       >> fileName    Standard  output  from the last command is redirected to
                      the file named fileName, appending  to  it  rather  than
                      overwriting it.

       2>> fileName   Standard  error  from  all  commands  in the pipeline is
                      redirected to the file named fileName, appending  to  it
                      rather than overwriting it.

       >>& fileName   Both  standard output from the last command and standard
                      error from all commands are redirected to the file named
                      fileName, appending to it rather than overwriting it.

       >@ fileId      FileId  must be the identifier for an open file, such as
                      the return value from a previous call to open.  Standard
                      fileName, appending to it rather than overwriting it.

       >@ fileId      FileId  must be the identifier for an open file, such as
                      the return value from a previous call to open.  Standard
                      output  from  the last command is redirected to fileId's
                      file, which must have been opened for writing.

       2>@ fileId     FileId must be the identifier for an open file, such  as
                      the return value from a previous call to open.  Standard
                      error from all commands in the pipeline is redirected to
                      fileId's file.  The file must have been opened for writ‐
                      ing.

       >&@ fileId     FileId must be the identifier for an open file, such  as
                      the  return  value  from  a previous call to open.  Both
                      standard output from the last command and standard error
                      from  all commands are redirected to fileId's file.  The
                      file must have been opened for writing.
分类: SHELL 标签: