Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70244
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-09 17:02
个人简介

专注

文章分类

全部博文(21)

文章存档

2015年(21)

我的朋友

分类: LINUX

2015-08-11 14:35:33

 我在安装MySQL时遇到的一个问题,现象如下:
tar zxvf  ~/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz  -C /opt/mysql
ln -s /opt/mysql/mysql-5.6.25-linux-glibc2.5-x86_64   /usr/local/mysql
ll /usr/local/
.....
lrwxrwxrwx. 1 root root   45 Aug 10 23:16 mysql -> /opt/mysql/mysql-5.6.25-linux-glibc2.5-x86_64
        其实是我是想rm掉/usr/local/mysql那个链接文件,没想到rm -rf /usr/local/mysql/直接把/opt/mysql/mysql-5.6.25-linux-glibc2.5-x86_64下的文件全部给删除掉了,再看ll /usr/local/没想到mysql -> /opt/mysql/mysql-5.6.25-linux-glibc2.5-x86_64文件还在.
    
    rm -rf /usr/local/mysql(不要结尾的"/")搞定,总结==>所以linux下rm删除文件养成结尾不要加"/"的习惯.
现附解决脚本:        
       上面提供的find and xargs的删除方法可以实现。但是只用rm为什么不能删除呢。我想应该是使用的方法上有问题,必须查阅rm和ln的用法。经过man查阅,ln的使用和 rm的使用并没有问题。推翻了前面的想法,我想从rm直接删除和find删除的不同入手找到原因。

[armlinux@lqm bootloader]$ find . -type l
./develop
./orig

看 来原因找到了。我在使用rm的时候总是习惯使用TAB键补全命令,但是TAB补全命令的时候,最后是以“/”结尾的。很明显的原因,rm也 好,unlink也好,并不能很好的处理这种情况,这算是一处bug。我在前面写shell脚本来实现autozip时的时候,自己遇到过这个问题,采用 了awk解决。原有的脚本如下:

[armlinux@lqm bin]$ cat autozip
#!/bin/bash
# Copyright 2007 (c), Shandong University
# All rights reserved.
#
# Filename : autozip
# Description: Compress files, and print "OK" out if the file
# can be compressed successfully.
# Syntax : autozip [filename | directory name]
# Author : Liu Qingmin
# Version : 1.0
# Date : 07-04-29
#
# Func: get_target()
# Desc: Obtain the name of target file
# Para: $1 -- file name that will be compressed
# Ret : TARGET -- current file name
get_target()
{
TARGET=`echo $1 |
awk -F/
'{if ($NF == "") print $(NF-1);
else print $(NF)}'
`
}
# Handle Parameters
if [ $# != 1 ];then
echo "Usage: `basename $0` "
exit 1
fi
# Assign the parameter to the Macro OPT
OPT=$1
# Uncompress files
if [ -d $OPT ]; then
get_target $OPT
tar zcvf ${TARGET}.tar.gz $OPT && echo "OK"
elif [ -f $OPT ]; then
get_target $OPT
cp $OPT tmp
gzip tmp
cp tmp.gz ${TARGET}.gz
rm tmp.gz
if [ -x ${TARGET}.gz ]; then
chmod -x ${TARGET}.gz
fi
echo "OK"
fi

上面的get_target就是对这个情况的处理。不过没有想到rm也无法处理这种情况,要知道,使用TAB键提高效率是经常用的手段啊。
找到了bug,还没有看rm的源代码,倒是可以利用上面的脚本的思路来解决这个小bug。写了一个脚本rmlink,如下:

[armlinux@lqm bin]$ cat rmlink
#!/bin/sh
# Copyright 2007 (c), Shandong University
# All rights reserved.
#
# Filename : rmlink
# Description : solve the bug of "rm" and "unlink"
# Syntax : rmlink <linkfile name>
# Author : Liu Qingmin
# Version : 1.0
# Date : 07-09-19
#
# Func: get_target()
# Desc: Obtain the name of target file
# Para: $1 -- file name that will be compressed
# Ret : TARGET -- current file name
get_target()
{
TARGET=`echo $1 |
awk -F/
'{if ($NF == "") print $(NF-1);
else print $(NF)}'
`
}
# Handle Parameters
if [ $# != 1 ];then
echo "Usage: `basename $0` "
exit 1
fi
# Assign the parameter to the Macro OPT
OPT=$1
# Uncompress files
if [ -d $OPT ]; then
# eliminate the "/" at the ending
get_target $OPT
# you also can use "unlink" instead of "rm"
rm ${TARGET}
fi
# OK
exit 0

测试:

[armlinux@lqm bootloader]$ ls
develop orig patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin
[armlinux@lqm bootloader]$ rmlink develop
[armlinux@lqm bootloader]$ rmlink orig
[armlinux@lqm bootloader]$ ls
patch u-boot-1.1.3 u-boot-1.2.0 u-boot-1.2.0.orig vivi vivi_origin







可见测试正常,rmlink可以正常使用。

至此,问题最终解决。
url:


阅读(1123) | 评论(0) | 转发(0) |
0

上一篇:MySQL数据类型详解

下一篇:SHELL特殊语法

给主人留下些什么吧!~~